Class: Slk::Formatters::ProfileFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/slk/formatters/profile_formatter.rb

Overview

Renders a Models::Profile for ‘slk who`. Compact mode: teems-style two-column card. Full mode: section-grouped layout matching Slack web UI.

Constant Summary collapse

MIN_LABEL_WIDTH =
8
MAX_LABEL_WIDTH =
20

Instance Method Summary collapse

Constructor Details

#initialize(output:, emoji_replacer: nil) ⇒ ProfileFormatter

Returns a new instance of ProfileFormatter.



12
13
14
15
16
# File 'lib/slk/formatters/profile_formatter.rb', line 12

def initialize(output:, emoji_replacer: nil)
  @output = output
  field_renderer = ProfileFieldRenderer.new(output: output)
  @rows = ProfileRows.new(field_renderer: field_renderer, emoji_replacer: emoji_replacer)
end

Instance Method Details

#compact(profile) ⇒ Object



18
19
20
21
22
# File 'lib/slk/formatters/profile_formatter.rb', line 18

def compact(profile)
  render_header(profile)
  emit_rows(@rows.compact(profile))
  nil
end

#emit_rows(rows) ⇒ Object



33
34
35
36
37
# File 'lib/slk/formatters/profile_formatter.rb', line 33

def emit_rows(rows)
  non_empty = rows.reject { |_, v| v.nil? || v.to_s.empty? }
  width = label_width(non_empty)
  non_empty.each { |label, value| emit_row(label, value, width) }
end

#full(profile) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/slk/formatters/profile_formatter.rb', line 24

def full(profile)
  render_header(profile)
  return emit_rows(@rows.external(profile)) if profile.external?

  render_section('Contact information', @rows.contact(profile))
  render_section('People', @rows.people(profile))
  render_section('About me', @rows.about(profile))
end

#label_for(label) ⇒ Object



44
45
46
47
# File 'lib/slk/formatters/profile_formatter.rb', line 44

def label_for(label)
  text = label.to_s
  text.length > MAX_LABEL_WIDTH ? "#{text[0, MAX_LABEL_WIDTH - 1]}" : text
end

#label_width(rows) ⇒ Object



39
40
41
42
# File 'lib/slk/formatters/profile_formatter.rb', line 39

def label_width(rows)
  max = rows.map { |label, _| label_for(label).length }.max || 0
  max.clamp(MIN_LABEL_WIDTH, MAX_LABEL_WIDTH)
end