From 4e8e3b707339b611292374719ba89fa203e995e8 Mon Sep 17 00:00:00 2001 From: laily Date: Tue, 26 May 2026 16:54:53 +0800 Subject: [PATCH] feat: add notify command, modify sidekick with tmux --- fish/functions/_notify_emit.fish | 7 +++++++ fish/functions/_notify_human_duration.fish | 13 ++++++++++++ fish/functions/notify-done.fish | 24 ++++++++++++++++++++++ fish/functions/notify.fish | 18 ++++++++++++++++ nvim/lua/plugins/sidekick.lua | 2 +- 5 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 fish/functions/_notify_emit.fish create mode 100644 fish/functions/_notify_human_duration.fish create mode 100644 fish/functions/notify-done.fish create mode 100644 fish/functions/notify.fish diff --git a/fish/functions/_notify_emit.fish b/fish/functions/_notify_emit.fish new file mode 100644 index 0000000..d5fbf99 --- /dev/null +++ b/fish/functions/_notify_emit.fish @@ -0,0 +1,7 @@ +function _notify_emit --argument-names title body + if set -q TMUX + printf '\ePtmux;\e\e]777;notify;%s;%s\a\e\\' "$title" "$body" + else + printf '\e]777;notify;%s;%s\a' "$title" "$body" + end +end diff --git a/fish/functions/_notify_human_duration.fish b/fish/functions/_notify_human_duration.fish new file mode 100644 index 0000000..451c06e --- /dev/null +++ b/fish/functions/_notify_human_duration.fish @@ -0,0 +1,13 @@ +function _notify_human_duration --argument-names ms + test -z "$ms"; and set ms 0 + set -l s (math --scale=0 $ms / 1000) + if test $s -lt 1 + echo {$ms}ms + else if test $s -lt 60 + echo {$s}s + else if test $s -lt 3600 + echo (math --scale=0 $s / 60)m(math $s % 60)s + else + echo (math --scale=0 $s / 3600)h(math --scale=0 ($s % 3600) / 60)m + end +end diff --git a/fish/functions/notify-done.fish b/fish/functions/notify-done.fish new file mode 100644 index 0000000..fc29f1a --- /dev/null +++ b/fish/functions/notify-done.fish @@ -0,0 +1,24 @@ +function notify-done --description '前置包装:运行命令并在完成时发送 OSC 777 通知' + if test (count $argv) -eq 0 + echo "Usage: notify-done [args...]" >&2 + return 2 + end + + set -l start (date +%s) + $argv + set -l rc $status + set -l elapsed (math (date +%s) - $start) + set -l human (_notify_human_duration (math $elapsed \* 1000)) + + set -l label + if test $rc -eq 0 + set label "完成: $argv[1]" + else + set label "失败 exit=$rc: $argv[1]" + end + + set -l body "$human · "(hostname -s)" · "(string join ' ' $argv) + + _notify_emit "$label" "$body" + return $rc +end diff --git a/fish/functions/notify.fish b/fish/functions/notify.fish new file mode 100644 index 0000000..059cb5d --- /dev/null +++ b/fish/functions/notify.fish @@ -0,0 +1,18 @@ +function notify --description '发送 OSC 777 通知;无参时复用上一条命令的状态与耗时' + set -l rc $status + set -l dur $CMD_DURATION + + set -l label + if test (count $argv) -gt 0 + set label (string join ' ' $argv) + else if test $rc -eq 0 + set label "完成" + else + set label "失败 exit=$rc" + end + + set -l body (_notify_human_duration $dur)" · "(hostname -s)" · "(pwd | string replace $HOME '~') + + _notify_emit "$label" "$body" + return $rc +end diff --git a/nvim/lua/plugins/sidekick.lua b/nvim/lua/plugins/sidekick.lua index fbb34d9..fc10d10 100644 --- a/nvim/lua/plugins/sidekick.lua +++ b/nvim/lua/plugins/sidekick.lua @@ -5,7 +5,7 @@ return { cli = { mux = { backend = vim.env.ZELLIJ and "zellij" or "tmux", - enabled = false, + enabled = true, }, }, },