class IO
@author {cuihaiqin@gmail.com cuihq}
@author {cuihaiqin@gmail.com cuihq}
@author {cuihaiqin@gmail.com cuihq}
@author {cuihaiqin@gmail.com cuihq}
@author {cuihaiqin@gmail.com cuihq}
@author {cuihaiqin@gmail.com cuihq}
Constants
- KEY
keybord & mouse hash.
Public Class Methods
moves the cursor backward by num columns.
@param num [Integer] the columns, the default num is 1. @return [Term] Term
self.
# File lib/term/cursor.rb, line 101 def self.backward(num = 1) print "\e[#{num}D" # CUB self end
ring a bell to a terminal.
@example
IO.bell
@return self
# File lib/term/bell.rb, line 10 def self.bell print "\a" self end
clear screen.
@note cursor position unchanged @return [Term] Term
self.
# File lib/term/cursor.rb, line 128 def self.clear print "\e[2J" self end
clear line from beginning to current cursor position.
@note cursor position unchanged @return [Term] Term
self.
# File lib/term/cursor.rb, line 146 def self.clear_backward print "\e[1K" self end
erase the screen from the current line down to the top of the screen.
@note cursor position unchanged @return [Term] Term
self.
# File lib/term/cursor.rb, line 173 def self.clear_down print "\e[J" self end
clear line from current cursor position to end of line.
@note cursor position unchanged @return [Term] Term
self.
# File lib/term/cursor.rb, line 137 def self.clear_forward print "\e[K" self end
clear whole line.
@note cursor position unchanged @return [Term] Term
self.
# File lib/term/cursor.rb, line 155 def self.clear_line print "\e[2K" self end
erase the screen from the current line up to the top of the screen.
@note cursor position unchanged @return [Term] Term
self.
# File lib/term/cursor.rb, line 164 def self.clear_up print "\e[1J" self end
moves the cursor down by num rows.
@param num [Integer] the rows, the default num is 1. @return [Term] Term
self.
# File lib/term/cursor.rb, line 83 def self.down(num = 1) print "\e[#{num}B" # CUD self end
moves the cursor forward by num columns.
@param num [Integer] the columns, the default num is 1. @return [Term] Term
self.
# File lib/term/cursor.rb, line 92 def self.forward(num = 1) print "\e[#{num}C" # CUF self end
get window height.
@return [Integer] the window height
# File lib/term/window.rb, line 47 def self.height $stdout.winsize[0] end
make cursor invisible.
@return [Term] Term
self.
# File lib/term/cursor.rb, line 34 def self.hide print "\e[?25l" self end
move cursor to home position (0-0).
@return [self] Term
self.
# File lib/term/cursor.rb, line 10 def self.home print "\e[H" self end
set icon name
@example set the terminal icon name
IO.icon_name = 'icon title'
@return [Term] Term
self.
# File lib/term/window.rb, line 22 def self.icon_name=(text = '') print "\e]1;#{text}\a" self end
get keybord & mouse key.
@return [Hash] keybord & mouse key
# 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
cursor next line num times.
@param num [Integer] the times, the default num is 1. @return [Term] Term
self.
# File lib/term/cursor.rb, line 110 def self.next_line(num = 1) print "\e[#{num}E" self end
parse key str
# 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
get password.
@return [String] password
# 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
query cursor position.
@return [Hash] pos cursor position.
# 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
cursor preceding line num times.
@param num [Integer] the times, the default num is 1. @return [Term] Term
self.
# File lib/term/cursor.rb, line 119 def self.prev_line(num = 1) print "\e[#{num}F" self end
restore saved cursor position & attrs.
@return [Term] Term
self.
# File lib/term/cursor.rb, line 26 def self.restore print "\e8" self end
save current cursor position & attrs.
@return [Term] Term
self.
# File lib/term/cursor.rb, line 18 def self.save print "\e7" self end
make cursor visible.
@return [Term] Term
self.
# File lib/term/cursor.rb, line 42 def self.show print "\e[?25h" self end
get table component.
@return [Table] the table instance
# File lib/term/table.rb, line 73 def self.table IO::Table.new end
set icon name and window title
@example set the terminal icon name and window title
IO.title = 'window title'
@return [Term] Term
self.
# File lib/term/window.rb, line 12 def self.title=(text = '') print "\e]0;#{text}\a" self end
home-positioning to x and y coordinates. @param pos [Hash] opt the coordinates @option opt [Integer] x the x coordinates, default 0 @option opt [Integer] y the y coordinates, default 0 @return [Term] Term
self.
# 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
moves the cursor up by num rows.
@param num [Integer] the rows, the default num is 1. @return [Term] Term
self.
# File lib/term/cursor.rb, line 74 def self.up(num = 1) print "\e[#{num}A" # CUU self end
get window width.
@return [Integer] the window width
# File lib/term/window.rb, line 40 def self.width $stdout.winsize[1] end
set window title
@example set the terminal window title
IO.window_title = 'window title'
@return [Term] Term
self.
# File lib/term/window.rb, line 32 def self.window_title=(text = '') print "\e]2;#{text}\a" self end
Private Class Methods
hide password text.
# File lib/term/password.rb, line 50 def self.hide_password_text IO.to(@begin_password_pos).clear_forward print '🔑 : ⚫ ⚫ ⚫ ⚫ ⚫ ⚫ ⚫ ⚫ 👁️' @end_password_pos ||= IO.pos end
process password input event.
# File lib/term/password.rb, line 19 def self.process_password_input @redio_loop = true while @redio_loop input = IO.input process_password_mouse_click input process_password_keyboard_input input[:key] end end
process keyboard input event.
# File lib/term/password.rb, line 30 def self.process_password_keyboard_input(key) @password_text ||= '' @password_text << key.to_s if key.size == 1 @redio_loop = false if key == :enter end
process mouse click event.
# File lib/term/password.rb, line 38 def self.process_password_mouse_click(input) type = input[:type] pos = input[:pos] if (type == :left_pressed) && (pos == @end_password_pos) show_password_text else hide_password_text end end
show password text.
# File lib/term/password.rb, line 58 def self.show_password_text IO.to(@begin_password_pos).clear_forward print "🔑 : #{@password_text || ''}" end