Class: BruteCLI::StreamFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/brute_cli/stream_formatter.rb

Overview

Buffers streamed text and renders it as styled Markdown via TTY::Markdown. Content arrives in small chunks (tokens) via <<. Each chunk is appended to an internal buffer; whenever a newline lands the accumulated buffer is passed through MarkdownRenderer which only emits lines not yet printed to screen.

Call #flush after the agent turn completes to render any remaining content, then #reset before the next turn.

fmt = BruteCLI::StreamFormatter.new(width: 80)
fmt << "# Hello "
fmt << "World\n"   # triggers incremental markdown render
fmt.flush
fmt.reset

Instance Method Summary collapse

Constructor Details

#initialize(output: nil, width: nil) ⇒ StreamFormatter

Returns a new instance of StreamFormatter.



20
21
22
23
24
25
# File 'lib/brute_cli/stream_formatter.rb', line 20

def initialize(output: nil, width: nil)
  @output   = output
  @width    = width || TTY::Screen.width
  @renderer = MarkdownRenderer.new(output: output, width: @width)
  reset
end

Instance Method Details

#<<(text) ⇒ Object

Accept a chunk of streamed text. Accumulate it and, when the chunk contains a newline, re-render the full buffer through TTY::Markdown (the renderer tracks already-printed lines).



30
31
32
33
# File 'lib/brute_cli/stream_formatter.rb', line 30

def <<(text)
  @buffer << text
  render_incremental if text.include?("\n")
end

#flushObject

Finalize any remaining content at the end of an agent turn. Forces a final markdown render pass and ensures output ends with a newline.



38
39
40
41
42
43
# File 'lib/brute_cli/stream_formatter.rb', line 38

def flush
  return if @buffer.empty?

  render_incremental
  out.puts unless @buffer.end_with?("\n")
end

#resetObject

Reset all state for the next agent turn.



46
47
48
49
# File 'lib/brute_cli/stream_formatter.rb', line 46

def reset
  @buffer = +""
  @renderer.reset
end