class Warg::Console

Attributes

hostname_width[RW]

Public Class Methods

SGR(text) click to toggle source
# File lib/warg.rb, line 71
def self.SGR(text)
  SelectGraphicRendition::Renderer.new(text)
end
new() click to toggle source
# File lib/warg.rb, line 75
def initialize
  @io = $stderr
  @history = History.new
  @cursor_position = CursorPosition.new
  @io_mutex = Mutex.new
  @history_mutex = Mutex.new
end

Public Instance Methods

append_to_history(content) click to toggle source
# File lib/warg.rb, line 124
def append_to_history(content)
  @history_mutex.synchronize do
    @history.append(content, at: @cursor_position)
    @cursor_position.adjust_to(content)
  end
end
print(text_or_content = nil) click to toggle source
print_content(text_or_content) click to toggle source
puts(text_or_content = nil) click to toggle source
# File lib/warg.rb, line 93
def puts(text_or_content = nil)
  content = print_content text_or_content

  unless text_or_content.to_s.end_with?("\n")
    print_content "\n"
  end

  content
end
redirecting_stdout_and_stderr() { || ... } click to toggle source
# File lib/warg.rb, line 83
def redirecting_stdout_and_stderr
  $stdout = IOProxy.new($stdout, self)
  $stderr = IOProxy.new($stderr, self)

  yield
ensure
  $stdout = $stdout.__getobj__
  $stderr = $stderr.__getobj__
end
reprint_content(content) click to toggle source

For CSI sequences, see: en.wikipedia.org/wiki/ANSI_escape_code#Terminal_output_sequences

# File lib/warg.rb, line 133
def reprint_content(content)
  @io_mutex.lock

  history_entry = @history.find_entry_for(content)

  rows_from_cursor_row_to_content_start = @cursor_position.row - history_entry.row_number

  # starting from the current line, clear the line and move up a line
  #
  # this will bring us to the line the content we're reprinting, clearing all lines beneath
  # it
  rows_from_cursor_row_to_content_start.times do
    # clear the current line
    @io.print "\e[2K"

    # move up a line
    @io.print "\e[1A"
  end

  # go to the column of the content
  @io.print "\e[#{history_entry.column_number}G"

  # clear from the starting column of the content to the end of the line
  @io.print "\e[0K"

  # re-print the content from its original location
  @io.print content.to_s

  # initialize how much we'll be adjusting the column number by
  column_adjustment = history_entry.end_column

  current_entry = history_entry

  until current_entry.next_entry.nil?
    next_entry = current_entry.next_entry

    # we only update the column of subsequent entries if they were on the same line as the
    # entry being reprinted.
    if next_entry.row_number == history_entry.end_row
      next_entry.column_number = column_adjustment
      column_adjustment += next_entry.last_line_length
    end

    # update the next entry's row number by how many rows the updated entry grew or shrank
    next_entry.row_number += history_entry.newline_count_diff

    # print the content
    @io.print next_entry.to_s

    # get the next entry to repeat
    current_entry = next_entry
  end

  # Update the cursor position by how many row's the new content has changed and the new
  # end column of the last entry in the history
  @cursor_position.column = current_entry.end_column
  @cursor_position.row += history_entry.newline_count_diff

  # reset `newline_count` and `last_line_length` to what they now are in `@content`
  history_entry.sync!

  @io_mutex.unlock
end