Class: Luck::ANSIDriver

Inherits:
Object
  • Object
show all
Defined in:
lib/luck/ansi.rb

Constant Summary collapse

TIOCGWINSZ =

yay grep

0x5413
TCGETS =
0x5401
TCSETS =
0x5402
ECHO =

0000010

8
ICANON =

0000002

2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeANSIDriver

Returns a new instance of ANSIDriver.



5
6
7
8
9
# File 'lib/luck/ansi.rb', line 5

def initialize
  prepare_modes
  
  @height, @width = terminal_size
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



3
4
5
# File 'lib/luck/ansi.rb', line 3

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



3
4
5
# File 'lib/luck/ansi.rb', line 3

def width
  @width
end

Instance Method Details

#clearObject



43
# File 'lib/luck/ansi.rb', line 43

def clear; print "\e[H"; end

#color(codes) ⇒ Object



22
23
24
# File 'lib/luck/ansi.rb', line 22

def color codes
  "\e[#{codes}m"
end

#cursor=(show) ⇒ Object



26
27
28
# File 'lib/luck/ansi.rb', line 26

def cursor=(show)
  print "\e[?25" + (show ? 'h' : 'l')
end

#cursor_to_homeObject



44
# File 'lib/luck/ansi.rb', line 44

def cursor_to_home; print "\e[J"; end

#flushObject



11
12
13
# File 'lib/luck/ansi.rb', line 11

def flush
  $stdout.flush
end

#linedrawing=(toggle) ⇒ Object



30
31
32
# File 'lib/luck/ansi.rb', line 30

def linedrawing=(toggle)
  print(toggle ? "\x0E\e)0" : "\x0F")
end

#prepare_modesObject

had to convert these from C… fun



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/luck/ansi.rb', line 195

def prepare_modes
  buf = [0, 0, 0, 0, 0, 0, ''].pack("IIIICCA*")
  $stdout.ioctl(TCGETS, buf)
  @old_modes = buf.unpack("IIIICCA*")
  new_modes = @old_modes.clone
  new_modes[3] &= ~ECHO # echo off
  new_modes[3] &= ~ICANON # one char @ a time
  $stdout.ioctl(TCSETS, new_modes.pack("IIIICCA*"))
  print "\e[2J" # clear screen
  print "\e[H" # go home
  print "\e[?47h" # kick xterm into the alt screen
  print "\e[?1000h" # kindly ask for mouse positions to make up for it
  self.cursor = false
  flush
end

#put(row, col, text) ⇒ Object



15
16
17
18
19
20
# File 'lib/luck/ansi.rb', line 15

def put row, col, text
  text.each_line do |line|
    print "\e[#{row.to_i};#{col.to_i}H#{line}"
    row += 1
  end
end

#resized?Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
# File 'lib/luck/ansi.rb', line 34

def resized?
  size = terminal_size
  
  return false if [@height, @width] == size
  
  @height, @width = size
  true
end

#restore_cursorObject



47
# File 'lib/luck/ansi.rb', line 47

def restore_cursor; print "\e[u"; end

#save_cursorObject



46
# File 'lib/luck/ansi.rb', line 46

def save_cursor; print "\e[s"; end

#set_cursor(row, col) ⇒ Object



48
# File 'lib/luck/ansi.rb', line 48

def set_cursor row, col; put row, col, "\e[s"; end

#set_title(title) ⇒ Object



50
# File 'lib/luck/ansi.rb', line 50

def set_title title; print "\e]2;#{title}\007"; end

#terminal_sizeObject

thanks google for all of this



185
186
187
188
189
190
191
192
# File 'lib/luck/ansi.rb', line 185

def terminal_size
  rows, cols = 25, 80
  buf = [0, 0, 0, 0].pack("SSSS")
  if $stdout.ioctl(TIOCGWINSZ, buf) >= 0 then
    rows, cols, row_pixels, col_pixels = buf.unpack("SSSS")
  end
  return [rows, cols]
end

#undo_modesObject

restore previous terminal mode



211
212
213
214
215
216
217
218
219
220
# File 'lib/luck/ansi.rb', line 211

def undo_modes # restore previous terminal mode
  $stdout.ioctl(TCSETS, @old_modes.pack("IIIICCA*"))
  print "\e[2J" # clear screen
  print "\e[H" # go home
  print "\e[?47l" # kick xterm back into the normal screen
  print "\e[?1000l" # turn off mouse reporting
  self.linedrawing = false
  self.cursor = true # show the mouse
  flush
end