Skip to content

Instantly share code, notes, and snippets.

@iliakonnov
Created January 1, 2019 17:11
Show Gist options
  • Save iliakonnov/9a152613d541fd5dcc98171dab8450a3 to your computer and use it in GitHub Desktop.
Save iliakonnov/9a152613d541fd5dcc98171dab8450a3 to your computer and use it in GitHub Desktop.
Simple zsh function to run command every second
monitor() {
heading_template='\[$(date +%T)\] '
heading_length=11 # "[HH:MM:SS] "=11
#$(echo -n '{CMD}' | cut -c -$(( $COLUMNS - {heading_length} )) )
heading_template+="\$( echo -n '$@' | cut -c -\$(( \$COLUMNS - $heading_length )) )"
heading=$(eval echo -n $heading_template)
output=$(eval $@ 2>&1)
while true; do
echo "\033[4m$heading\033[0m"
echo $output
sleep 1
# calculate next output
new_output=$(eval $@)
# then clean previous
lines=$(echo -n "$output" | wc -l)
lines=$(( $lines + 1 )) # add header
echo -n "\033[0G" # move to beginning of line
for i ({0..$lines}); do
echo -n "\033[K\033[F" # clear line and move up
done
echo -n "\033[K" # clear line again
output=$new_output
heading=$(eval echo -n $heading_template)
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment