module TTY::Cursor

Terminal cursor movement ANSI codes

Constants

CSI
DEC_RST
DEC_SET
DEC_TCEM
ESC
VERSION

Public Instance Methods

backward(n = nil) click to toggle source

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
Also aliased as: cursor_backward
clear_char(n = nil) click to toggle source

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
clear_line() click to toggle source

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
clear_line_after() click to toggle source

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
clear_line_before() click to toggle source

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_lines(n, direction = :up) click to toggle source

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
Also aliased as: clear_rows
clear_rows(n, direction = :up)
Alias for: clear_lines
clear_screen() click to toggle source

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() click to toggle source

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() click to toggle source

Clear screen up from current position @api public

# File lib/tty/cursor.rb, line 184
def clear_screen_up
  CSI + '1J'
end
column(n = nil) click to toggle source

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
current() click to toggle source

Query cursor current position @api public

# File lib/tty/cursor.rb, line 50
def current
  CSI + '6n'
end
cursor_backward(n = nil)
Alias for: backward
cursor_down(n = nil)
Alias for: down
cursor_forward(n = nil)
Alias for: forward
cursor_up(n = nil)
Alias for: up
down(n = nil) click to toggle source

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
Also aliased as: cursor_down
forward(n = nil) click to toggle source

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
Also aliased as: cursor_forward
hide() click to toggle source

Hide cursor @api public

# File lib/tty/cursor.rb, line 23
def hide
  CSI + DEC_TCEM + DEC_RST
end
invisible(stream = $stdout) { || ... } click to toggle source

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(x, y) click to toggle source

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
move_to(row = nil, column = nil) click to toggle source

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
next_line() click to toggle source

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
prev_line() click to toggle source

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() click to toggle source

Restore cursor position @api public

# File lib/tty/cursor.rb, line 44
def restore
  Gem.win_platform? ? CSI + 'u' : ESC + '8'
end
row(n = nil) click to toggle source

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() click to toggle source

Save current position @api public

# File lib/tty/cursor.rb, line 38
def save
  Gem.win_platform? ? CSI + 's' : ESC + '7'
end
scroll_down() click to toggle source

Scroll display down one line @api public

# File lib/tty/cursor.rb, line 202
def scroll_down
  ESC + 'D'
end
scroll_up() click to toggle source

Scroll display up one line @api public

# File lib/tty/cursor.rb, line 196
def scroll_up
  ESC + 'M'
end
show() click to toggle source

Make cursor visible @api public

# File lib/tty/cursor.rb, line 17
def show
  CSI + DEC_TCEM + DEC_SET
end
up(n = nil) click to toggle source

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
Also aliased as: cursor_up