Module: Termfront::TerminalOutput

Defined in:
lib/termfront/terminal_output.rb

Constant Summary collapse

ANSI_PATTERN =
/\e\[[0-9;]*[A-Za-z]/.freeze

Class Method Summary collapse

Class Method Details

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



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

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

.end_frameObject



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

def end_frame
  ""
end

.fit_ansi(text, width) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/termfront/terminal_output.rb', line 19

def fit_ansi(text, width)
  visible = 0
  out = +""
  index = 0

  while index < text.length && visible < width
    if (match = ANSI_PATTERN.match(text, index)) && match.begin(0) == index
      out << match[0]
      index = match.end(0)
      next
    end

    char = text[index]
    out << char
    visible += 1
    index += 1
  end

  out << "\e[0m" if out.include?("\e[")
  out << (" " * (width - visible)) if visible < width
  out
end

.write_all(io, data) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/termfront/terminal_output.rb', line 42

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