Class: XCCache::LiveLog

Inherits:
Object
  • Object
show all
Includes:
UI::Mixin
Defined in:
lib/xccache/core/live_log.rb

Constant Summary collapse

CURSOR_LOCK =
Monitor.new

Instance Attribute Summary collapse

Attributes included from UI::Mixin

#indent

Instance Method Summary collapse

Methods included from UI::Mixin

#error, #error!, #info, #message, #section, #warn

Methods included from Config::Mixin

#config

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(**options)
  @output = options[:output] || $stdout
  @max_lines = options[:max_lines] || 5
  @n_sticky = 0
  @lines = []
  @cursor = TTY::Cursor
  @screen = TTY::Screen
  @tee = options[:tee]
end

Instance Attribute Details

#cursorObject (readonly)

Returns the value of attribute cursor.



10
11
12
# File 'lib/xccache/core/live_log.rb', line 10

def cursor
  @cursor
end

#linesObject (readonly)

Returns the value of attribute lines.



10
11
12
# File 'lib/xccache/core/live_log.rb', line 10

def lines
  @lines
end

#max_linesObject (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

#outputObject (readonly)

Returns the value of attribute output.



10
11
12
# File 'lib/xccache/core/live_log.rb', line 10

def output
  @output
end

#teeObject (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

#clearObject



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