Created
January 13, 2022 14:11
-
-
Save bradland/b60f318ffaed23760941aea49746e16a to your computer and use it in GitHub Desktop.
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
module ShellScriptUtils | |
class CursedReport | |
def initialize(message = nil) | |
@clear_commands = [] | |
@message = message | |
update @message | |
end | |
def update(message) | |
clear @message # clear the current message | |
@message = message # udpate with the new message | |
puts @message # output new message | |
end | |
private | |
def clear(string) | |
return unless string | |
build_clear_command(string) | |
print @clear_commands.join.chomp | |
reset_clear_commands | |
end | |
def build_clear_command(string) | |
newlines = string.scan(/\n/).size + 1 | |
add_command move_beg_line | |
add_command clear_line | |
newlines.times do | |
add_command move_up_line | |
add_command clear_line | |
end | |
end | |
def add_command(command) | |
@clear_commands << command | |
end | |
def reset_clear_commands | |
@clear_commands = [] | |
end | |
def move_beg_line | |
"\r" | |
end | |
def move_up_line | |
"\e[A" | |
end | |
def clear_line | |
"\e[K" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment