やってみる

アウトプットすべく己を導くためのブログ。その試行錯誤すらたれ流す。

watchexec ファイル監視ツールを使ってみた

 ファイルが更新されたときに任意のコマンドを実行するコマンド。

情報源

インストール

cargo install watchexec-cli

 時間がかかった。

使ってみる

ヘルプ

ヘルプ

watchexec --help
watchexec 1.17.1
Execute commands when watched files change

USAGE:
    watchexec [FLAGS] [OPTIONS] <command>...

FLAGS:
        --changes-only         Only print path change information. Overridden by --verbose
    -c, --clear                Clear screen before executing command
    -h, --help                 Prints help information
        --no-default-ignore    Skip auto-ignoring of commonly ignored globs
        --no-environment       Do not set WATCHEXEC_*_PATH environment variables for the command
        --no-ignore            Skip auto-loading of ignore files (.gitignore, .ignore, etc.) for filtering
        --no-meta              Ignore metadata changes
        --no-process-group     Do not use a process group when running the command
    -n, --no-shell             Do not wrap command in a shell. Deprecated: use --shell=none instead.
        --no-vcs-ignore        Skip auto-loading of .gitignore files for filtering
    -N, --notify               Send a desktop notification when watchexec notices a change (experimental, behaviour may change)
    -p, --postpone             Wait until first change to execute command
    -r, --restart              Restart the process if it's still running. Shorthand for --on-busy-update=restart
    -V, --version              Prints version information
    -v, --verbose              Print debugging messages to stderr
    -W, --watch-when-idle      Deprecated alias for --on-busy-update=do-nothing, which will become the default in 2.0.

OPTIONS:
    -d, --debounce <milliseconds>            Set the timeout between detected change and command execution, defaults to 100ms
    -e, --exts <extensions>                  Comma-separated list of file extensions to watch (e.g. js,css,html)
    -f, --filter <pattern>...                Ignore all modifications except those matching the pattern
    -i, --ignore <pattern>...                Ignore modifications to paths matching the pattern
        --on-busy-update <on-busy-update>    Select the behaviour to use when receiving events while the command is running. Current
                                             default is queue, will change to do-nothing in 2.0. [possible values: do-nothing, queue,
                                             restart, signal]
    -w, --watch <path>...                    Watch a specific file or directory
        --force-poll <interval>              Force polling mode (interval in milliseconds)
        --shell <shell>                      Use a different shell, or `none`. E.g. --shell=bash
    -s, --signal <signal>                    Send signal to process upon changes, e.g. SIGHUP

ARGS:
    <command>...    Command to execute

Use @argfile as first argument to load arguments from the file `argfile` (one argument per line) which will be inserted in place of the
@argfile (further arguments on the CLI will override or add onto those in the file).

ファイルの文字数を表示する

 ファイル保存されるたびにポップアップで文字数を教えてもらう。

 という予定だったが、なぜか毎回おなじ文字数しか報告してくれない。どうやら最初の文字数から変化しないようだ。

 ポップアップするタイミングは保存した直後なので合っている。なら、一体なぜ文字数だけが昔のままなのか。謎。

TARGET=test.md
watchexec -w $TARGET -- zenity --info --text="$(wc -c $TARGET)"'文字です'

 以下のように色々と試してみたが変わらず。

  • -r, -W, --force-poll 1オプションを付与してみた
  • TARGETの値をフルパスに変更してみた

所感

 本当は1文字入力するたびに文字数をカウントして、フロントマターに書き出したかった。

 別のアプローチが必要か。

 VSCode拡張機能を自作するのはどうだろう。エディタ限定になってしまうが仕方ない。全ファイル一括してカウントするのはコストが多すぎる。1ファイルを保存するたびにやるのが必要最小限のはず。だからファイル監視してコマンドでやりたかったのに。watchexecは使えなかった。

対象環境

$ uname -a
Linux raspberrypi 5.10.52-v7l+ #1441 SMP Tue Aug 3 18:11:56 BST 2021 armv7l GNU/Linux