module TTY::Cursor
Terminal cursor movement ANSI codes
Constants
- CSI
- DEC_RST
- DEC_SET
- DEC_TCEM
- ESC
- VERSION
Public Instance Methods
Move the cursor backward by n @param [Integer] n @api public
# File lib/tty/cursor.rb, line 93 def backward(n = nil) CSI + "#{n || 1}D" end
Erase n characters from the current cursor position @api public
# File lib/tty/cursor.rb, line 136 def clear_char(n = nil) CSI + "#{n}X" end
Erase the entire current line and return to beginning of the line @api public
# File lib/tty/cursor.rb, line 142 def clear_line CSI + '2K' + column(1) end
Erase from the current position (inclusive) to the end of the line @api public
# File lib/tty/cursor.rb, line 156 def clear_line_after CSI + '0K' end
Erase from the beginning of the line up to and including the current cursor position. @api public
# File lib/tty/cursor.rb, line 149 def clear_line_before CSI + '1K' end
Clear a number of lines
@param [Integer] n
the number of lines to clear
@param [Symbol] :direction
the direction to clear, default :up
@api public
# File lib/tty/cursor.rb, line 168 def clear_lines(n, direction = :up) n.times.reduce([]) do |acc, i| dir = direction == :up ? up : down acc << clear_line + ((i == n - 1) ? '' : dir) end.join end
Clear the screen with the background colour and moves the cursor to home @api public
# File lib/tty/cursor.rb, line 190 def clear_screen CSI + '2J' end
Clear screen down from current position @api public
# File lib/tty/cursor.rb, line 178 def clear_screen_down CSI + 'J' end
Clear screen up from current position @api public
# File lib/tty/cursor.rb, line 184 def clear_screen_up CSI + '1J' end
Cursor moves to nth position horizontally in the current line @param [Integer] n
the nth aboslute position in line
@api public
# File lib/tty/cursor.rb, line 110 def column(n = nil) CSI + "#{n || 1}G" end
Query cursor current position @api public
# File lib/tty/cursor.rb, line 50 def current CSI + '6n' end
Move the cursor down by n @param [Integer] n @api public
# File lib/tty/cursor.rb, line 85 def down(n = nil) CSI + "#{(n || 1)}B" end
Move the cursor forward by n @param [Integer] n @api public
# File lib/tty/cursor.rb, line 101 def forward(n = nil) CSI + "#{n || 1}C" end
Hide cursor @api public
# File lib/tty/cursor.rb, line 23 def hide CSI + DEC_TCEM + DEC_RST end
Switch off cursor for the block @api public
# File lib/tty/cursor.rb, line 29 def invisible(stream = $stdout) stream.print(hide) yield ensure stream.print(show) end
Move cursor relative to its current position
@param [Integer] x @param [Integer] y
@api public
# File lib/tty/cursor.rb, line 69 def move(x, y) (x < 0 ? backward(-x) : (x > 0 ? forward(x) : '')) + (y < 0 ? down(-y) : (y > 0 ? up(y) : '')) end
Set the cursor absolute position @param [Integer] row @param [Integer] column @api public
# File lib/tty/cursor.rb, line 58 def move_to(row = nil, column = nil) return CSI + 'H' if row.nil? && column.nil? CSI + "#{column + 1};#{row + 1}H" end
Move cursor down to beginning of next line @api public
# File lib/tty/cursor.rb, line 124 def next_line CSI + 'E' + column(1) end
Move cursor up to beginning of previous line @api public
# File lib/tty/cursor.rb, line 130 def prev_line CSI + 'A' + column(1) end
Restore cursor position @api public
# File lib/tty/cursor.rb, line 44 def restore Gem.win_platform? ? CSI + 'u' : ESC + '8' end
Cursor moves to the nth position vertically in the current column @param [Integer] n
the nth absolute position in column
@api public
# File lib/tty/cursor.rb, line 118 def row(n = nil) CSI + "#{n || 1}d" end
Save current position @api public
# File lib/tty/cursor.rb, line 38 def save Gem.win_platform? ? CSI + 's' : ESC + '7' end
Scroll display down one line @api public
# File lib/tty/cursor.rb, line 202 def scroll_down ESC + 'D' end
Scroll display up one line @api public
# File lib/tty/cursor.rb, line 196 def scroll_up ESC + 'M' end
Make cursor visible @api public
# File lib/tty/cursor.rb, line 17 def show CSI + DEC_TCEM + DEC_SET end
Move cursor up by n @param [Integer] n @api public
# File lib/tty/cursor.rb, line 77 def up(n = nil) CSI + "#{(n || 1)}A" end