Skip to content

Instantly share code, notes, and snippets.

@xunker
Last active October 11, 2024 18:23
Show Gist options
  • Save xunker/d0474365683e70680796d7cd67006259 to your computer and use it in GitHub Desktop.
Save xunker/d0474365683e70680796d7cd67006259 to your computer and use it in GitHub Desktop.
"ruler" function for Bash/Zsh, print a ruler of given length to your terminal
# Print out a line of text of given length to use as a rulter to resize your
# terminal. Requires some version of `ruby` to be installed on your system;
# I'm pretty sure it will work with versions as old as 1.8.3 up to the newest
# version 3.3.
#
# Install in your terminal by adding `source ruler.sh` (change filename as
# appropriate) to your .bashrc/.bash_profile or .zshrc/.zprofile file.
# Alternately, simply copy the function below in to one of those files.
#
# usage:
# ruler [width, default 80] [units, default 10]
#
# example output:
# $ ruler 40 10
# 0--------|---------|---------|--------40
#
# Updates can be found at:
# https://gist.github.com/xunker/d0474365683e70680796d7cd67006259
ruler() {
/usr/bin/env ruby -e "
# ruler width as first agument, or 80 as default
ruler_w = (ARGV[0] || 80).to_i
# ruler "tick" mark spacing as second agument, or 10 as default
ruler_tick_spacing = (ARGV[1] || 10).to_i
# character to use for a ruler unit
ruler_unit_char = '-'
# character to use for a "tick" mark
ruler_tick_char = '|'
# generate the basic ruler string with tick marks
ruler_str = ruler_w.times.map{|idx|
((idx+1) % ruler_tick_spacing) == 0 ? ruler_tick_char : ruler_unit_char
}.join
# Set beginning of ruler string to be "0"
ruler_str[0] = '0'
# Set ending of ruler string to be ruler length, offset from end
ruler_str[((ruler_str.length-ruler_w.to_s.length))..-1] = ruler_w.to_s
# print the ruler
puts ruler_str
" $*
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment