Skip to content

Instantly share code, notes, and snippets.

@naoki-mizuno
Created April 7, 2026 11:05
Show Gist options
  • Select an option

  • Save naoki-mizuno/7570414697fcd228c49f67d1826cad1e to your computer and use it in GitHub Desktop.

Select an option

Save naoki-mizuno/7570414697fcd228c49f67d1826cad1e to your computer and use it in GitHub Desktop.
Emit OSC 9;4 progress bar escape sequences
#!/usr/bin/env bash
# osc-progress - Emit OSC 9;4 progress bar escape sequences
#
# Usage:
# osc-progress <value> # set progress (0-100)
# osc-progress normal:60 # same as above (at 60%)
# osc-progress info:60 # same as above (at 60%)
# osc-progress indeterminate # set indeterminate/pulsing state
# osc-progress paused # set paused state
# osc-progress paused:60 # set paused state at 60%
# osc-progress error # set error state
# osc-progress error:60 # set error state at 60%
# osc-progress clear # clear progress bar
#
# echo "42" | osc-progress # read value from stdin
# echo "error:42" | osc-progress # same format as command line argument
#
# States: 0=clear, 1=normal, 2=error, 3=indeterminate, 4=paused
set -euo pipefail
_emit() {
local seq="$1"
if [[ -n "${TMUX:-}" ]]; then
printf '\033Ptmux;\033%s\033\\' "$seq"
else
printf '%s' "$seq"
fi
}
_progress() {
local state="$1"
local value="${2:-}"
if [[ -n "$value" ]]; then
_emit $'\033]9;4;'"${state};${value}"$'\007'
else
_emit $'\033]9;4;'"${state}"$'\007'
fi
}
_usage() {
sed -n '2,18p' "$0" | sed 's/^# \{0,1\}//'
exit 1
}
# Read from stdin if piped
if [[ ! -t 0 ]] && [[ $# -eq 0 ]]; then
read -r value
set -- "$value"
fi
[[ $# -eq 0 ]] && _usage
arg="${1:-}"
cmd="${arg%%:*}"
val="${arg#*:}"
[[ "$val" == "$arg" ]] && val="" # no colon present
case "$cmd" in
clear) _progress 0 ;;
normal|info) _progress 1 "$val" ;;
error) _progress 2 "$val" ;;
indeterminate) _progress 3 ;;
paused) _progress 4 "$val" ;;
[0-9]|[0-9][0-9]|100)
_progress 1 "$cmd" ;;
*)
echo "osc-progress: invalid argument: $arg" >&2
_usage ;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment