LoginSignup
0
4

More than 5 years have passed since last update.

Xonshでコマンドの実行時間を表示するイベントハンドラー的なの

Posted at

少し前からTLの影響でXonsh使ってます。

以前のシェルで「実行したコマンドの実行時間」を表示させてたことがあったのですが、Xonshでもやりたくなったので、超雑に作ってみました。

event_handlers.xsh
@events.on_postcommand
def _print_proc_time(
        cmd: str, rtn: int, out: str or None, ts: list) -> None:
    """Print time process after running command
    """
    dt_ = ts[1] - ts[0]
    print(f"{dt_:.2f} secs")

Xonshには結構なイベント用デコレータが存在しており、on_postcommandイベント を使えばコマンド実行後に処理を追加できそうです。
しかも、引数にある通りtsという処理開始前後のタイムスタンプを持つリストを渡してくれるので、何も考えずにそのまま使うのが一番楽そう。

組み込むとこんな感じで、プロンプト表示の手前に「何秒かかったか」が出るようになります。

xonsh-post_command-demo.gif

0
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
4