Module: TTY::Cursor

Defined in:
lib/tty/cursor.rb,
lib/tty/cursor/version.rb

Overview

Terminal cursor movement ANSI codes

Constant Summary collapse

ESC =
"\e".freeze
CSI =
"\e[".freeze
DEC_RST =
'l'.freeze
DEC_SET =
'h'.freeze
DEC_TCEM =
'?25'.freeze
VERSION =
"0.7.1"

Class Method Summary collapse

Class Method Details

.backward(n = nil) ⇒ Object Also known as: cursor_backward

Move the cursor backward by n

Parameters:

  • n (Integer) (defaults to: nil)


94
95
96
# File 'lib/tty/cursor.rb', line 94

def backward(n = nil)
  CSI + "#{n || 1}D"
end

.clear_char(n = nil) ⇒ Object

Erase n characters from the current cursor position



137
138
139
# File 'lib/tty/cursor.rb', line 137

def clear_char(n = nil)
  CSI + "#{n}X"
end

.clear_lineObject

Erase the entire current line and return to beginning of the line



143
144
145
# File 'lib/tty/cursor.rb', line 143

def clear_line
  CSI + '2K' + column(1)
end

.clear_line_afterObject

Erase from the current position (inclusive) to the end of the line



157
158
159
# File 'lib/tty/cursor.rb', line 157

def clear_line_after
  CSI + '0K'
end

.clear_line_beforeObject

Erase from the beginning of the line up to and including the current cursor position.



150
151
152
# File 'lib/tty/cursor.rb', line 150

def clear_line_before
  CSI + '1K'
end

.clear_lines(n, direction = :up) ⇒ Object Also known as: clear_rows

Clear a number of lines

Parameters:

  • n (Integer)

    the number of lines to clear

  • :direction (Symbol)

    the direction to clear, default :up



169
170
171
172
173
174
# File 'lib/tty/cursor.rb', line 169

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_screenObject

Clear the screen with the background colour and moves the cursor to home



191
192
193
# File 'lib/tty/cursor.rb', line 191

def clear_screen
  CSI + '2J'
end

.clear_screen_downObject

Clear screen down from current position



179
180
181
# File 'lib/tty/cursor.rb', line 179

def clear_screen_down
  CSI + 'J'
end

.clear_screen_upObject

Clear screen up from current position



185
186
187
# File 'lib/tty/cursor.rb', line 185

def clear_screen_up
  CSI + '1J'
end

.column(n = nil) ⇒ Object

Cursor moves to nth position horizontally in the current line

Parameters:

  • n (Integer) (defaults to: nil)

    the nth aboslute position in line



111
112
113
# File 'lib/tty/cursor.rb', line 111

def column(n = nil)
  CSI + "#{n || 1}G"
end

.currentObject

Query cursor current position



51
52
53
# File 'lib/tty/cursor.rb', line 51

def current
  CSI + '6n'
end

.down(n = nil) ⇒ Object Also known as: cursor_down

Move the cursor down by n

Parameters:

  • n (Integer) (defaults to: nil)


86
87
88
# File 'lib/tty/cursor.rb', line 86

def down(n = nil)
  CSI + "#{(n || 1)}B"
end

.forward(n = nil) ⇒ Object Also known as: cursor_forward

Move the cursor forward by n

Parameters:

  • n (Integer) (defaults to: nil)


102
103
104
# File 'lib/tty/cursor.rb', line 102

def forward(n = nil)
  CSI + "#{n || 1}C"
end

.hideObject

Hide cursor



24
25
26
# File 'lib/tty/cursor.rb', line 24

def hide
  CSI + DEC_TCEM + DEC_RST
end

.invisible(stream = $stdout) ⇒ Object

Switch off cursor for the block



30
31
32
33
34
35
# File 'lib/tty/cursor.rb', line 30

def invisible(stream = $stdout)
  stream.print(hide)
  yield
ensure
  stream.print(show)
end

.move(x, y) ⇒ Object

Move cursor relative to its current position

Parameters:

  • x (Integer)
  • y (Integer)


70
71
72
73
# File 'lib/tty/cursor.rb', line 70

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) ⇒ Object

Set the cursor absolute position

Parameters:

  • row (Integer) (defaults to: nil)
  • column (Integer) (defaults to: nil)


59
60
61
62
# File 'lib/tty/cursor.rb', line 59

def move_to(row = nil, column = nil)
  return CSI + 'H' if row.nil? && column.nil?
  CSI + "#{column + 1};#{row + 1}H"
end

.next_lineObject

Move cursor down to beginning of next line



125
126
127
# File 'lib/tty/cursor.rb', line 125

def next_line
  CSI + 'E' + column(1)
end

.prev_lineObject

Move cursor up to beginning of previous line



131
132
133
# File 'lib/tty/cursor.rb', line 131

def prev_line
  CSI + 'A' + column(1)
end

.restoreObject

Restore cursor position



45
46
47
# File 'lib/tty/cursor.rb', line 45

def restore
  Gem.win_platform? ? CSI + 'u' : ESC + '8'
end

.row(n = nil) ⇒ Object

Cursor moves to the nth position vertically in the current column

Parameters:

  • n (Integer) (defaults to: nil)

    the nth absolute position in column



119
120
121
# File 'lib/tty/cursor.rb', line 119

def row(n = nil)
  CSI + "#{n || 1}d"
end

.saveObject

Save current position



39
40
41
# File 'lib/tty/cursor.rb', line 39

def save
  Gem.win_platform? ? CSI + 's' : ESC + '7'
end

.scroll_downObject

Scroll display down one line



203
204
205
# File 'lib/tty/cursor.rb', line 203

def scroll_down
  ESC + 'D'
end

.scroll_upObject

Scroll display up one line



197
198
199
# File 'lib/tty/cursor.rb', line 197

def scroll_up
  ESC + 'M'
end

.showObject

Make cursor visible



18
19
20
# File 'lib/tty/cursor.rb', line 18

def show
  CSI + DEC_TCEM + DEC_SET
end

.up(n = nil) ⇒ Object Also known as: cursor_up

Move cursor up by n

Parameters:

  • n (Integer) (defaults to: nil)


78
79
80
# File 'lib/tty/cursor.rb', line 78

def up(n = nil)
  CSI + "#{(n || 1)}A"
end