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 92 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 135 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 141 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 155 def clear_line_after CSI + '1K' end
Erase from the beginning of the line up to and including the current cursor position. @api public
# File lib/tty/cursor.rb, line 148 def clear_line_before CSI + '0K' 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 167 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 189 def clear_screen CSI + '2J' end
Clear screen down from current position @api public
# File lib/tty/cursor.rb, line 177 def clear_screen_down CSI + 'J' end
Clear screen up from current position @api public
# File lib/tty/cursor.rb, line 183 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 109 def column(n = nil) CSI + "#{n || 1}G" end
Query cursor current position @api public
# File lib/tty/cursor.rb, line 49 def current CSI + '6n' end
Move the cursor down by n @param [Integer] n @api public
# File lib/tty/cursor.rb, line 84 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 100 def forward(n = nil) CSI + "#{n || 1}C" end
Hide cursor @api public
# File lib/tty/cursor.rb, line 22 def hide CSI + DEC_TCEM + DEC_RST end
Switch off cursor for the block @api public
# File lib/tty/cursor.rb, line 28 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 68 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 57 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 123 def next_line CSI + 'E' + column(1) end
Move cursor up to beginning of previous line @api public
# File lib/tty/cursor.rb, line 129 def prev_line CSI + 'A' + column(1) end
Restore cursor position @api public
# File lib/tty/cursor.rb, line 43 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 117 def row(n = nil) CSI + "#{n || 1}d" end
Save current position @api public
# File lib/tty/cursor.rb, line 37 def save Gem.win_platform? ? CSI + 's' : ESC + '7' end
Make cursor visible @api public
# File lib/tty/cursor.rb, line 16 def show CSI + DEC_TCEM + DEC_SET end
Move cursor up by n @param [Integer] n @api public
# File lib/tty/cursor.rb, line 76 def up(n = nil) CSI + "#{(n || 1)}A" end