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
- .begin_frame(home: false, clear: false) ⇒ Object
- .end_frame ⇒ Object
- .fit_ansi(text, width) ⇒ Object
- .sync_updates? ⇒ Boolean
- .write_all(io, data) ⇒ Object
Class Method Details
.begin_frame(home: false, clear: false) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/termfront/terminal_output.rb', line 12 def begin_frame(home: false, clear: false) buf = +"" buf << "\e[?2026h" if sync_updates? buf << "\e[H" if home buf << "\e[2J" if clear buf end |
.end_frame ⇒ Object
20 21 22 |
# File 'lib/termfront/terminal_output.rb', line 20 def end_frame sync_updates? ? "\e[?2026l" : "" end |
.fit_ansi(text, width) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/termfront/terminal_output.rb', line 24 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 |
.sync_updates? ⇒ Boolean
8 9 10 |
# File 'lib/termfront/terminal_output.rb', line 8 def sync_updates? ENV.fetch("TERMFRONT_SYNC_UPDATES", "1") == "1" end |
.write_all(io, data) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/termfront/terminal_output.rb', line 47 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 |