sci

最果て風呂

端末の裏方

まえふり

以前の記事で issue 登録をしたところ、検証や助言を頂くことができました。結論を先に言うと、この現象は私だけに発生するもので、根本的な解決にはなりませんが、現象を回避する方法がわかりました。ターミナル.app の環境設定で「ターミナルの宣言方法」を ansi にすれば良いのです。高給取りの人々をタダ働きさせてしまった感がありますが、請求書も届いていないことですし、別の何かで恩返しが出来ればと思っています。

裏方を覗くための環境整備

さて、もう終ったことなのですが、アドバイスの中で Higashi さんが Trachet というプログラムを使ってみ?ということでしたので、調べてみることにしました。

キータの kefir_ さん(あれ?この nyancat アイコン、端末で画像を表示するの人では?)の記事を読みながら進めます。

自分は Python3 しか使わないので、2 系のライブラリ等をシステムに入れたくありません。そこで Flask でやっているように venv で環境を整えてみます。

mkdir py2env
cd py2env
virtualenv --python=python2.7 venv
. venv/bin/activate
pip install trachet

これで 2.7 系での環境が整いました。あとは監視するための端末を別途開いて、tty を調べて起動します。

trachet -o/dev/ttys002

これで trachet を起動した方の端末に入力したものが ttys002 の端末側にもろもろの情報と一緒に出力さるようになります。これで監視が出来るというわけですね。

Vim を落す

早速、Vim を起動してみます。<SP> がたくさん表示されました。アプリケーションを起動するだけなのに、裏方では様々なことをしているのですね。

Vim が落ちる手順を進めたのですが、監視する側も一緒に落ちてしまうので log が取れずに意味がありません(>_<)

ここで諦める奈幾乃ではありません。監視する側を iTerm2 の端末に指定してやればいいんですよ!

xterm-256color で

  1. Terminal.app で trachet を起動し、iTerm2 で起動した端末を出力先として指定します。
  2. Vim を落とします。
  3. その時のログ。
<<<  j
>>>   CSI 23;22H              CUP / move cursor to (row=23, col=22)
>>>   CSI 1m                  SGR 1 / bold
>>>   CSI 7m                  SGR 7 / inverse
>>>  0<SP>
>>>   CSI 54C                 CUF / cursor forward
>>>  2/
>>>   CSI 2;1H                CUP / move cursor to (row=2, col=1)
<<<  j
>>>   CSI 23;22H              CUP / move cursor to (row=23, col=22)
>>>  124
>>>   CSI 53C                 CUF / cursor forward
>>>  3/
>>>   CSI 3;1H                CUP / move cursor to (row=3, col=1)
<<<  l
>>>   CSI m                   SGR 0 / reset attributes
>>>  <
<<<  x
>>>   CSI ?25l                DECRST 25 - DECTCEM / hide cursor
>>>   CSI 3;22r               DECSTBM / set top and bottom margins: top=3 bottom=22
>>>   CSI 22;1H               CUP / move cursor to (row=22, col=1)
>>>  <CR>                     CR / carriage return
>>>  <NL>                     LF / line feed
>>>   CSI 1;24r               DECSTBM / set top and bottom margins: top=1 bottom=24
>>>   CSI 3;1H                CUP / move cursor to (row=3, col=1)
>>>  <>
>>>   CSI 4;79H               CUP / move cursor to (row=4, col=79)
>>>  あ
>>>   CSI 5;1H                CUP / move cursor to (row=5, col=1)
>>>  ああああああああああああああああああああああああああああああああああああああ</p>
>>>   CSI 22;1H               CUP / move cursor to (row=22, col=1)
>>>   CSI 1m                  SGR 1 / bold
>>>   CSI 34m                 SGR 34 / set fourground color to blue
>>>  ~<SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP>
>>>   CSI m                   SGR 0 / reset attributes
>>>   CSI 23;10H              CUP / move cursor to (row=23, col=10)
>>>   CSI 1m                  SGR 1 / bold
>>>   CSI 7m                  SGR 7 / inverse
>>>  +][utf-8-unix]<SP>123
>>>   CSI m                   SGR 0 / reset attributes
>>>   CSI 24;1H               CUP / move cursor to (row=24, col=1)
>>>   CSI K                   EL 0 / erase line: from the cursor through the end of the line
>>>   CSI 3;2H                CUP / move cursor to (row=3, col=2)
>>>   CSI ?12l                DECRST 12 / steady cursor
>>>   CSI ?25h                DECSET 25 - DECTCEM / show cursor
<<<  k
>>>   CSI 23;25H              CUP / move cursor to (row=23, col=25)
>>>   CSI 1m                  SGR 1 / bold
>>>   CSI 7m                  SGR 7 / inverse
>>>  0<SP><SP><SP>
>>>   CSI 49C                 CUF / cursor forward
>>>  2/
>>>   CSI 2;1H                CUP / move cursor to (row=2, col=1)
<-- ここで k を入力しているのだけれど伝わってない?で落ちる。

