Module: SpinningCursor::ConsoleHelpers
- Included in:
- Cursor
- Defined in:
- lib/spinning_cursor/console_helpers.rb
Constant Summary collapse
- CLR =
Unix Contains a string to clear the line in the shell
"\e[0K"
- ESC_CURS_INVIS =
ANSI escape sequence for hiding terminal cursor
"\e[?25l"
- ESC_CURS_VIS =
ANSI escape sequence for showing terminal cursor
"\e[?25h"
- ESC_R_AND_CLR =
ANSI escape sequence for clearing line in terminal
"\r#{CLR}"
- ESC_UP_A_LINE =
ANSI escape sequence for going up a line in terminal
"\e[1A"
- @@prev =
0
Instance Method Summary collapse
-
#capture_console ⇒ Object
Sets
$stdout
global variable to aStringIO
object to buffer output. -
#captured_console_empty? ⇒ Boolean
Returns true if the output buffer is currently empty.
-
#console_captured? ⇒ Boolean
Returns true if
$stdout
is aStringIO
object. -
#console_columns ⇒ Object
Returns the width of the terminal window.
-
#hide_cursor ⇒ Object
Hides the terminal cursor.
-
#release_console ⇒ Object
Resets
$stdout
global variable toSTDOUT
. -
#reset_line(text = "") ⇒ Object
Manages line reset in the console.
-
#restore_stdout_sync_status ⇒ Object
Restores the previously stored
STDOUT.sync
value. -
#save_stdout_sync_status ⇒ Object
Stores current
STDOUT.sync
value and sets it to true. -
#show_cursor ⇒ Object
Shows the terminal cursor.
Instance Method Details
#capture_console ⇒ Object
Sets $stdout
global variable to a StringIO
object to buffer output
77 78 79 |
# File 'lib/spinning_cursor/console_helpers.rb', line 77 def capture_console $stdout = StringIO.new end |
#captured_console_empty? ⇒ Boolean
Returns true if the output buffer is currently empty
98 99 100 |
# File 'lib/spinning_cursor/console_helpers.rb', line 98 def captured_console_empty? console_captured? and $stdout.string.empty? end |
#console_captured? ⇒ Boolean
Returns true if $stdout
is a StringIO
object
91 92 93 |
# File 'lib/spinning_cursor/console_helpers.rb', line 91 def console_captured? $stdout.is_a?(StringIO) end |
#console_columns ⇒ Object
Returns the width of the terminal window
119 120 121 |
# File 'lib/spinning_cursor/console_helpers.rb', line 119 def console_columns HighLine::SystemExtensions.terminal_size.first end |
#hide_cursor ⇒ Object
Hides the terminal cursor
105 106 107 |
# File 'lib/spinning_cursor/console_helpers.rb', line 105 def hide_cursor $console.print ESC_CURS_INVIS end |
#release_console ⇒ Object
Resets $stdout
global variable to STDOUT
84 85 86 |
# File 'lib/spinning_cursor/console_helpers.rb', line 84 def release_console $stdout = $console end |
#reset_line(text = "") ⇒ Object
Manages line reset in the console
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# 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 ⇒ Object
Restores the previously stored STDOUT.sync
value
70 71 72 |
# 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 ⇒ Object
Stores current STDOUT.sync
value and sets it to true
62 63 64 65 |
# 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 ⇒ Object
Shows the terminal cursor
112 113 114 |
# File 'lib/spinning_cursor/console_helpers.rb', line 112 def show_cursor $console.print ESC_CURS_VIS end |