Skip to content

Instantly share code, notes, and snippets.

@tylerhunt
Last active February 7, 2023 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tylerhunt/33d431e9e5f9c4856223b6732538121d to your computer and use it in GitHub Desktop.
Save tylerhunt/33d431e9e5f9c4856223b6732538121d to your computer and use it in GitHub Desktop.
A simple task status output helper for Ruby.
class StatusLine
MAX_LENGTH = 72
FORMAT = "[ ] %.#{MAX_LENGTH}s"
BUSY = 'BUSY'
DONE = 'DONE'
FAIL = 'FAIL'
COLOR="\033[%s;%sm"
COLORS = {
black: 30,
red: 31,
green: 32,
yellow: 33,
blue: 34,
magenta: 35,
cyan: 36,
white: 37,
}
COLORS.update Hash[COLORS.collect { |name, code|
[:"bright_#{name}", code + 60]
}]
EFFECTS = {
none: 0,
bold: 1,
}
def initialize(text, busy: true, color: :white, effect: :none)
color color
puts FORMAT % text + (text.length > MAX_LENGTH ? '…' : '')
busy! if busy
end
def busy!(color: :yellow, effect: :none)
move_cursor
color color, effect
puts BUSY
reset
end
def done!(color: :green, effect: :none)
move_cursor
color color, effect
puts DONE
reset
end
def fail!(color: :red, effect: :none)
move_cursor
color color, effect
puts FAIL
reset
end
private
def color(color, effect=:none)
print COLOR % [EFFECTS.fetch(effect), COLORS.fetch(color)]
end
def reset
color :white, :none
end
def move_cursor
print "\033[1A\033[1C"
end
end
require 'status_line'
status_line = Podlast::StatusLine.new('Status')
# => [BUSY] Status
sleep 1
status_line.done!
# => [DONE] Status
sleep 1
status_line.fail!
# => [FAIL] Status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment