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
-
.backward(n = nil) ⇒ Object
(also: cursor_backward)
Move the cursor backward by n.
-
.clear_char(n = nil) ⇒ Object
Erase n characters from the current cursor position.
-
.clear_line ⇒ Object
Erase the entire current line and return to beginning of the line.
-
.clear_line_after ⇒ Object
Erase from the current position (inclusive) to the end of the line.
-
.clear_line_before ⇒ Object
Erase from the beginning of the line up to and including the current cursor position.
-
.clear_lines(n, direction = :up) ⇒ Object
(also: clear_rows)
Clear a number of lines.
-
.clear_screen ⇒ Object
Clear the screen with the background colour and moves the cursor to home.
-
.clear_screen_down ⇒ Object
Clear screen down from current position.
-
.clear_screen_up ⇒ Object
Clear screen up from current position.
-
.column(n = nil) ⇒ Object
Cursor moves to nth position horizontally in the current line.
-
.current ⇒ Object
Query cursor current position.
-
.down(n = nil) ⇒ Object
(also: cursor_down)
Move the cursor down by n.
-
.forward(n = nil) ⇒ Object
(also: cursor_forward)
Move the cursor forward by n.
-
.hide ⇒ Object
Hide cursor.
-
.invisible(stream = $stdout) ⇒ Object
Switch off cursor for the block.
-
.move(x, y) ⇒ Object
Move cursor relative to its current position.
-
.move_to(row = nil, column = nil) ⇒ Object
Set the cursor absolute position.
-
.next_line ⇒ Object
Move cursor down to beginning of next line.
-
.prev_line ⇒ Object
Move cursor up to beginning of previous line.
-
.restore ⇒ Object
Restore cursor position.
-
.row(n = nil) ⇒ Object
Cursor moves to the nth position vertically in the current column.
-
.save ⇒ Object
Save current position.
-
.scroll_down ⇒ Object
Scroll display down one line.
-
.scroll_up ⇒ Object
Scroll display up one line.
-
.show ⇒ Object
Make cursor visible.
-
.up(n = nil) ⇒ Object
(also: cursor_up)
Move cursor up by n.
Class Method Details
.backward(n = nil) ⇒ Object Also known as: cursor_backward
Move the cursor backward by n
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_line ⇒ Object
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_after ⇒ Object
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_before ⇒ Object
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
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_screen ⇒ Object
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_down ⇒ Object
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_up ⇒ Object
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
111 112 113 |
# File 'lib/tty/cursor.rb', line 111 def column(n = nil) CSI + "#{n || 1}G" end |
.current ⇒ Object
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
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
102 103 104 |
# File 'lib/tty/cursor.rb', line 102 def forward(n = nil) CSI + "#{n || 1}C" end |
.hide ⇒ Object
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
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
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_line ⇒ Object
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_line ⇒ Object
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 |
.restore ⇒ Object
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
119 120 121 |
# File 'lib/tty/cursor.rb', line 119 def row(n = nil) CSI + "#{n || 1}d" end |
.save ⇒ Object
Save current position
39 40 41 |
# File 'lib/tty/cursor.rb', line 39 def save Gem.win_platform? ? CSI + 's' : ESC + '7' end |
.scroll_down ⇒ Object
Scroll display down one line
203 204 205 |
# File 'lib/tty/cursor.rb', line 203 def scroll_down ESC + 'D' end |
.scroll_up ⇒ Object
Scroll display up one line
197 198 199 |
# File 'lib/tty/cursor.rb', line 197 def scroll_up ESC + 'M' end |
.show ⇒ Object
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
78 79 80 |
# File 'lib/tty/cursor.rb', line 78 def up(n = nil) CSI + "#{(n || 1)}A" end |