最終的な端末後処理の結果だけが維持されるように、tputを使用してコマンドの出力をどのように「レンダリング」できますか? [コピー]

最終的な端末後処理の結果だけが維持されるように、tputを使用してコマンドの出力をどのように「レンダリング」できますか? [コピー]

tputを使って画面に何かを描くスクリプトの出力をキャプチャしました。 cat myoutputを実行すると、すべてが明確になります(ターミナルが最初から再解釈するようです)。ただし、出力を編集またはパイプすると、tputclear waitなど、多くのansiシーケンスと以前のすべての項目の破壊的な印刷が表示されます。

最終的な「レンダリング」のみを取得できるように後処理するにはどうすればよいですか?

より良いことは、現在スクリプトを書いているので、端末を除くすべての項目をファイルとして印刷することです。

そしてexec > >(tee /dev/tty)

保存する前にstdoutチャンネルにすべてを「レンダリング」するように指示する方法はありますか?

ベストアンサー1

あなたが望むのはプログラムです明らかこれらの端末はシーケンスを制御し、最終ビューをレンダリングできます。その後、そのようなプログラムを呼び出します。端末エミュレータ。そのうちのいくつかは、gnome-terminalやalacrittyなどのシェルを使用するプログラムを起動するときのようなグラフィックであり、他のものには主に頭がありません。

これには、より古いまたはscreenより現代的なtmuxものがすべて関連しています。

  1. 「外部」スクリプトを作成します。
    1. 名前付きパイプの作成
    2. tmuxバックグラウンドで「内部」スクリプト(つまり、コンテンツを出力するスクリプト)を起動します。
    3. 外部スクリプトからFIFOを読み込みます(何も記録されないためブロックされます)。
    4. 読み取りが完了した後にtmux出力スクリーンショットを表示します。
  2. 内部スクリプトで、Named Pipeに何かを書いてスクリーンショットを撮りたい状態にあることを示します。

まとめてみるとこんな感じ

#!/usr/bin/zsh
# outer script
# SPDX-License-Identifier: WTFPL

# Make a temporary directory
tmpdir="$(mktemp -d)"

# Prepare the FIFO
fifo="${tmpdir}/fifo"
mkfifo "${fifo}"

# Start the inner script in tmux
tmux new-session -d -s "${tmpdir}" -e "FIFO=${fifo}" ./inner-script.sh …
#^   ^           ^  ^------+-----^ ^------+--------^ ^                 ^
#|   |           |         |              |          |                 |
#\------run tmux, the terminal emulator
#    |           |         |              |          |                 |
#    \---in tmux, run the "new-session" command to, well, get a new session
#                |         |              |          |                 |
#                \---detach that session, i.e. *don't* connect it to the current terminal
#                          |              |          |                 |
#                          \--specify the session name. Conveniently, we abuse the name
#                             of our temporary directory, as it's inherently unique
#                                         |          |                 |
#                                         \-- for the started command, set the environment
#                                             variable FIFO to the name of our FIFO
#                                                    |                 |
#                                                    \-- launch your script
#                                                …with its arguments--/

# Wait for something to be written to our FIFO
cat "${fifo}" > /dev/null

# instruct tmux to take a "pane shot"
tmux capture-pane -t "${tmpdir}" -p > "/path/to/capture"
#                 ^------+-----^  ^ ^---------+--------^
#                        |        |           |
#                        \-------------------------------- target (session name as above)
#                                 |           |
#                                 \----------------------- print to stdout
#                                             |
#                                             \----------- redirect stdout to file

# Finally, clean up session and temporary directory
tmux kill-session -t "${tmpdir}"
rm -rf "${tmpdir}"

fifoに作成した内容を追加するだけですinner-script.sh(例:)echo done > "${FIFO}"; sleep 100

すでに「ロギング」出力がある場合は、inner-script.sh次のことができます。cat recording.txt; echo done > "${FIFO}"; sleep 100

おすすめ記事