Class: OTerm::VT100
- Inherits:
-
Object
- Object
- OTerm::VT100
- Defined in:
- lib/oterm/vt100.rb
Direct Known Subclasses
Constant Summary collapse
- ESC =
27.chr
- MAX_DIM =
9999
- BLANK =
graphic font characters
'_'
- DIAMOND =
'`'
- CHECKER =
'a'
- HT =
'b'
- FF =
'c'
- CR =
'd'
- LF =
'e'
- DEGREE =
'f'
- PLUS_MINUS =
'g'
- NL =
'h'
- VT =
'i'
- LOW_RIGHT =
'j'
- UP_RIGHT =
'k'
- UP_LEFT =
'l'
- LOW_LEFT =
'm'
- CROSS =
'n'
- DASH1 =
'o'
- DASH3 =
'p'
- DASH5 =
'q'
- DASH7 =
'r'
- DASH9 =
's'
- LEFT_T =
't'
- RIGHT_T =
'u'
- LOW_T =
'v'
- UP_T =
'w'
- BAR =
'x'
- LTE =
'y'
- GTE =
'z'
- PI =
'{'
- NE =
'|'
- DOT =
'~'
- BLACK =
colors
30
- RED =
31
- GREEN =
32
- YELLOW =
33
- BLUE =
34
- MAGENTA =
35
- CYAN =
36
- WHITE =
37
Instance Attribute Summary collapse
-
#con ⇒ Object
Returns the value of attribute con.
Instance Method Summary collapse
- #absolute_origin ⇒ Object
- #attrs_off ⇒ Object
- #big_bottom ⇒ Object
- #big_top ⇒ Object
- #blink ⇒ Object
- #bold ⇒ Object
- #clear_line ⇒ Object
- #clear_screen ⇒ Object
- #clear_to_end ⇒ Object
- #clear_to_start ⇒ Object
- #default_font ⇒ Object
- #dim ⇒ Object
- #down(n) ⇒ Object
- #frame(y, x, h, w) ⇒ Object
- #get_cursor ⇒ Object
- #graphic_font ⇒ Object
- #home ⇒ Object
- #identify ⇒ Object
-
#initialize(con) ⇒ VT100
constructor
A new instance of VT100.
- #invisible ⇒ Object
- #is_vt100? ⇒ Boolean
- #left(n) ⇒ Object
- #narrow ⇒ Object
- #recv_wait(max, timeout, pat) ⇒ Object
- #relative_origin ⇒ Object
-
#reset ⇒ Object
Reset terminal to initial state.
-
#restore_cursor ⇒ Object
Restore cursor position and attributes.
- #reverse ⇒ Object
- #right(n) ⇒ Object
-
#save_cursor ⇒ Object
Save cursor position and attributes.
- #screen_size ⇒ Object
- #scroll(n) ⇒ Object
- #set_colors(fg, bg) ⇒ Object
-
#set_cursor(v, h) ⇒ Object
Move cursor to screen location v, h.
- #underline ⇒ Object
- #up(n) ⇒ Object
- #wide ⇒ Object
Constructor Details
#initialize(con) ⇒ VT100
Returns a new instance of VT100.
53 54 55 56 |
# File 'lib/oterm/vt100.rb', line 53 def initialize(con) @con = con @is_vt100 = false end |
Instance Attribute Details
#con ⇒ Object
Returns the value of attribute con.
51 52 53 |
# File 'lib/oterm/vt100.rb', line 51 def con @con end |
Instance Method Details
#absolute_origin ⇒ Object
132 133 134 |
# File 'lib/oterm/vt100.rb', line 132 def absolute_origin() @con.print("\x1b[?6l") if @is_vt100 end |
#attrs_off ⇒ Object
136 137 138 |
# File 'lib/oterm/vt100.rb', line 136 def attrs_off() @con.print("\x1b[m") if @is_vt100 end |
#big_bottom ⇒ Object
168 169 170 |
# File 'lib/oterm/vt100.rb', line 168 def big_bottom() @con.print("\x1b#4") if @is_vt100 end |
#big_top ⇒ Object
164 165 166 |
# File 'lib/oterm/vt100.rb', line 164 def big_top() @con.print("\x1b#3") if @is_vt100 end |
#blink ⇒ Object
152 153 154 |
# File 'lib/oterm/vt100.rb', line 152 def blink() @con.print("\x1b[5m") if @is_vt100 end |
#bold ⇒ Object
140 141 142 |
# File 'lib/oterm/vt100.rb', line 140 def bold() @con.print("\x1b[1m") if @is_vt100 end |
#clear_line ⇒ Object
116 117 118 |
# File 'lib/oterm/vt100.rb', line 116 def clear_line() @con.print("\x1b[2K") if @is_vt100 end |
#clear_screen ⇒ Object
112 113 114 |
# File 'lib/oterm/vt100.rb', line 112 def clear_screen() @con.print("\x1b[2J") if @is_vt100 end |
#clear_to_end ⇒ Object
120 121 122 |
# File 'lib/oterm/vt100.rb', line 120 def clear_to_end() @con.print("\x1b[0K") if @is_vt100 end |
#clear_to_start ⇒ Object
124 125 126 |
# File 'lib/oterm/vt100.rb', line 124 def clear_to_start() @con.print("\x1b[1K") if @is_vt100 end |
#default_font ⇒ Object
107 108 109 110 |
# File 'lib/oterm/vt100.rb', line 107 def default_font() # TBD allow to set to specific character set for original terminal, for now US font @con.print("\x1b(B") if @is_vt100 end |
#dim ⇒ Object
144 145 146 |
# File 'lib/oterm/vt100.rb', line 144 def dim() @con.print("\x1b[2m") if @is_vt100 end |
#down(n) ⇒ Object
201 202 203 |
# File 'lib/oterm/vt100.rb', line 201 def down(n) @con.print("\x1b[#{n}B") if @is_vt100 end |
#frame(y, x, h, w) ⇒ Object
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/oterm/vt100.rb', line 235 def frame(y, x, h, w) return if 2 > h || 2 > w graphic_font() set_cursor(y, x) @con.print(UP_LEFT + DASH5 * (w - 2) + UP_RIGHT) (h - 2).times do |i| i += 1 set_cursor(y + i, x) @con.print(BAR) set_cursor(y + i, x + w - 1) @con.print(BAR) end set_cursor(y + h - 1, x) @con.print(LOW_LEFT + DASH5 * (w - 2) + LOW_RIGHT) default_font() end |
#get_cursor ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/oterm/vt100.rb', line 71 def get_cursor() v, h = 0, 0 if @is_vt100 @con.print("\x1b[6n") # expect: ^[<v>;<h>R rx = /^\x1b\[(\d+);(\d+)R/ m = recv_wait(16, 1.0, rx) v, h = m.captures.map { |s| s.to_i } end return v, h end |
#graphic_font ⇒ Object
103 104 105 |
# File 'lib/oterm/vt100.rb', line 103 def graphic_font() @con.print("\x1b(2") if @is_vt100 end |
#home ⇒ Object
213 214 215 |
# File 'lib/oterm/vt100.rb', line 213 def home() @con.print("\x1b[H") if @is_vt100 end |
#identify ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/oterm/vt100.rb', line 62 def identify() @con.print("\x1b[c") # expect: ^[[?1;<n>0c rx = /^\x1b\[\?1;.+c/ m = recv_wait(10, 1.0, rx) # Don't care about the type, just that the reply is valid for a vt100. @is_vt100 = nil != m end |
#invisible ⇒ Object
160 161 162 |
# File 'lib/oterm/vt100.rb', line 160 def invisible() @con.print("\x1b[8m") if @is_vt100 end |
#is_vt100? ⇒ Boolean
58 59 60 |
# File 'lib/oterm/vt100.rb', line 58 def is_vt100?() return @is_vt100 end |
#left(n) ⇒ Object
205 206 207 |
# File 'lib/oterm/vt100.rb', line 205 def left(n) @con.print("\x1b[#{n}D") if @is_vt100 end |
#narrow ⇒ Object
172 173 174 |
# File 'lib/oterm/vt100.rb', line 172 def narrow() @con.print("\x1b#5") if @is_vt100 end |
#recv_wait(max, timeout, pat) ⇒ Object
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/oterm/vt100.rb', line 252 def recv_wait(max, timeout, pat) giveup = Time.now + timeout reply = '' begin while nil == pat.match(reply) # just peek incase the string is not what we want reply = @con.recv_nonblock(max, Socket::MSG_PEEK) # DEBUG # reply.each_byte { |x| print("#{x} ") } # plain = reply.gsub(/./) { |c| c.ord < 32 || 127 <= c.ord ? '*' : c } # puts "[#{reply.size()}] #{plain}" end rescue IO::WaitReadable now = Time.now if now < giveup IO.select([@con], [], [], giveup - now) retry end end m = pat.match(reply) if nil != m # There was a match so read the characters we already peeked. cnt = m.to_s().size if 0 < cnt @con.recv_nonblock(cnt) end end m end |
#relative_origin ⇒ Object
128 129 130 |
# File 'lib/oterm/vt100.rb', line 128 def relative_origin() @con.print("\x1b[?6h") if @is_vt100 end |
#reset ⇒ Object
Reset terminal to initial state.
99 100 101 |
# File 'lib/oterm/vt100.rb', line 99 def reset() @con.print("\x1bc") if @is_vt100 end |
#restore_cursor ⇒ Object
Restore cursor position and attributes.
94 95 96 |
# File 'lib/oterm/vt100.rb', line 94 def restore_cursor() @con.print("\x1b8") if @is_vt100 end |
#reverse ⇒ Object
156 157 158 |
# File 'lib/oterm/vt100.rb', line 156 def reverse() @con.print("\x1b[7m") if @is_vt100 end |
#right(n) ⇒ Object
209 210 211 |
# File 'lib/oterm/vt100.rb', line 209 def right(n) @con.print("\x1b[#{n}C") if @is_vt100 end |
#save_cursor ⇒ Object
Save cursor position and attributes.
89 90 91 |
# File 'lib/oterm/vt100.rb', line 89 def save_cursor() @con.print("\x1b7") if @is_vt100 end |
#screen_size ⇒ Object
227 228 229 230 231 232 233 |
# File 'lib/oterm/vt100.rb', line 227 def screen_size() save_cursor() set_cursor(MAX_DIM, MAX_DIM) h, w = get_cursor() restore_cursor() return h, w end |
#scroll(n) ⇒ Object
217 218 219 220 221 222 223 224 225 |
# File 'lib/oterm/vt100.rb', line 217 def scroll(n) return unless @is_vt100 if 0 > n n = -n n.times { @con.print("\x1b[D") } elsif 0 < n n.times { @con.print("\x1b[M") } end end |
#set_colors(fg, bg) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/oterm/vt100.rb', line 180 def set_colors(fg, bg) return unless @is_vt100 if nil == fg if nil != bg bg += 10 @con.print("\x1b[#{bg}m") end else if nil != bg bg += 10 @con.print("\x1b[#{fg};#{bg}m") else @con.print("\x1b[#{fg}m") end end end |
#set_cursor(v, h) ⇒ Object
Move cursor to screen location v, h.
84 85 86 |
# File 'lib/oterm/vt100.rb', line 84 def set_cursor(v, h) @con.print("\x1b[#{v};#{h}H") if @is_vt100 end |
#underline ⇒ Object
148 149 150 |
# File 'lib/oterm/vt100.rb', line 148 def underline() @con.print("\x1b[4m") if @is_vt100 end |
#up(n) ⇒ Object
197 198 199 |
# File 'lib/oterm/vt100.rb', line 197 def up(n) @con.print("\x1b[#{n}A") if @is_vt100 end |
#wide ⇒ Object
176 177 178 |
# File 'lib/oterm/vt100.rb', line 176 def wide() @con.print("\x1b#6") if @is_vt100 end |