Class: Chamomile::Help

Inherits:
Object
  • Object
show all
Defined in:
lib/chamomile/components/help.rb

Overview

Auto-generated keybinding help view with short and full display modes.

Constant Summary collapse

ELLIPSIS =
"\u2026"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width: 80) ⇒ Help

Returns a new instance of Help.



10
11
12
13
14
15
16
# File 'lib/chamomile/components/help.rb', line 10

def initialize(width: 80)
  @width = width
  @short_separator = " \u2022 "
  @full_separator = "    "
  @ellipsis = ELLIPSIS
  @show_all = false
end

Instance Attribute Details

#ellipsisObject

Returns the value of attribute ellipsis.



8
9
10
# File 'lib/chamomile/components/help.rb', line 8

def ellipsis
  @ellipsis
end

#full_separatorObject

Returns the value of attribute full_separator.



8
9
10
# File 'lib/chamomile/components/help.rb', line 8

def full_separator
  @full_separator
end

#short_separatorObject

Returns the value of attribute short_separator.



8
9
10
# File 'lib/chamomile/components/help.rb', line 8

def short_separator
  @short_separator
end

#show_allObject

Returns the value of attribute show_all.



8
9
10
# File 'lib/chamomile/components/help.rb', line 8

def show_all
  @show_all
end

#widthObject

Returns the value of attribute width.



8
9
10
# File 'lib/chamomile/components/help.rb', line 8

def width
  @width
end

Instance Method Details

#full_help_view(groups) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/chamomile/components/help.rb', line 30

def full_help_view(groups)
  filtered_groups = groups.map { |g| enabled_bindings(g) }.reject(&:empty?)
  return "" if filtered_groups.empty?

  max_rows = filtered_groups.map(&:length).max
  lines = Array.new(max_rows, "")

  max_rows.times do |row|
    cols = filtered_groups.map do |group|
      if row < group.length
        b = group[row]
        "#{b[:key]}  #{b[:desc]}"
      else
        ""
      end
    end
    lines[row] = cols.join(@full_separator).rstrip
  end

  lines.join("\n")
end

#handle(_msg) ⇒ Object Also known as: update



62
63
64
# File 'lib/chamomile/components/help.rb', line 62

def handle(_msg)
  nil
end

#short_help_view(bindings) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/chamomile/components/help.rb', line 18

def short_help_view(bindings)
  filtered = enabled_bindings(bindings)
  return "" if filtered.empty?

  parts = filtered.map { |b| "#{b[:key]} #{b[:desc]}" }
  result = parts.join(@short_separator)

  return result if @width <= 0 || result.length <= @width

  truncate(parts)
end

#view(bindings_or_groups) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/chamomile/components/help.rb', line 52

def view(bindings_or_groups)
  if @show_all
    groups = bindings_or_groups.first.is_a?(Array) ? bindings_or_groups : [bindings_or_groups]
    full_help_view(groups)
  else
    flat = bindings_or_groups.first.is_a?(Array) ? bindings_or_groups.flatten : bindings_or_groups
    short_help_view(flat)
  end
end