Class: Clacky::RichUI::ThinkingLiveView
- Inherits:
-
Object
- Object
- Clacky::RichUI::ThinkingLiveView
- Defined in:
- lib/clacky/rich_ui/components/thinking_live_view.rb
Constant Summary collapse
- SPINNER =
['|', '/', '-', '\\'].freeze
Instance Attribute Summary collapse
-
#height ⇒ Object
Returns the value of attribute height.
-
#start_time ⇒ Object
readonly
Returns the value of attribute start_time.
-
#width ⇒ Object
Returns the value of attribute width.
Instance Method Summary collapse
- #append_text(delta) ⇒ Object
- #desired_height ⇒ Object
- #finish_thinking ⇒ Object
- #idle! ⇒ Object
-
#initialize(shell) ⇒ ThinkingLiveView
constructor
A new instance of ThinkingLiveView.
- #render ⇒ Object
- #start_thinking ⇒ Object
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
#height ⇒ Object
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_time ⇒ Object (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 |
#width ⇒ Object
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_height ⇒ Object
23 24 25 |
# File 'lib/clacky/rich_ui/components/thinking_live_view.rb', line 23 def desired_height @status == :idle ? 0 : 6 end |
#finish_thinking ⇒ Object
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 |
#render ⇒ Object
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_thinking ⇒ Object
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 |