Last active
February 7, 2023 18:43
-
-
Save tylerhunt/33d431e9e5f9c4856223b6732538121d to your computer and use it in GitHub Desktop.
A simple task status output helper for Ruby.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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