Module: Termfront::TerminalOutput

Defined in:
lib/termfront/terminal_output.rb

Class Method Summary collapse

Class Method Details

.begin_frame(home: false, clear: false) ⇒ Object



7
8
9
10
11
12
# File 'lib/termfront/terminal_output.rb', line 7

def begin_frame(home: false, clear: false)
  buf = +""
  buf << "\e[H" if home
  buf << "\e[2J" if clear
  buf
end

.end_frameObject



14
15
16
# File 'lib/termfront/terminal_output.rb', line 14

def end_frame
  ""
end

.fit_ansi(text, width) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/termfront/terminal_output.rb', line 18

def fit_ansi(text, width)
  out = +""
  visible = 0
  any_ansi = false
  i = 0
  len = text.bytesize
  segment_start = 0

  while i < len
    byte = text.getbyte(i)
    if byte == 0x1B
      out << text.byteslice(segment_start, i - segment_start) if i > segment_start

      esc_start = i
      i += 1
      while i < len
        b = text.getbyte(i)
        i += 1
        break if (b >= 65 && b <= 90) || (b >= 97 && b <= 122)
      end
      out << text.byteslice(esc_start, i - esc_start)
      any_ansi = true
      segment_start = i
    else
      if (byte & 0xC0) != 0x80
        break if visible >= width

        visible += 1
      end
      i += 1
    end
  end

  out << text.byteslice(segment_start, i - segment_start) if i > segment_start
  out << "\e[0m" if any_ansi
  (width - visible).times { out << " " } if visible < width
  out
end

.write_all(io, data) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/termfront/terminal_output.rb', line 57

def write_all(io, data)
  total = 0
  bytes = data.bytesize

  while total < bytes
    begin
      written = io.syswrite(data.byteslice(total, bytes - total))
      total += written
    rescue IO::WaitWritable
      IO.select(nil, [io])
    end
  end

  total
end