class TrickBag::Io::TextModeStatusUpdater

Updates the terminal line with text, erasing the original content and displaying at the same place. Uses ANSI escape sequences for cursor positioning and clearing (see www.oldlinux.org/Linux.old/Ref-docs/ASCII/ANSI%20Escape%20Sequences.htm).

Example:

updater = TrickBag::Io::TextModeStatusUpdater.new(->{ Time.now }) 5.times { updater.print; sleep(1) }

Public Class Methods

new(text_generator, outstream = $stdout, force_output_non_tty = false) click to toggle source
# File lib/trick_bag/io/text_mode_status_updater.rb, line 15
def initialize(text_generator, outstream = $stdout, force_output_non_tty = false)
  @text_generator = text_generator
  @outstream = outstream
  @force_output_non_tty = force_output_non_tty
  @first_time = true
end

Public Instance Methods

print(*args) click to toggle source

Causes the text generator lambda to be called, calls to_s on its result, and outputs the resulting text to the output stream, moving the cursor left to the beginning of the previous call's output if not the first call to print.

Since this method uses ASCII escape sequences that would look messy in a file, this method will silently return if the output stream is not a TTY, unless @force_output_non_tty has been set to true.

@param args Optional arguments to be passed to the text generator

Private Instance Methods

clear_to_end_of_line_text() click to toggle source

The following methods are for cursor placement and text clearing:

# File lib/trick_bag/io/text_mode_status_updater.rb, line 51
def clear_to_end_of_line_text
  "\x1b[2K"
end
go_to_start_of_line_text() click to toggle source
# File lib/trick_bag/io/text_mode_status_updater.rb, line 59
def go_to_start_of_line_text
  "\x1b0`"
end
insert_blank_chars_text(num_chars) click to toggle source
# File lib/trick_bag/io/text_mode_status_updater.rb, line 71
def insert_blank_chars_text(num_chars)
  "\x1b[#{num_chars}@"
end
move_cursor_left_text(num_chars) click to toggle source
# File lib/trick_bag/io/text_mode_status_updater.rb, line 63
def move_cursor_left_text(num_chars)
  "\x1b[#{num_chars}D"
end
restore_cursor_position_text() click to toggle source
# File lib/trick_bag/io/text_mode_status_updater.rb, line 67
def restore_cursor_position_text
  "\x1b[u"
end
save_cursor_position_text() click to toggle source
# File lib/trick_bag/io/text_mode_status_updater.rb, line 55
def save_cursor_position_text
  "\x1b[s"
end