Class: XCCache::LiveLog
- Inherits:
-
Object
- Object
- XCCache::LiveLog
- Includes:
- UI::Mixin
- Defined in:
- lib/xccache/core/live_log.rb
Constant Summary collapse
- CURSOR_LOCK =
Monitor.new
Instance Attribute Summary collapse
-
#cursor ⇒ Object
readonly
Returns the value of attribute cursor.
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
-
#max_lines ⇒ Object
readonly
Returns the value of attribute max_lines.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#tee ⇒ Object
readonly
Returns the value of attribute tee.
Attributes included from UI::Mixin
Instance Method Summary collapse
- #capture(header) ⇒ Object
- #clear ⇒ Object
-
#initialize(**options) ⇒ LiveLog
constructor
A new instance of LiveLog.
- #puts(line, sticky: false) ⇒ Object
Methods included from UI::Mixin
#error, #error!, #info, #message, #section, #warn
Methods included from Config::Mixin
Constructor Details
#initialize(**options) ⇒ LiveLog
Returns a new instance of LiveLog.
12 13 14 15 16 17 18 19 20 |
# File 'lib/xccache/core/live_log.rb', line 12 def initialize(**) @output = [:output] || $stdout @max_lines = [:max_lines] || 5 @n_sticky = 0 @lines = [] @cursor = TTY::Cursor @screen = TTY::Screen @tee = [:tee] end |
Instance Attribute Details
#cursor ⇒ Object (readonly)
Returns the value of attribute cursor.
10 11 12 |
# File 'lib/xccache/core/live_log.rb', line 10 def cursor @cursor end |
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
10 11 12 |
# File 'lib/xccache/core/live_log.rb', line 10 def lines @lines end |
#max_lines ⇒ Object (readonly)
Returns the value of attribute max_lines.
10 11 12 |
# File 'lib/xccache/core/live_log.rb', line 10 def max_lines @max_lines end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
10 11 12 |
# File 'lib/xccache/core/live_log.rb', line 10 def output @output end |
#tee ⇒ Object (readonly)
Returns the value of attribute tee.
10 11 12 |
# File 'lib/xccache/core/live_log.rb', line 10 def tee @tee end |
Instance Method Details
#capture(header) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/xccache/core/live_log.rb', line 45 def capture(header) header_start = header.magenta.bold header_success = "#{header} ✔".green.bold header_error = "#{header} ✖".red.bold puts(header_start, sticky: true) yield if block_given? clear update_header(header_success) rescue StandardError => e update_header(header_error) raise e end |
#clear ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/xccache/core/live_log.rb', line 22 def clear commit do output.print(cursor.clear_lines(lines.count + @n_sticky)) @lines = [] @n_sticky = 0 end end |
#puts(line, sticky: false) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/xccache/core/live_log.rb', line 30 def puts(line, sticky: false) commit do output.print(cursor.clear_lines(lines.count + 1)) if sticky @n_sticky += 1 output.puts(truncated(line)) else lines.shift if lines.count >= max_lines lines << truncated(line) end output.puts(lines) # print non-sticky content end File.open(tee, "a") { |f| f << "#{line}\n" } if tee end |