Skip to content

Instantly share code, notes, and snippets.

@bradland
Created January 13, 2022 14:11
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 bradland/b60f318ffaed23760941aea49746e16a to your computer and use it in GitHub Desktop.
Save bradland/b60f318ffaed23760941aea49746e16a to your computer and use it in GitHub Desktop.
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