mkogg.py: Fix 'self.get_mp4_metadata(self, source)'
[blog.git] / posts / script.mdwn
1 `script` is a nice utility for recording terminal sessions.  It spawns
2 a new shell (by default), and records any activity in that shell:
3
4     $ script /tmp/script
5     Script started, file is /tmp/script
6     $ ps -u wking | grep script
7     12102 pts/7    00:00:00 script
8     12103 pts/7    00:00:00 script
9     $ pstree 12102
10     script───script───bash───pstree
11     $ exit
12     exit
13     Script done, file is /tmp/script
14
15 The recorded file stores all the characters sent to the terminal:
16
17     $ cat -v /tmp/script
18     Script started on Mon 10 Dec 2012 08:07:17 AM EST
19     ^[]0;wking@mjolnir:~^G^[[?1034h^[[01;32mwking@mjolnir^[[01;34m ~ $^[[00m ps -u wking | grep script^M
20     12102 pts/7    00:00:00 ^[[01;31m^[[Kscript^[[m^[[K^M
21     12103 pts/7    00:00:00 ^[[01;31m^[[Kscript^[[m^[[K^M
22     ^[]0;wking@mjolnir:~^G^[[01;32mwking@mjolnir^[[01;34m ~ $^[[00m pstr^G^M
23     pstree   pstruct  ^M
24     ^[[01;32mwking@mjolnir^[[01;34m ~ $^[[00m pstr^M
25     pstree   pstruct  ^M
26     ^[[01;32mwking@mjolnir^[[01;34m ~ $^[[00m pstree 12102^M
27     scriptM-bM-^TM-^@M-bM-^TM-^@M-bM-^TM-^@scriptM-bM-^TM-^@M-bM-^TM-^@M-bM-^TM-^@bashM-bM-^TM-^@M-bM-^TM-^@M-bM-^TM-^@pstree^M
28     ^[]0;wking@mjolnir:~^G^[[01;32mwking@mjolnir^[[01;34m ~ $^[[00m exit^M
29     exit^M
30
31     Script done on Mon 10 Dec 2012 08:07:51 AM EST
32
33 You can also record timing information in a separate file (this
34 approach was developed by Joey Hess and posted in [Debian bug
35 68556][68556]):
36
37     $ script --timing=/tmp/timing /tmp/script
38     Script started, file is /tmp/script
39     $ echo hello
40     hello
41     $ exit
42     exit
43     Script done, file is /tmp/script
44
45 The timing file has a “delay in seconds” column and a “number of
46 characters to send column”:
47
48     $ cat /tmp/timing
49     0.671478 67
50     0.159100 1
51     0.941919 1
52     0.149764 1
53
54 You can play back a script with timing information:
55
56     $ scriptreplay -t /tmp/timing -s /tmp/script
57     …playback…
58
59 You can also play it back with altered timing (e.g. in slow motion):
60
61     $ scriptreplay -t /tmp/timing -s /tmp/script -d 0.5
62     …half-speed playback playback…
63
64 This even works reasonably well with curses applications (`emacs`,
65 `vi`, …), but information such as window size is not recorded or
66 replayed.  From util-linux's `scriptreplay(1)`:
67
68 > Since the same information is simply being displayed, scriptreplay
69 > is only guaranteed to work properly if run on the same type of
70 > terminal the typescript was recorded on.  Otherwise, any escape
71 > characters in the typescript may be interpreted differently by the
72 > terminal to which scriptreplay is sending its output.
73
74 This means that if you want interoperable replay, you'll want to do
75 something like
76
77     $ reset
78     $ echo "$TERM $LINES $COLUMNS"
79     xterm 24 80
80
81 early on so folks know how to setup their replay environment.  If
82 you're using some wonky `$TERM`, you may even want to post your
83 terminfo:
84
85     $ infocmp
86     xterm|xterm terminal emulator (X Window System),
87             am, bce, km, mc5i, mir, msgr, npc, xenl,
88             colors#8, cols#80, it#8, lines#24, pairs#64,
89             acsc=``aaffggiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~,
90     …
91
92 The user can compare with their current terminal:
93
94     $ infocmp xterm linux
95     comparing xterm to linux.
96         comparing booleans.
97             ccc: F:T.
98             eo: F:T.
99             …
100         comparing numbers.
101             cols: 80, NULL.
102             lines: 24, NULL.
103             ncv: NULL, 18.
104         …
105
106 It would be nice if there was an `iconv`-style converter to translate
107 between terminal operation encodings, but I haven't found one yet.
108 [[Screen]] [does something like this internally][screen], and their
109 [list of control sequences][seq] is a useful reference.  I've started
110 work on an [[escape-sequence-to-HTML
111 converter|script/script-publish.py]], in case you want to play around
112 with these conversions in [[Python]].
113
114
115 [68556]: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=68556
116 [screen]: http://www.gnu.org/software/screen/manual/html_node/Virtual-Terminal.html
117 [seq]: http://www.gnu.org/software/screen/manual/html_node/Control-Sequences.html#Control-Sequences