Class: Clacky::RichUI::RichSidebar

Inherits:
Object
  • Object
show all
Defined in:
lib/clacky/rich_ui/components/sidebar.rb

Constant Summary collapse

MODES =
%i[work tasks context auto hidden].freeze
PANEL_HEIGHT_RATIOS =
{ 1 => [1.0], 2 => [0.5, 0.5], 3 => [0.34, 0.33, 0.33] }.freeze
PANEL_NAMES =
{ work: "Work", tasks: "Tasks", context: "Context" }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRichSidebar

Returns a new instance of RichSidebar.



16
17
18
19
20
21
22
23
24
25
# File 'lib/clacky/rich_ui/components/sidebar.rb', line 16

def initialize
  @mode = :auto
  @panels = {
    work: RichWorkPanel.new,
    tasks: RichTasksPanel.new,
    context: RichContextPanel.new
  }
  @width = 0
  @height = 0
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



13
14
15
# File 'lib/clacky/rich_ui/components/sidebar.rb', line 13

def height
  @height
end

#modeObject (readonly)

Returns the value of attribute mode.



14
15
16
# File 'lib/clacky/rich_ui/components/sidebar.rb', line 14

def mode
  @mode
end

#widthObject

Returns the value of attribute width.



13
14
15
# File 'lib/clacky/rich_ui/components/sidebar.rb', line 13

def width
  @width
end

Instance Method Details

#panel_has_content?(panel) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/clacky/rich_ui/components/sidebar.rb', line 105

def panel_has_content?(panel)
  case panel
  when RichWorkPanel
    true  # Always show — shows "0 tasks · $0.0000" when empty
  when RichTasksPanel
    panel.has_tasks?
  when RichContextPanel
    true  # Always show — shows "No token data" when empty
  else
    false
  end
end

#panel_heights(visible) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/clacky/rich_ui/components/sidebar.rb', line 90

def panel_heights(visible)
  max_h = [@height, 1].max
  # Context panel gets exactly 6 lines; remaining space split among others
  ctx_idx = visible.index(:context)
  if ctx_idx
    ctx_h = [6, max_h / [visible.length, 1].max].min
    other_count = visible.length - 1
    other_h = other_count > 0 ? (max_h - ctx_h) / other_count : 0
    visible.each_with_index.map { |_, i| i == ctx_idx ? ctx_h : [other_h, 1].max }
  else
    h = max_h / visible.length
    visible.map { [h, 1].max }
  end
end

#renderObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/clacky/rich_ui/components/sidebar.rb', line 60

def render
  visible = visible_panels
  return [""] if visible.empty?

  heights = panel_heights(visible)
  panel_lines = visible.each_with_index.flat_map do |key, i|
    panel = @panels[key]
    panel.width = [@width - 2, 1].max
    panel.height = heights[i]
    p = RubyRich::Panel.new(panel.render, title: PANEL_NAMES[key], border_style: :blue, title_align: :left)
    p.width = @width
    p.height = heights[i]
    p.render
  end
  panel_lines.first(@height)
end

#set_mode(mode) ⇒ Object



56
57
58
# File 'lib/clacky/rich_ui/components/sidebar.rb', line 56

def set_mode(mode)
  @mode = MODES.include?(mode) ? mode : :auto
end

#set_tasks(tasks) ⇒ Object



32
33
34
35
# File 'lib/clacky/rich_ui/components/sidebar.rb', line 32

def set_tasks(tasks)
  @panels[:tasks].set_tasks(tasks)
  self
end

#tasksObject

Returns the tasks list from the tasks panel (for tests/assertions)



52
53
54
# File 'lib/clacky/rich_ui/components/sidebar.rb', line 52

def tasks
  @panels[:tasks].instance_variable_get(:@tasks)
end

#update_context(token_data) ⇒ Object



37
38
39
40
# File 'lib/clacky/rich_ui/components/sidebar.rb', line 37

def update_context(token_data)
  @panels[:context].update_tokens(token_data)
  self
end

#update_plan(text) ⇒ Object



27
28
29
30
# File 'lib/clacky/rich_ui/components/sidebar.rb', line 27

def update_plan(text)
  @panels[:work].update_plan(text)
  self
end

#update_work_activities(activities) ⇒ Object



42
43
44
45
# File 'lib/clacky/rich_ui/components/sidebar.rb', line 42

def update_work_activities(activities)
  @panels[:work].update_activities(activities)
  self
end

#update_work_stats(tasks, cost) ⇒ Object



47
48
49
# File 'lib/clacky/rich_ui/components/sidebar.rb', line 47

def update_work_stats(tasks, cost)
  @panels[:work].update_stats(tasks, cost)
end