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

backward(num = 1) click to toggle source

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

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

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

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

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

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

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

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
down(num = 1) click to toggle source

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
forward(num = 1) click to toggle source

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

get window height.

@return [Integer] the window height

# File lib/term/window.rb, line 47
def self.height
  $stdout.winsize[0]
end
hide() click to toggle source

make cursor invisible.

@return [Term] Term self.

# File lib/term/cursor.rb, line 34
def self.hide
  print "\e[?25l"
  self
end
home() click to toggle source

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
icon_name=(text = '') click to toggle source

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

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

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

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

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

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

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

restore saved cursor position & attrs.

@return [Term] Term self.

# File lib/term/cursor.rb, line 26
def self.restore
  print "\e8"
  self
end
save() click to toggle source

save current cursor position & attrs.

@return [Term] Term self.

# File lib/term/cursor.rb, line 18
def self.save
  print "\e7"
  self
end
show() click to toggle source

make cursor visible.

@return [Term] Term self.

# File lib/term/cursor.rb, line 42
def self.show
  print "\e[?25h"
  self
end
table() click to toggle source

get table component.

@return [Table] the table instance

# File lib/term/table.rb, line 73
def self.table
  IO::Table.new
end
title=(text = '') click to toggle source

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

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
up(num = 1) click to toggle source

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

get window width.

@return [Integer] the window width

# File lib/term/window.rb, line 40
def self.width
  $stdout.winsize[1]
end
window_title=(text = '') click to toggle source

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

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

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

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

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

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