module SpinningCursor::ConsoleHelpers

Constants

CLR

DOS Contains a string to clear the line in the shell

ESC_CURS_INVIS

ANSI escape sequence for hiding terminal cursor

ESC_CURS_VIS

ANSI escape sequence for showing terminal cursor

ESC_R_AND_CLR

ANSI escape sequence for clearing line in terminal

ESC_UP_A_LINE

ANSI escape sequence for going up a line in terminal

Public Instance Methods

capture_console() click to toggle source

Sets `$stdout` global variable to a `StringIO` object to buffer output

# File lib/spinning_cursor/console_helpers.rb, line 77
def capture_console
  $stdout = StringIO.new
end
captured_console_empty?() click to toggle source

Returns true if the output buffer is currently empty

# File lib/spinning_cursor/console_helpers.rb, line 98
def captured_console_empty?
  console_captured? and $stdout.string.empty?
end
console_captured?() click to toggle source

Returns true if `$stdout` is a `StringIO` object

# File lib/spinning_cursor/console_helpers.rb, line 91
def console_captured?
  $stdout.is_a?(StringIO)
end
console_columns() click to toggle source

Returns the width of the terminal window

# File lib/spinning_cursor/console_helpers.rb, line 119
def console_columns
  HighLine::SystemExtensions.terminal_size.first
end
hide_cursor() click to toggle source

Hides the terminal cursor

# File lib/spinning_cursor/console_helpers.rb, line 105
def hide_cursor
  $console.print ESC_CURS_INVIS
end
release_console() click to toggle source

Resets `$stdout` global variable to `STDOUT`

# File lib/spinning_cursor/console_helpers.rb, line 84
def release_console
  $stdout = $console
end
reset_line(text = "") click to toggle source

Manages line reset in the console

# File lib/spinning_cursor/console_helpers.rb, line 32
def reset_line(text = "")
  # Initialise ANSI escape string
  escape = ""

  # Get terminal window width
  cols = console_columns

  # The number of lines the previous message spanned
  lines = @@prev / cols

  # If cols == 80 and @@prev == 80, @@prev / cols == 1 but we don't want to
  # go up an extra line since it fits exactly
  lines -= 1 if @@prev % cols == 0

  # Clear and go up a line
  lines.times { escape += "#{ESC_R_AND_CLR}#{ESC_UP_A_LINE}" }

  # Clear the line that is to be printed on
  escape += "#{ESC_R_AND_CLR}"

  # Console is clear, we can print!
  $console.print "#{escape}#{text}"

  # Store the current message so we know how many lines it spans
  @@prev = text.to_s.length
end
restore_stdout_sync_status() click to toggle source

Restores the previously stored `STDOUT.sync` value

# File lib/spinning_cursor/console_helpers.rb, line 70
def restore_stdout_sync_status
  STDOUT.sync = @stdout_sync_saved_status
end
save_stdout_sync_status() click to toggle source

Stores current `STDOUT.sync` value and sets it to true

# File lib/spinning_cursor/console_helpers.rb, line 62
def save_stdout_sync_status
  @stdout_sync_saved_status = STDOUT.sync
  STDOUT.sync = true
end
show_cursor() click to toggle source

Shows the terminal cursor

# File lib/spinning_cursor/console_helpers.rb, line 112
def show_cursor
  $console.print ESC_CURS_VIS
end