LoginSignup
1
1

Crsytalで実行が終了したらnotifyするコマンドラインツールを作る

Last updated at Posted at 2023-12-20

はじめに

Crystalのアドベントカレンダーです。

Ubuntuのデスクトップ通知を出すCrystalのライブラリが notify.cr です。

これを使って、時間のかかるコマンドを実行したときに、通知を出すコマンドラインツールを作ってみます。

アイディア

こんな感じのツールを想定しています。

nt crystal build huge_project.cr

ビルドが終わると通知が出ます。もちろん引数や標準入出力も自在に使えてほしいですね。

使い方

とりあえず次のようなコードを書きました。

nt.cr
require "notify"

notifier = Notify.new

command = ARGV.join(" ")
status : Process::Status 

elapsed_time = Time.measure do
  status = Process.run(command, shell: true, input: STDIN, output: STDOUT, error: STDERR)
end

if status.success?
  notifier.notify "成功 #{elapsed_time}", body: command
else
  notifier.notify "失敗 #{elapsed_time}", body: command
end

細かいところ、大丈夫か確信はありませんが、なんとなく動いてますね。

(notify.crの動作確認してテスト追加してPR送ってたら深夜になってしまいました)

この記事は以上です。

1
1
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
1
1