Skip to content

Instantly share code, notes, and snippets.

@OhMeadhbh
Last active June 1, 2023 21:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OhMeadhbh/c89e861416262a1f18163867a107ac90 to your computer and use it in GitHub Desktop.
Save OhMeadhbh/c89e861416262a1f18163867a107ac90 to your computer and use it in GitHub Desktop.
Print something like a completion bar on terminals that support SIXEL graphics
This is a simple utility that prints a bar on a
terminal screen. It's no different than a thousand
other such utilities except it's output doesn't look
like crap. It uses SIXEL mode, so you'll have to
have a terminal (or terminal emulator) that supports
it. (if you start xterm with `xterm -ti vt340` it
should work.)
This is useful if, like me, you SSH into remote *nix
systems (like on AWS) and you want to get a graphical
representation of the percentage of something FROM THE
COMMAND LINE, so you don't have to take your fingers
off the keyboard, mouse over to your browser, wander
about the AWS console looking for the data you're after,
realize about five minuted into it they've changed the
UI so you have to open a help page and now you've
wasted 10 minutes.
So if you install these two shell scripts somewhere
in your path, you can run the following set of
commands and it will print out a progress bar:
clear ; \
for i in `seq 0 10` ; do \
vgxy 3 3 ; \
PER=$(( $i * 10 )) ; \
echo `sbar $PER 100` $PER% Complete ; \
sleep 1 ; \
done ; \
echo
Copyright (c) 2020 Meadhbh S. Hamrick
All Rights Reserved
Released under a 3-clause BSD license
#!/bin/bash
# Uses SIXELs to print a completion bar on standard out. The second parameter
# (FULL) is the total number of pixels on the inside of the bar. The first
# parameter (PART) is the number of pixels that are filled in. It is not a
# percentage, don't fall into the trap of thinking that it is.
#
# To use this script, you'll need to be executing the command on a terminal or
# terminal emulator that supports SIXEL graphics. If you're on a *nix system
# with X11, `xterm -ti vt340` should work. Or you can add this line to your
# ~/.Xresources file:
#
# XTerm*decTerminalID: vt340
PART=${1:-20}
FULL=${2:-24}
if [ "$PART" -lt 0 -o "$FULL" -lt 1 ] ; then
echo "`basename $0` $PART $FULL : non-positive values? are you high?" 1>&2
exit 1
fi
if [ "$PART" -gt "$FULL" ] ; then
PART=$FULL
fi
# Undocumented code to stop XTerm from adding a CRLF after rendering
echo -n -e "\033[?8452h"
# Start SIXEL mode
echo -n -e "\033Pq"
# Set color registers to WHITE and GREEN
echo -n -e "#0;2;99;99;99#1;2;0;99;0"
# Render 3 lines, 6 pixels high of graph data
echo -n -e "#0?o!${FULL}Oo\$#1??!${PART}_-"
echo -n -e "#0?~!${FULL}?~\$#1??!${PART}~-"
echo -n -e "#0?B!${FULL}AB\$#1??!${PART}@-"
# Exit SIXEL mode
echo -n -e "\033\\"
#!/bin/bash
# Emits the ANSI control sequence to move the cursor to a particular spot
# on the screen. Do not use negative values.
X=${1:-1}
Y=${2:-1}
if [ "$X" -lt 1 -o "$Y" -lt 1 ] ; then
echo "`basename $0` $X $Y : non-positive values? are you high?" 1>&2
exit 1
fi
# Use VT100 command to set x,y position
echo -n -e "\033[${Y};${X}H"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment