termtosvg - 在Linux中记录终端会话的工具

时间:2020-03-05 15:29:53  来源:igfitidea点击:

在演示文稿或者程序/教程期间,我们通常需要介绍在终端中执行的命令的结果。
当存在受众时,可以立即显示输出,但对于那些不存在的人来说,它们对于显示命令执行的录制视频将是有趣的。
这可以通过一些工具来完成,但我们将采用特定的外观Termstovg,这是一个用Python编写的Linux终端录像机,它将结果呈现为SVG动画。

TermStovg是用Python编写的工具,允许录制Linux终端会话。
有趣的方式是呈现它作为独立SVG动画的渲染。
然后,我们可以使用Web浏览器在录制后播放动画。
此工具有几个主题可使用特定颜色主题录制终端会话。
它是由Nicolas Bodos开发的GitHub项目,这非常有趣。

安装termstovg.

所有我们需要的准备工作是安装在Linux计算机中的Python。
好消息是与Python 3.5等兼容性。
例如,在Ubuntu 18.04上,我们已经安装了Python,默认版本是Python-3.6.

我们可以查看以下:

$dpkg -l | grep python3.6
ii libpython3.6:amd64 3.6.5-3 amd64 Shared Python runtime library (version 3.6)
ii libpython3.6-minimal:amd64 3.6.5-3 amd64 Minimal subset of the Python language (version 3.6)
ii libpython3.6-stdlib:amd64 3.6.5-3 amd64 Interactive high-level object-oriented language (standard library, version 3.6)
ii python3.6 3.6.5-3 amd64 Interactive high-level object-oriented language (version 3.6)
ii python3.6-minimal 3.6.5-3 amd64 Minimal subset of the Python language (version 3.6)

现在我们需要安装pip for python

$sudo apt install python3-pip
Reading package lists... Done
Building dependency tree 
Reading state information... Done
The following additional packages will be installed:
...
...

现在安装Termtosvg的要求

  • pyte渲染终端屏幕
  • svgwrite创建SVG动画
  • python-xlib要查询x服务器以获取颜色配置并解析Xresources数据
  • Base16-xresources for默认颜色主题
$pip3 install pyte python-xlib svgwrite
Collecting pyte
...
Collecting python3-xlib
 Downloading https://files.pythonhosted.org/packages/ef/c6/2c5999de3bb1533521f1101e8fe56fd9c266732f4d48011c7c69b29d12ae/python3-xlib-0.15.tar.gz (132kB)
....

现在有关默认颜色主题,请运行以下命令:

$curl https://raw.githubusercontent.com/chriskempson/base16-xresources/master/xresources/base16-default-dark-256.Xresources >> ~/.Xresources
$xrdb -load ~/.Xresources

文件~/.xressources现在存在于家中,我们可以粘贴下面的Termtosvg颜色。

$vim ~/.Xresources
termtosvg.foreground: #f8f8f2
termtosvg.background: #272822
termtosvg.color0: #272822
termtosvg.color1: #f92672
termtosvg.color2: #a6e22e
termtosvg.color3: #f4bf75
termtosvg.color4: #66d9ef
termtosvg.color5: #ae81ff
termtosvg.color6: #a1efe4
termtosvg.color7: #f8f8f2
termtosvg.color8: #75715e
termtosvg.color9: #fd971f
termtosvg.color10: #383830
termtosvg.color11: #49483e
termtosvg.color12: #a59f85
termtosvg.color13: #f5f4f1
termtosvg.color14: #cc6633
termtosvg.color15: #f9f8f5

然后我们可以通过pip安装termtosvg

# pip3 install termtosvg
Collecting termtosvg
 Downloading https://files.pythonhosted.org/packages/04/43/dd755b9185f58c4a4cf49fa710a0966b32b68e930c33bab785cf926201bd/termtosvg-0.2.2-py3-none-any.whl
Collecting pyte (from termtosvg)
 Downloading https://files.pythonhosted.org/packages/66/37/6fed89b484c8012a0343117f085c92df8447a18af4966d25599861cd5aa0/pyte-0.8.0.tar.gz (50kB)
 100% |████████████████████████████████| 51kB 6.0MB/s 
Collecting python-xlib (from termtosvg)
 Downloading https://files.pythonhosted.org/packages/54/44/e56454e3ce8fd2333e635d704e157e9cc432a375ab6b680e3c98dd7c3bc0/python_xlib-0.23-py2.py3-none-any.whl (123kB)
 100% |████████████████████████████████| 133kB 5.5MB/s 
Collecting svgwrite (from termtosvg)
 Downloading https://files.pythonhosted.org/packages/9f/27/a29fc710b5fc4dc8031d55e903c1352a194df4014dccf8b507049dd754e6/svgwrite-1.1.12-py2.py3-none-any.whl (64kB)
 100% |████████████████████████████████| 71kB 6.5MB/s 
Requirement already satisfied: setuptools in /usr/lib/python3/dist-packages (from termtosvg)
Collecting wcwidth (from pyte->termtosvg)
 Downloading https://files.pythonhosted.org/packages/7e/9f/526a6947247599b084ee5232e4f9190a38f398d7300d866af3ab571a5bfe/wcwidth-0.1.7-py2.py3-none-any.whl
Requirement already satisfied: six>=1.10.0 in /usr/lib/python3/dist-packages (from python-xlib->termtosvg)
Requirement already satisfied: pyparsing>=2.0.1 in /usr/lib/python3/dist-packages (from svgwrite->termtosvg)
Building wheels for collected packages: pyte
 Running setup.py bdist_wheel for pyte ... done
 Stored in directory: /root/.cache/pip/wheels/c0/dd/4a/d0ec26b9d07a3b48e25ba3456dc9bcab875686af6da9e23fcd
Successfully built pyte
Installing collected packages: wcwidth, pyte, python-xlib, svgwrite, termtosvg
Successfully installed pyte-0.8.0 python-xlib-0.23 svgwrite-1.1.12 termtosvg-0.2.2 wcwidth-0.1.7

记录终端会话

要开始录制会话实时终端,只需在终端中运行termstosvg命令并使用exit命令结束录制过程。

开始录音

$termtosvg
Recording started, enter "exit" command or Control-D to end

然后输入命令和工作。
完成后,我们可以结束录制实时会话。
我们可以看到它指示路径(通常为`/tmp)和录制文件的名称

$exit
exit
Recording ended, SVG animation is /tmp/termtosvg_ksz18r3y.svg