如何在Hugo博客中使用终端播放器Asciinema-Player
Asciinema-Player是一款著名的的终端录制播放器,可用于播放asciinema录制的播放文件,经常被用来进行终端操作演示。这里也将其引入到了当前Hugo博客中使用,下面我将讲讲引入的过程。
1.效果展示
这里直接借用官方demo进行展示。
> # Welcome to asciinema! > # See how easy it is to record a terminal session > # First install the asciinema recorder > brew install asciinema ==> Downloading https://homebrew.bintray.com/bottles/asciinema-2.0.2_2.catalina.bottle.1.tar.gz ==> Downloading from https://akamai.bintray.com/4a/4ac59de631594cea60621b45d85214e39a90a0ba8ddf4eeec5cba34bd6145711 ######################################################################## 100.0% ==> Pouring asciinema-2.0.2_2.catalina.bottle.1.tar.gz 🍺 /usr/local/Cellar/asciinema/2.0.2_2: 613 files, 6.4MB > # Now start recording > asciinema rec asciinema: recording asciicast to /tmp/u52erylk-ascii.cast asciinema: press <ctrl-d> or type "exit" when you're done bash-3.2$ # I am in a new shell instance which is being recorded now bash-3.2$ sha1sum /etc/f* | tail -n 10 | lolcat -F 0.3 da39a3ee5e6b4b0d3255bfef95601890afd80709 /etc/find.codes 88dd3ea7ffcbb910fbd1d921811817d935310b34 /etc/fstab.hd 442a5bc4174a8f4d6ef8d5ae5da9251ebb6ab455 /etc/ftpd.conf 442a5bc4174a8f4d6ef8d5ae5da9251ebb6ab455 /etc/ftpd.conf.default d3e5fb0c582645e60f8a13802be0c909a3f9e4d7 /etc/ftpusers bash-3.2$ # To finish recording just exit the shell bash-3.2$ exit exit asciinema: recording finished asciinema: press <enter> to upload to asciinema.org, <ctrl-c> to save locally https://asciinema.org/a/17648 > # Open the above URL to view the recording > # Now install asciinema and start recording your own sessions > # Oh, and you can copy-paste from here > # Bye!
2.需求分析
作为技术人员,在写博客时总是需要进行一些终端操作演示,而演示方式无非以下几种:
方式 | 优点 | 缺点 |
---|---|---|
视频 | 能配音配特效 | 网站流量消耗大,不能复制文本 |
动图 | 文件相对小些 | 不能控制进度,不能复制文本 |
代码 | 占用流量小,能复制 | 不能控制进度,也不太好展示效果 |
而终端录制播放器Asciinema-Player则兼容体积小、能控制、能复制于一体并能完美复现终端操作场景,非常适合用于在博客中进行一些终端操作演示。
3.解决方案
首先,在网上找下前人在Hugo博客里扩展嵌入asciinema-player的方法,比如 Embedding asciinema cast in your Hugo site。
shortcode
可以找到里面asciinema-player的shortcode代码如下:
|
|
细细口味了下这段代码,结合自己需求进行了修改:
|
|
注意看高亮部分,主要修改了以下几点:
if .Get
形式代码过于累赘,这里把不需要取默认值的语句统统改成了with .Get
形式;- 只有固定的
key
方法从本站获取.cast
录制文件,这里扩展了src
以便从站外获取录制文件地址; poster
这里会得到一个奇怪的数据#ZgotmplZ
,它是一个安全防护的默认数据,见官方说明,会导致设置指定时间封面无效,解决起来也简单,加上| safeURL
管道方法就可解决;
js、css
参考方案里动态引入js
和css
,用到的该播放器的文章里需设置asciinema
为true
:
|
|
|
|
可以看到上面是通过在文章里加asciinema
参数实现的js
、css
资源动态加载,其实可以参考其它如mermaid
的接入方式,直接在渲染时置标记就好:
|
|
其中$params.draft
是为了开发模式下也能设置草稿参数进行预览:
|
|
调整下css
样式,让播放器能够在超出显示区域时滚动过去:
|
|
font
可以注意到font-family
额外加了Terminal Glyphs
,这也可参考了 Custom Fonts in Asciinema。
主要是为了不至于出现下文中乱码的问题。正常如下:
╭─ …/Code/blog master 2.38G 91% (6:00) 22:18:33 │ ╰ exi
4.使用方法
asciinema-player
asciinema-player即播放器,在博客中使用只需简单在文章中加入以下代码:
|
|
其中,cols
、rows
分别为行列,preload
则是要不要预加载,poster
则是封面,有data
模式也有npt
模式,npt
即按时间截取封面,上面的就是截取55
秒时的终端作为封面。
另外还有speed
:播放速度,autoplay
:自动播放等参数,具体可参考asciinema-player设置
呈现效果如下(借用了helix-editor的演示,文件大些可能加载慢些):
476 doc.set_selection(view.id, Selection::point(pos)); 477 } 478 479 fn go┌──────────────────────────────────────────────┐┌──────────────────────────────────────────────┐ 480 g│ ││ use serde::de::{self, Deserialize, Deseriali │ 481 } │──────────────────────────────────────────────││ │ 482 │ Context ││ pub struct Context<'a> { │ 483 fn go│ selected_register ││ pub selected_register: helix_view::Regis │ 484 g│ count ││ pub count: Option<NonZeroUsize>, │ 485 } │ editor ││ pub editor: &'a mut Editor, │ 486 │ callback ││ │ 487 fn go│ on_next_key_callback ││ pub callback: Option<crate::compositor:: │ 488 g│ jobs ││ pub on_next_key_callback: Option<Box<dyn │ 489 } │ impl Context<'a> ││ pub jobs: &'a mut Jobs, │ 490 │ > push_layer ││ } │ 491 fn mo│ on_next_key ││ │ 492 l│ callback ││ impl<'a> Context<'a> { │ 493 l│ count ││ /// Push a new component onto the compos │ 494 l│ Align ││ pub fn push_layer(&mut self, component: │ 495 │ Top ││ self.callback = Some(Box::new(|compo │ 496 l│ Center ││ compositor.push(component) │ 497 │ Bottom ││ })); │ 498 │ align_view ││ } │ 499 │ Command ││ │ 500 d│ name ││ #[inline] │ 501 } │ fun ││ pub fn on_next_key( │ 502 │ doc ││ &mut self, │ 503 fn mo│ commands ││ on_next_key_callback: impl FnOnce(&m │ 504 l│ impl Command ││ ) { │ 505 l│ execute ││ self.on_next_key_callback = Some(Box │ 506 l│ name ││ } │ 507 └──────────────────────────────────────────────┘└──────────────────────────────────────────────┘ 508 let selection = doc NOR helix-term/src/commands.rs[+] 0 496:16
点击播放按钮播放,可拖动进度条或者使用方向键控制播放进度。
asciinema
上述终端播放所使用的cast
文件,都是使用asciinema在终端录制的。
安装
|
|
使用
|
|
另外如果想将其转化为gif动图,也可以使用asciicast2gif。
5.总结
通过以上实践,虽然过程有些曲折,但最终还是顺利地将asciinema-player终端播放器引入到了hugo中使用,后续发布的博客也将经常见到其身影。
v1.5.0