ansi

  1. Vim が落ちないケースに Terminal.app の設定を変更(ansi へ)
  2. 上記と同様の操作をします(Vim は落ちない)。
  3. その時のログ。
<<<  j
>>>   CSI 23;22H              CUP / move cursor to (row=23, col=22)
>>>  124
>>>   CSI 53C                 CUF / cursor forward
>>>  3
>>>   CSI 3;1H                CUP / move cursor to (row=3, col=1)
<<<  l
>>>   CSI 0m                  SGR 0 / reset attributes
>>>  <
<<<  x
>>>  <BS>                     BS / back space
>>>   CSI M                   DL 1 / delete a line
>>>   CSI 22;1H               CUP / move cursor to (row=22, col=1)
>>>   CSI L                   IL 1 / insert a line
>>>   CSI 3;1H                CUP / move cursor to (row=3, col=1)
>>>  <>
>>>   CSI 4;79H               CUP / move cursor to (row=4, col=79)
>>>  あ
>>>   CSI 5;1H                CUP / move cursor to (row=5, col=1)
>>>  ああああああああああああああああああああああああああああああああああああああ</p>
>>>   CSI 22;1H               CUP / move cursor to (row=22, col=1)
>>>   CSI 34m                 SGR 34 / set fourground color to blue
>>>  ~<SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP><SP>
>>>   CSI 0m                  SGR 0 / reset attributes
>>>   CSI 23;10H              CUP / move cursor to (row=23, col=10)
>>>   CSI 1m                  SGR 1 / bold
>>>   CSI 7m                  SGR 7 / inverse
>>>  +][utf-8-unix]<SP>123
>>>   CSI 0m                  SGR 0 / reset attributes
>>>   CSI 24;1H               CUP / move cursor to (row=24, col=1)
>>>   CSI K                   EL 0 / erase line: from the cursor through the end of the line
>>>   CSI 3;2H                CUP / move cursor to (row=3, col=2)
<<<  k
>>>   CSI 23;25H              CUP / move cursor to (row=23, col=25)
>>>   CSI 1m                  SGR 1 / bold
>>>   CSI 7m                  SGR 7 / inverse
>>>  0<SP><SP>
>>>   CSI 50C                 CUF / cursor forward
>>>  2
>>>   CSI 2;1H                CUP / move cursor to (row=2, col=1)
<<<  k   <-- xterm 設定の場合はここで落ちるのだ!
>>>   CSI 23;25H              CUP / move cursor to (row=23, col=25)
>>>  4
>>>   CSI 52C                 CUF / cursor forward
>>>  1
>>>   CSI 1;1H                CUP / move cursor to (row=1, col=1)

設定によってやりとりする内容が違うのですね……。これ、ヒトの言語でいうところの方言って感じなのでしょうか?それとも外国語?すべてに対応するのは大変そう……。

おまけ

ちなみに VSCode の Terminal で動かした場合は次のようになりました。

<<<  j
>>>   CSI 22;22H              CUP / move cursor to (row=22, col=22)
>>>  0<SP>
>>>   CSI 90C                 CUF / cursor forward
>>>  2/
>>>   CSI 2;1H                CUP / move cursor to (row=2, col=1)
<<<  j
>>>   CSI 22;22H              CUP / move cursor to (row=22, col=22)
>>>  124
>>>   CSI 89C                 CUF / cursor forward
>>>  3/
>>>   CSI 3;1H                CUP / move cursor to (row=3, col=1)
<<<  l
>>>   CSI m                   SGR 0 / reset attributes
>>>  <
<<<  x
>>>   CSI ?25l                DECRST 25 - DECTCEM / hide cursor
>>>  >あああああああああああああああああああああああああああああああああああああああああああああああああああああああああ
>>>   CSI 5;5H                CUP / move cursor to (row=5, col=5)
>>>  </p>
>>>   CSI 5;9H                CUP / move cursor to (row=5, col=9)
>>>   CSI K                   EL 0 / erase line: from the cursor through the end of the line
>>>   CSI 22;10H              CUP / move cursor to (row=22, col=10)
>>>   CSI 1m                  SGR 1 / bold
>>>   CSI 7m                  SGR 7 / inverse
>>>  +][utf-8-unix]<SP>123
>>>   CSI 3;2H                CUP / move cursor to (row=3, col=2)
>>>   CSI ?12l                DECRST 12 / steady cursor
>>>   CSI ?25h                DECSET 25 - DECTCEM / show cursor
<<<  k
>>>   CSI 22;25H              CUP / move cursor to (row=22, col=25)
>>>  0<SP><SP><SP>
>>>   CSI 85C                 CUF / cursor forward
>>>  2/
>>>   CSI 2;1H                CUP / move cursor to (row=2, col=1)
<<<  k   <-- xterm 設定の場合はここで落ちるのだ!
>>>   CSI 22;25H              CUP / move cursor to (row=22, col=25)
>>>  4<SP>
>>>   CSI 87C                 CUF / cursor forward
>>>  1/
>>>   CSI 1;1H                CUP / move cursor to (row=1, col=1)