Skip to content

Instantly share code, notes, and snippets.

@dhamidi
Created August 6, 2025 17:48
Show Gist options
  • Select an option

  • Save dhamidi/474a605d11ce83bd50924c6ec3d68114 to your computer and use it in GitHub Desktop.

Select an option

Save dhamidi/474a605d11ce83bd50924c6ec3d68114 to your computer and use it in GitHub Desktop.
#!/usr/bin/env tclsh
proc every {interval body} {
while {true} {
uplevel 1 $body
after $interval set every_timer done
vwait every_timer
}
}
proc parse-interval {interval} {
if {[scan $interval %d result] != 1} {
set result 1
}
return [expr {$result * 1000}]
}
proc in-alternate-screen {body} {
puts -nonewline "\033\[?1049h"
clear-screen
catch { uplevel 1 $body }
puts -nonewline "\033\[?1049l"
}
proc cursor-to-top {} {
set esc [format %c 27]
puts -nonewline "${esc}\[H"
flush stdout
}
proc clear-screen {} {
puts -nonewline "\n\033\[2J"
flush stdout
}
proc clear-rest {} {
puts -nonewline "\n\033\[J"
flush stdout
}
if {$argc < 1} {
chan puts stderr {Usage: watch <command> [interval]}
}
set command [lindex $argv 0]
set interval [parse-interval [lindex $argv 1]]
in-alternate-screen {
every $interval {
set output ""
append output "\033\[H"
set result [catch {exec $env(SHELL) -c $command} outputLines]
if {$result == 0} {
append output $outputLines
} else {
append output "Error: $outputLines"
}
append output "\033\[J"
puts -nonewline $output
flush stdout
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment