Skip to content

Instantly share code, notes, and snippets.

@mblsha
Last active August 29, 2015 14:14
Show Gist options
  • Save mblsha/caf522fea62ff7bbb778 to your computer and use it in GitHub Desktop.
Save mblsha/caf522fea62ff7bbb778 to your computer and use it in GitHub Desktop.
Monitors finished processes in inactive Terminal.app tabs, and activate them by clicking on the notification
# Add these two event hooks to your ~/.config/fish/config.fish
#
# Prerequisites:
# 1. fish 2.1.2. Use "brew upgrade --HEAD fish" to install
# 2. terminal-notifier. Use "brew install terminal-notifier" to install
# 3. Add other files to some directory that's in PATH, and ensure all are marked as executable (chmod +x)
function mbl_preexec --on-event fish_preexec
set -l now (date +%s)
set -g last_exec_timestamp $now
end
function mbl_postexec --on-event fish_postexec
set -l last_status $status
set -l last_cmd_line $history[1]
set -l now (date +%s)
set -l taken (math $now - $last_exec_timestamp)
if test $taken -gt 1
postexec.fish $last_status $taken $last_cmd_line &
end
end
#!/usr/bin/env osascript -l JavaScript
// activates specified tty tab in Terminal.app
function activateTty(tty) {
app = Application("Terminal")
for (windowIndex in app.windows()) {
w = app.windows()[windowIndex]
for (tabIndex in w.tabs()) {
t = w.tabs()[tabIndex]
if (t.tty() == tty) {
t.selected = true
if (w.index() != 1) {
// work-around for weird window activation
// http://stackoverflow.com/questions/13173514/applescript-bring-window-to-foreground
w.index = 1
w.visible = false
w.visible = true
}
app.activate()
break
}
}
}
}
function run(argv) {
for (i in argv) {
tty = argv[i]
activateTty(tty)
}
}
#!/usr/bin/env osascript -l JavaScript
// prints tty of active tab in Terminal.app
function printActiveTerminalTty() {
ObjC.import("stdio")
app = Application("Terminal")
for (windowIndex in app.windows()) {
w = app.windows()[windowIndex]
for (tabIndex in w.tabs()) {
t = w.tabs()[tabIndex]
if (!t.selected())
continue
$.printf("%s", t.tty())
}
// first window is the frontmost one
break
}
}
function run(argv) {
printActiveTerminalTty()
}
#!/usr/bin/env fish
set -l terminal_notifier (which terminal-notifier)
if test $status -ne 0
exit 1
end
set -g returncode $argv[1]
set -g taken $argv[2]
set -g cmd_name $argv[3]
# ignore some commands
switch $cmd_name
case "man*" "less*" "ssh*"
exit 0
end
function notify
# print BEL, which makes Terminal.app dock icon jump
# and adds Bell icon to the tab
printf '\a'
set -l active_tty (tty)
set -l human_taken (time_interval.fish $taken)
if test $returncode -eq 0
set -g title "$human_taken ($active_tty)"
else
set -g title "Returned $returncode, took $human_taken ($active_tty)"
end
set -l activate_terminal_tty (which activate-terminal-tty.jxa)
set -l activate_command $activate_terminal_tty $active_tty
terminal-notifier -title $title -message $cmd_name -execute "$activate_command"
end
set -l active_app (osascript \
-e 'tell application (path to frontmost application as text)' \
-e ' name as text' \
-e 'end tell')
if test $active_app = "Terminal"
set -l active_tty (tty)
set -l active_terminal_tty (active-terminal-tty.jxa)
if test $active_tty = $active_terminal_tty
# do nothing
else
notify
end
else
notify
end
#!/usr/bin/env fish
set -l interval $argv[1]
if test (uname -s) = 'Darwin'
date -u -r $interval +"%T"
else
date -u -d @$interval +"%T"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment