Class: Clacky::RichUI::ThinkingLiveView

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

Constant Summary collapse

SPINNER =
['|', '/', '-', '\\'].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(shell) ⇒ ThinkingLiveView

Returns a new instance of ThinkingLiveView.



13
14
15
16
17
18
19
20
21
# File 'lib/clacky/rich_ui/components/thinking_live_view.rb', line 13

def initialize(shell)
  @shell = shell
  @status = :idle     # :idle, :thinking, :done
  @text = +""
  @start_time = nil
  @spinner_index = 0
  @width = 0
  @height = 0
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



10
11
12
# File 'lib/clacky/rich_ui/components/thinking_live_view.rb', line 10

def height
  @height
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



11
12
13
# File 'lib/clacky/rich_ui/components/thinking_live_view.rb', line 11

def start_time
  @start_time
end

#widthObject

Returns the value of attribute width.



10
11
12
# File 'lib/clacky/rich_ui/components/thinking_live_view.rb', line 10

def width
  @width
end

Instance Method Details

#append_text(delta) ⇒ Object



34
35
36
37
# File 'lib/clacky/rich_ui/components/thinking_live_view.rb', line 34

def append_text(delta)
  @text << delta.to_s
  @shell.live&.refresh
end

#desired_heightObject



23
24
25
# File 'lib/clacky/rich_ui/components/thinking_live_view.rb', line 23

def desired_height
  @status == :idle ? 0 : 6
end

#finish_thinkingObject



39
40
41
42
# File 'lib/clacky/rich_ui/components/thinking_live_view.rb', line 39

def finish_thinking
  @status = :done
  @shell.live&.refresh
end

#idle!Object



44
45
46
47
48
49
# File 'lib/clacky/rich_ui/components/thinking_live_view.rb', line 44

def idle!
  @status = :idle
  @text = +""
  @start_time = nil
  @shell.live&.refresh
end

#renderObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/clacky/rich_ui/components/thinking_live_view.rb', line 51

def render
  theme = @shell.theme
  case @status
  when :idle
    [""]
  when :thinking
    elapsed = @start_time ? (Time.now - @start_time).round(1) : 0.0
    @spinner_index = (@spinner_index + 1) % SPINNER.length
    spinner = theme.style(SPINNER[@spinner_index], :thinking)
    time_str = theme.style("#{elapsed}s", :accent)
    header = " #{spinner} #{theme.style("Thinking", :thinking)}  #{time_str}"
    lines = [header]
    visible = @text.to_s.split("\n").last(5)
    visible.each { |l| lines << "  #{theme.style(l, :thinking)}" }
    (5 - visible.length).times { lines << "" }
    lines
  when :done
    elapsed = @start_time ? (Time.now - @start_time).round(1) : 0.0
    header = " #{theme.style("Thinking done", :thinking)}  #{theme.style("#{elapsed}s", :accent)}"
    lines = [header]
    visible = @text.to_s.split("\n").last(4)
    visible.each { |l| lines << "  #{theme.style(l, :muted)}" }
    (4 - visible.length).times { lines << "" }
    lines
  end
end

#start_thinkingObject



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

def start_thinking
  @status = :thinking
  @start_time = Time.now
  @text = +""
  @shell.live&.refresh
end