Class: Foruiman::TUI::LogFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/foruiman/tui/log_formatter.rb

Instance Method Summary collapse

Constructor Details

#initialize(theme, names) ⇒ LogFormatter

Returns a new instance of LogFormatter.



8
9
10
11
12
# File 'lib/foruiman/tui/log_formatter.rb', line 8

def initialize(theme, names)
  @theme = theme
  @colors = names.each_with_index.to_h { |name, index| [name, theme.process(index)] }
  @name_width = names.map { |name| Text.width(name) }.max.to_i.clamp(3, 14)
end

Instance Method Details

#row(record, aggregate:, width:) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/foruiman/tui/log_formatter.rb', line 14

def row(record, aggregate:, width:)
  prefix = +""
  prefix << @theme.paint("#{record.time.strftime('%H:%M:%S')}  ", :faint) if width >= 55
  if aggregate
    name_width = [@name_width, width / 5].min
    name = Text.pad(Text.clip(record.name, name_width, ellipsis: true), name_width)
    prefix << @theme.paint(name, @colors.fetch(record.name, :cyan)) << "  "
  end
  marker, color = stream_style(record.stream)
  prefix << @theme.paint(marker, color) << @theme.paint(" │ ", :border)
  content = record.stream == :lifecycle ? record.text.delete_prefix("--- ").delete_suffix(" ---") : record.text
  content = Text.clip(content, [width - Text.width(prefix), 0].max, ellipsis: true)
  prefix + @theme.log(content, color: body_color(record.stream))
end