Class: IO
- Inherits:
-
Object
- Object
- IO
- Defined in:
- lib/term/bell.rb,
lib/term/input.rb,
lib/term/table.rb,
lib/term/cursor.rb,
lib/term/window.rb,
lib/term/password.rb
Overview
Defined Under Namespace
Classes: Table
Constant Summary collapse
- KEY =
keybord & mouse hash.
{ "\e[A" => :up, "\e[B" => :down, "\e[C" => :right, "\e[D" => :left, "\t" => :tab, "\r" => :enter, ' ' => :space, "\e\[M" => :mouse }.freeze
Class Method Summary collapse
-
.backward(num = 1) ⇒ Term
moves the cursor backward by num columns.
-
.bell ⇒ Object
ring a bell to a terminal.
-
.clear ⇒ Term
clear screen.
-
.clear_backward ⇒ Term
clear line from beginning to current cursor position.
-
.clear_down ⇒ Term
erase the screen from the current line down to the top of the screen.
-
.clear_forward ⇒ Term
clear line from current cursor position to end of line.
-
.clear_line ⇒ Term
clear whole line.
-
.clear_up ⇒ Term
erase the screen from the current line up to the top of the screen.
-
.down(num = 1) ⇒ Term
moves the cursor down by num rows.
-
.forward(num = 1) ⇒ Term
moves the cursor forward by num columns.
-
.height ⇒ Integer
get window height.
-
.hide ⇒ Term
make cursor invisible.
-
.home ⇒ self
move cursor to home position (0-0).
-
.icon_name=(text = '') ⇒ Term
set icon name.
-
.input ⇒ Hash
get keybord & mouse key.
-
.next_line(num = 1) ⇒ Term
cursor next line num times.
-
.parse_key_str(str) ⇒ Object
parse key str.
-
.password ⇒ String
get password.
-
.pos ⇒ Hash
query cursor position.
-
.prev_line(num = 1) ⇒ Term
cursor preceding line num times.
-
.restore ⇒ Term
restore saved cursor position & attrs.
-
.save ⇒ Term
save current cursor position & attrs.
-
.show ⇒ Term
make cursor visible.
-
.table ⇒ Table
get table component.
-
.title=(text = '') ⇒ Term
set icon name and window title.
-
.to(pos) ⇒ Term
home-positioning to x and y coordinates.
-
.up(num = 1) ⇒ Term
moves the cursor up by num rows.
-
.width ⇒ Integer
get window width.
-
.window_title=(text = '') ⇒ Term
set window title.
Class Method Details
.backward(num = 1) ⇒ Term
moves the cursor backward by num columns.
101 102 103 104 |
# File 'lib/term/cursor.rb', line 101 def self.backward(num = 1) print "\e[#{num}D" # CUB self end |
.bell ⇒ Object
ring a bell to a terminal.
10 11 12 13 |
# File 'lib/term/bell.rb', line 10 def self.bell print "\a" self end |
.clear ⇒ Term
cursor position unchanged
clear screen.
128 129 130 131 |
# File 'lib/term/cursor.rb', line 128 def self.clear print "\e[2J" self end |
.clear_backward ⇒ Term
cursor position unchanged
clear line from beginning to current cursor position.
146 147 148 149 |
# File 'lib/term/cursor.rb', line 146 def self.clear_backward print "\e[1K" self end |
.clear_down ⇒ Term
cursor position unchanged
erase the screen from the current line down to the top of the screen.
173 174 175 176 |
# File 'lib/term/cursor.rb', line 173 def self.clear_down print "\e[J" self end |
.clear_forward ⇒ Term
cursor position unchanged
clear line from current cursor position to end of line.
137 138 139 140 |
# File 'lib/term/cursor.rb', line 137 def self.clear_forward print "\e[K" self end |
.clear_line ⇒ Term
cursor position unchanged
clear whole line.
155 156 157 158 |
# File 'lib/term/cursor.rb', line 155 def self.clear_line print "\e[2K" self end |
.clear_up ⇒ Term
cursor position unchanged
erase the screen from the current line up to the top of the screen.
164 165 166 167 |
# File 'lib/term/cursor.rb', line 164 def self.clear_up print "\e[1J" self end |
.down(num = 1) ⇒ Term
moves the cursor down by num rows.
83 84 85 86 |
# File 'lib/term/cursor.rb', line 83 def self.down(num = 1) print "\e[#{num}B" # CUD self end |
.forward(num = 1) ⇒ Term
moves the cursor forward by num columns.
92 93 94 95 |
# File 'lib/term/cursor.rb', line 92 def self.forward(num = 1) print "\e[#{num}C" # CUF self end |
.height ⇒ Integer
get window height.
47 48 49 |
# File 'lib/term/window.rb', line 47 def self.height $stdout.winsize[0] end |
.hide ⇒ Term
make cursor invisible.
34 35 36 37 |
# File 'lib/term/cursor.rb', line 34 def self.hide print "\e[?25l" self end |
.home ⇒ self
move cursor to home position (0-0).
10 11 12 13 |
# File 'lib/term/cursor.rb', line 10 def self.home print "\e[H" self end |
.icon_name=(text = '') ⇒ Term
set icon name
22 23 24 25 |
# File 'lib/term/window.rb', line 22 def self.icon_name=(text = '') print "\e]1;#{text}\a" self end |
.input ⇒ Hash
get keybord & mouse key.
10 11 12 13 14 15 16 17 |
# File 'lib/term/input.rb', line 10 def self.input $stdin.raw do print "\e[?1000h" # DEC Private Mode Set str = $stdin.readpartial(4096).force_encoding('utf-8') print "\e[?1000l" # DEC Private Mode Reset parse_key_str str end end |
.next_line(num = 1) ⇒ Term
cursor next line num times.
110 111 112 113 |
# File 'lib/term/cursor.rb', line 110 def self.next_line(num = 1) print "\e[#{num}E" self end |
.parse_key_str(str) ⇒ Object
parse key str
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/term/input.rb', line 32 def self.parse_key_str(str) key = KEY[str[0, 3]] || str.to_sym if key == :mouse type, mouse_x, mouse_y = str[3, 3].unpack('CCC') type = i(left_pressed scroll_down right_pressed released)[type & 0b11] { key: key, type: type, pos: { x: mouse_x - 33, y: mouse_y - 33 } } else { key: key } end end |
.password ⇒ String
get password.
10 11 12 13 14 15 16 |
# File 'lib/term/password.rb', line 10 def self.password @begin_password_pos = IO.hide.pos hide_password_text process_password_input IO.show @password_text || '' end |
.pos ⇒ Hash
query cursor position.
50 51 52 53 54 55 56 |
# File 'lib/term/cursor.rb', line 50 def self.pos $stdin.raw do print "\e[6n" y_axis, x_axis = $stdin.gets('R')[2..-2].split(';') { x: x_axis.to_i - 1, y: y_axis.to_i - 1 } end end |
.prev_line(num = 1) ⇒ Term
cursor preceding line num times.
119 120 121 122 |
# File 'lib/term/cursor.rb', line 119 def self.prev_line(num = 1) print "\e[#{num}F" self end |
.restore ⇒ Term
restore saved cursor position & attrs.
26 27 28 29 |
# File 'lib/term/cursor.rb', line 26 def self.restore print "\e8" self end |
.save ⇒ Term
save current cursor position & attrs.
18 19 20 21 |
# File 'lib/term/cursor.rb', line 18 def self.save print "\e7" self end |
.show ⇒ Term
make cursor visible.
42 43 44 45 |
# File 'lib/term/cursor.rb', line 42 def self.show print "\e[?25h" self end |
.table ⇒ Table
get table component.
73 74 75 |
# File 'lib/term/table.rb', line 73 def self.table IO::Table.new end |
.title=(text = '') ⇒ Term
set icon name and window title
12 13 14 15 |
# File 'lib/term/window.rb', line 12 def self.title=(text = '') print "\e]0;#{text}\a" self end |
.to(pos) ⇒ Term
home-positioning to x and y coordinates.
63 64 65 66 67 68 |
# File 'lib/term/cursor.rb', line 63 def self.to(pos) x_axis = pos[:x] || 0 y_axis = pos[:y] || 0 print "\e[#{y_axis + 1};#{x_axis + 1}H" # ANSI uses 1-1 as home self end |
.up(num = 1) ⇒ Term
moves the cursor up by num rows.
74 75 76 77 |
# File 'lib/term/cursor.rb', line 74 def self.up(num = 1) print "\e[#{num}A" # CUU self end |
.width ⇒ Integer
get window width.
40 41 42 |
# File 'lib/term/window.rb', line 40 def self.width $stdout.winsize[1] end |
.window_title=(text = '') ⇒ Term
set window title
32 33 34 35 |
# File 'lib/term/window.rb', line 32 def self.window_title=(text = '') print "\e]2;#{text}\a" self end |