Class: ConfCtl::Cli::LogView

Inherits:
Object
  • Object
show all
Defined in:
lib/confctl/cli/log_view.rb

Overview

Create a fixed-size box showing the last ‘n` lines from streamed data

Constant Summary collapse

CONSOLE_LOCK =

All writes to the console must go through this lock

Monitor.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header: nil, title: nil, size: 10, reserved_lines: 0, output: $stdout) ⇒ LogView

Returns a new instance of LogView.

Parameters:

  • header (String) (defaults to: nil)

    optional string outputted above the box, must have new lines

  • title (String) (defaults to: nil)

    optional box title

  • size (Integer, :auto) (defaults to: 10)

    number of lines to show

  • reserved_lines (Integer) (defaults to: 0)

    number of reserved lines below the box when ‘size` is `:auto`

  • output (IO) (defaults to: $stdout)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/confctl/cli/log_view.rb', line 55

def initialize(header: nil, title: nil, size: 10, reserved_lines: 0, output: $stdout)
  @cursor = TTY::Cursor
  @outmutex = Mutex.new
  @inlines = Queue.new
  @outlines = []
  @header = header
  @title = title || 'Log'
  @size = size
  @current_size = size if size != :auto
  @reserved_lines = reserved_lines
  @output = output
  @enabled = output.respond_to?(:tty?) && output.tty?
  @resized = false
  @stop = false
  @generation = 0
  @rendered = 0
end

Class Method Details

.open(**kwargs) {|log_view| ... } ⇒ Object

Instantiate ConfCtl::Cli::LogView, yield and then cleanup

Yield Parameters:



17
18
19
20
21
22
23
24
25
26
# File 'lib/confctl/cli/log_view.rb', line 17

def self.open(**kwargs)
  lw = new(**kwargs)
  lw.start

  begin
    yield(lw)
  ensure
    lw.stop
  end
end

.open_with_logger(**kwargs) {|log_view| ... } ⇒ Object

Instantiate ConfCtl::Cli::LogView with feed from Logger, yield and then cleanup

Yield Parameters:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/confctl/cli/log_view.rb', line 31

def self.open_with_logger(**kwargs)
  lw = new(**kwargs)
  lw.start

  lb = ConfCtl::LineBuffer.new { |line| lw << line }
  ConfCtl::Logger.instance.add_reader(lb)

  begin
    yield(lw)
  ensure
    ConfCtl::Logger.instance.remove_reader(lb)
    lw.stop
  end
end

.sync_consoleObject



11
12
13
# File 'lib/confctl/cli/log_view.rb', line 11

def self.sync_console(&)
  CONSOLE_LOCK.synchronize(&)
end

Instance Method Details

#<<(line) ⇒ Object



104
105
106
# File 'lib/confctl/cli/log_view.rb', line 104

def <<(line)
  inlines << line.strip
end

#enabled?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/confctl/cli/log_view.rb', line 112

def enabled?
  @enabled
end

#flushObject



100
101
102
# File 'lib/confctl/cli/log_view.rb', line 100

def flush
  sleep(1)
end

#startObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/confctl/cli/log_view.rb', line 73

def start
  return unless enabled?

  @stop = false
  fetch_size
  init
  render_inplace(outlines)
  @feeder = Thread.new { feeder_loop }
  @renderer = Thread.new { renderer_loop }

  Signal.trap('WINCH') do
    fetch_size
    @resized = true
  end
end

#stopObject



89
90
91
92
93
94
95
96
97
98
# File 'lib/confctl/cli/log_view.rb', line 89

def stop
  return if @stop || !enabled?

  @stop = true
  inlines.clear
  inlines << :stop
  feeder.join
  renderer.join
  Signal.trap('WINCH', 'DEFAULT')
end

#sync_consoleObject



108
109
110
# File 'lib/confctl/cli/log_view.rb', line 108

def sync_console(&)
  self.class.sync_console(&)
end