Class: Kward::PromptInterface::StreamState

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/prompt_interface/stream_state.rb

Overview

State object for streamed assistant output blocks.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStreamState

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

#blockObject (readonly)

Returns the value of attribute block.



7
8
9
# File 'lib/kward/prompt_interface/stream_state.rb', line 7

def block
  @block
end

#colObject (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_wrapObject



42
43
44
45
# File 'lib/kward/prompt_interface/stream_state.rb', line 42

def clear_pending_wrap
  @col = 0
  @pending_wrap = false
end

#finish_blockObject



23
24
25
# File 'lib/kward/prompt_interface/stream_state.rb', line 23

def finish_block
  @block = nil
end

#pending_wrap?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/kward/prompt_interface/stream_state.rb', line 27

def pending_wrap?
  @pending_wrap
end

#resetObject



13
14
15
16
17
# File 'lib/kward/prompt_interface/stream_state.rb', line 13

def reset
  @block = nil
  @col = 0
  @pending_wrap = false
end

#reset_position_from_rows(rows, width) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/kward/prompt_interface/stream_state.rb', line 31

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



19
20
21
# File 'lib/kward/prompt_interface/stream_state.rb', line 19

def start_block(label)
  @block = label
end

#update_position(text, width:) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/kward/prompt_interface/stream_state.rb', line 47

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