跳到主要内容
  1. Skills/
  2. 善用工具/

使用 asciinema 来记录命令行

··字数 1081·3 分钟
asciinema cmd

在有悟文章 《fd, 替代 find 命令的工具》一文中,有一段比较短的视频,里面演示了 fd 工具的操作效果。这个视频的封面画面上的命令 asciinema play fd,正在播放一段命令的执行过程。

是不是感觉很奇怪,一般视频不都是录制后播放嘛?那有什么好说的。请听有悟娓娓道来。

相关有悟文章 《fd, 替代 find 命令的工具》

本文所讲的命令行录制与播放,并不是使用录屏工具录制屏幕。

命令行记录,由来已久。通过终端工具的日志选项,把通过终端执行的命令与输出全部记录到日志文件中。在命令行工具上,总是有一些富有创意的工程师,在想办法让这个纯字符界面的工作变得生动有趣。

本文要介绍的是使用工具来记录命令行执行,这些记录可用于回放,用在于演示情形非常有用。目前主要有两个用户比较多的工具, ttyrec asciinema ,其中 asciinema 花样比较多,它不仅仅是一个命令行记录工具,还是一个命令行视频分享平台。用户除了过程录制外和回放,也可以上传到 asciinema.org 上进行分享。

ttyrec 比较久远,一直没有更新,本文还是围绕 asciinema 为重点来介绍。

安装 asciinema #

  • macOS 平台 在 macos 上,使用 brew 安装
> brew install asciinema
  • linux

在 ubuntu、debian 上,使用 apt 安装

> sudo apt-get install asciinema
  • python3

安装了 python3 的 macos 或者 linux,可以使用 pip 来安装。

> pip install asciinema

这也说明了 asciinema 是使用 python3 编写的。

  • window

别试了,windows 上不能用。有网友开发一个兼容 asciinema 的 dotnet 版本,叫 PowerSession ,我试用过,报 \AppData\Local\Temp\.net\PowerSession\ulm0zbkq.zae\PowerSession.dll 之后就果断弃坑。

如果你使用 windows 10,安装了 ubuntu 等 wsl linux 子系统环境,那么在 wsl 环境内可以使用 sudo apt 或者 pip 安装。

记录(录制) #

asciiname 的使用非常简单,在命令行使用 asciinema rec 启动,

~/Projects/aff/youwu git:(develop)
➜  asciinema rec
asciinema: recording asciicast to /var/folders/c0/231yc1tx5nzcd1p5bvkgp8z40000gn/T/tmpq_xdvw5a-ascii.cast
asciinema: press <ctrl-d> or type "exit" when you're done

asciinema 会生成一个临时文件,很来保存过程数据,使用 ctrl-d 或者输入 exit 退出录制状态。注意,如果你使用了 tmux,请不要 ctrl-d,因为很可能默认配置的关系,你的 tmux 窗口会被关闭。

~/Projects/aff/youwu git:(develop)
exit
asciinema: recording finished
asciinema: press <enter> to upload to asciinema.org, <ctrl-c> to save locally

View the recording at:

    https://asciinema.org/a/2aI6KSxjQYFAHCm6d0qM0mmMa

This installation of asciinema recorder hasn't been linked to any asciinema.org
account. All unclaimed recordings (from unknown installations like this one)
are automatically archived 7 days after upload.

If you want to preserve all recordings made on this machine, connect this
installation with asciinema.org account by opening the following link:

    https://asciinema.org/connect/dd2c337d-c74e-404b-b247-e4418aa96379

退出时,不心按了回车确认,我还没注册账号呢,怎么就被上传到 asciinema 上分享了,怎么删?😲😲😲

asciinema 回放 #

在上一节中介绍,使用 asciinema rec 来录制。为了可以回放,还是有必要为录制指定一个易取的位置来保存数据。

> asciinema rec 文件名

随便起个名字,然后使用下面的命令来播放:

> asciinema play 上面rec的那个文件名

asciinema 的其它说明 #

除了上面提到的 recplay命令,还有其它,使用 asciinema -h 查看 。

asciinema rec 的数据到底是什么东西 #

以 《fd, 替代 find 命令的工具》中为例, asciinema rec fd 命令生的fd文件开头如下, 原来是一个大大的json,里面记录了时间、窗口大小、每个命令与终端输出,输出的内容带有 ascii escape 颜色标记。

{"version": 2, "width": 63, "height": 21, "timestamp": 1621859591, "env": {"SHELL": "/usr/local/bin/zsh", "TERM": "screen-256color"}}
[2.119317, "o", "\u001b]1337;RemoteHost=youwu@yowutodaydeiMac.local\u0007\u001b]1337;CurrentDir=/Users/youwu/Projects/aff/youwu\u0007\u001b]1337;ShellIntegrationVersion=11;shell=zsh\u0007"]
[2.557297, "o", "\u001b[1m\u001b[3m%\u001b[23m\u001b[1m\u001b[0m                                                              \r \r"]
[2.557465, "o", "\u001bk..cts/aff/youwu\u001b\\"]
...