Class: Kward::PromptInterface::StreamState
- Inherits:
-
Object
- Object
- Kward::PromptInterface::StreamState
- Defined in:
- lib/kward/prompt_interface/stream_state.rb
Overview
State object for streamed assistant output blocks.
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#col ⇒ Object
readonly
Returns the value of attribute col.
Instance Method Summary collapse
- #clear_pending_wrap ⇒ Object
- #finish_block ⇒ Object
-
#initialize ⇒ StreamState
constructor
A new instance of StreamState.
- #initialize_copy(source) ⇒ Object
- #pending_wrap? ⇒ Boolean
- #reset ⇒ Object
- #reset_position_from_rows(rows, width) ⇒ Object
- #start_block(label) ⇒ Object
- #update_position(text, width:) ⇒ Object
Constructor Details
#initialize ⇒ StreamState
Returns a new instance of StreamState.
9 10 11 |
# File 'lib/kward/prompt_interface/stream_state.rb', line 9 def initialize reset end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
7 8 9 |
# File 'lib/kward/prompt_interface/stream_state.rb', line 7 def block @block end |
#col ⇒ Object (readonly)
Returns the value of attribute col.
7 8 9 |
# File 'lib/kward/prompt_interface/stream_state.rb', line 7 def col @col end |
Instance Method Details
#clear_pending_wrap ⇒ Object
49 50 51 52 |
# File 'lib/kward/prompt_interface/stream_state.rb', line 49 def clear_pending_wrap @col = 0 @pending_wrap = false end |
#finish_block ⇒ Object
30 31 32 |
# File 'lib/kward/prompt_interface/stream_state.rb', line 30 def finish_block @block = nil end |
#initialize_copy(source) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/kward/prompt_interface/stream_state.rb', line 13 def initialize_copy(source) super @block = source.block&.dup @col = source.col @pending_wrap = source.pending_wrap? end |
#pending_wrap? ⇒ Boolean
34 35 36 |
# File 'lib/kward/prompt_interface/stream_state.rb', line 34 def pending_wrap? @pending_wrap end |
#reset ⇒ Object
20 21 22 23 24 |
# File 'lib/kward/prompt_interface/stream_state.rb', line 20 def reset @block = nil @col = 0 @pending_wrap = false end |
#reset_position_from_rows(rows, width) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/kward/prompt_interface/stream_state.rb', line 38 def reset_position_from_rows(rows, width) last_length = rows.empty? ? 0 : ANSI.strip(rows.last).length if last_length >= width @col = 0 @pending_wrap = true else @col = last_length @pending_wrap = false end end |
#start_block(label) ⇒ Object
26 27 28 |
# File 'lib/kward/prompt_interface/stream_state.rb', line 26 def start_block(label) @block = label end |
#update_position(text, width:) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/kward/prompt_interface/stream_state.rb', line 54 def update_position(text, width:) ANSI.strip(text).each_char do |char| case char when "\n", "\r" @col = 0 @pending_wrap = false else @pending_wrap = false @col += 1 if @col >= width @col = 0 @pending_wrap = true end end end end |