Class: IO

Inherits:
Object
  • Object
show all
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

Author:

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

Class Method Details

.backward(num = 1) ⇒ Term

moves the cursor backward by num columns.

Parameters:

  • num (Integer) (defaults to: 1)

    the columns, the default num is 1.

Returns:

  • (Term)

    Term self.



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

def self.backward(num = 1)
  print "\e[#{num}D" # CUB
  self
end

.bellObject

ring a bell to a terminal.

Examples:

IO.bell

Returns:

  • self



10
11
12
13
# File 'lib/term/bell.rb', line 10

def self.bell
  print "\a"
  self
end

.clearTerm

Note:

cursor position unchanged

clear screen.

Returns:

  • (Term)

    Term self.



128
129
130
131
# File 'lib/term/cursor.rb', line 128

def self.clear
  print "\e[2J"
  self
end

.clear_backwardTerm

Note:

cursor position unchanged

clear line from beginning to current cursor position.

Returns:

  • (Term)

    Term self.



146
147
148
149
# File 'lib/term/cursor.rb', line 146

def self.clear_backward
  print "\e[1K"
  self
end

.clear_downTerm

Note:

cursor position unchanged

erase the screen from the current line down to the top of the screen.

Returns:

  • (Term)

    Term self.



173
174
175
176
# File 'lib/term/cursor.rb', line 173

def self.clear_down
  print "\e[J"
  self
end

.clear_forwardTerm

Note:

cursor position unchanged

clear line from current cursor position to end of line.

Returns:

  • (Term)

    Term self.



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

def self.clear_forward
  print "\e[K"
  self
end

.clear_lineTerm

Note:

cursor position unchanged

clear whole line.

Returns:

  • (Term)

    Term self.



155
156
157
158
# File 'lib/term/cursor.rb', line 155

def self.clear_line
  print "\e[2K"
  self
end

.clear_upTerm

Note:

cursor position unchanged

erase the screen from the current line up to the top of the screen.

Returns:

  • (Term)

    Term self.



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.

Parameters:

  • num (Integer) (defaults to: 1)

    the rows, the default num is 1.

Returns:

  • (Term)

    Term self.



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.

Parameters:

  • num (Integer) (defaults to: 1)

    the columns, the default num is 1.

Returns:

  • (Term)

    Term self.



92
93
94
95
# File 'lib/term/cursor.rb', line 92

def self.forward(num = 1)
  print "\e[#{num}C" # CUF
  self
end

.heightInteger

get window height.

Returns:

  • (Integer)

    the window height



47
48
49
# File 'lib/term/window.rb', line 47

def self.height
  $stdout.winsize[0]
end

.hideTerm

make cursor invisible.

Returns:

  • (Term)

    Term self.



34
35
36
37
# File 'lib/term/cursor.rb', line 34

def self.hide
  print "\e[?25l"
  self
end

.homeself

move cursor to home position (0-0).

Returns:

  • (self)

    Term self.



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

Examples:

set the terminal icon name

IO.icon_name = 'icon title'

Returns:

  • (Term)

    Term self.



22
23
24
25
# File 'lib/term/window.rb', line 22

def self.icon_name=(text = '')
  print "\e]1;#{text}\a"
  self
end

.inputHash

get keybord & mouse key.

Returns:

  • (Hash)

    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.

Parameters:

  • num (Integer) (defaults to: 1)

    the times, the default num is 1.

Returns:

  • (Term)

    Term self.



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

.passwordString

get password.

Returns:



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

.posHash

query cursor position.

Returns:

  • (Hash)

    pos 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.

Parameters:

  • num (Integer) (defaults to: 1)

    the times, the default num is 1.

Returns:

  • (Term)

    Term self.



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

def self.prev_line(num = 1)
  print "\e[#{num}F"
  self
end

.restoreTerm

restore saved cursor position & attrs.

Returns:

  • (Term)

    Term self.



26
27
28
29
# File 'lib/term/cursor.rb', line 26

def self.restore
  print "\e8"
  self
end

.saveTerm

save current cursor position & attrs.

Returns:

  • (Term)

    Term self.



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

def self.save
  print "\e7"
  self
end

.showTerm

make cursor visible.

Returns:

  • (Term)

    Term self.



42
43
44
45
# File 'lib/term/cursor.rb', line 42

def self.show
  print "\e[?25h"
  self
end

.tableTable

get table component.

Returns:

  • (Table)

    the table instance



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

Examples:

set the terminal icon name and window title

IO.title = 'window title'

Returns:

  • (Term)

    Term self.



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.

Parameters:

  • pos (Hash)

    opt the coordinates

  • opt (Hash)

    a customizable set of options

Returns:

  • (Term)

    Term self.



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.

Parameters:

  • num (Integer) (defaults to: 1)

    the rows, the default num is 1.

Returns:

  • (Term)

    Term self.



74
75
76
77
# File 'lib/term/cursor.rb', line 74

def self.up(num = 1)
  print "\e[#{num}A" # CUU
  self
end

.widthInteger

get window width.

Returns:

  • (Integer)

    the 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

Examples:

set the terminal window title

IO.window_title = 'window title'

Returns:

  • (Term)

    Term self.



32
33
34
35
# File 'lib/term/window.rb', line 32

def self.window_title=(text = '')
  print "\e]2;#{text}\a"
  self
end