Class: BruteCLI::MarkdownRenderer

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

Overview

Renders a growing text buffer through TTY::Markdown, tracking which lines have already been printed so only new output is emitted.

Designed for incremental/streaming use: call #render repeatedly with the full accumulated buffer. Only lines beyond the previous high-water mark are printed. The last line is held back when it does not end with a newline because it may still be incomplete —it will be emitted on the next call once more text arrives, or when #flush forces the final render.

renderer = MarkdownRenderer.new(width: 80)
renderer.render("# Hello\n")        # prints styled heading
renderer.render("# Hello\nWorld\n") # prints only "World" line
renderer.flush                      # no-op (nothing pending)
renderer.reset

Constant Summary collapse

CLEAR_TO_EOL =
"\e[K"

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of MarkdownRenderer.



25
26
27
28
29
30
# File 'lib/brute_cli/markdown_renderer.rb', line 25

def initialize(output: nil, width: nil)
  @output = output
  @width  = width || TTY::Screen.width
  @committed_lines = 0
  @buffer = +""
end

Instance Method Details

#flushObject

Force-render any held-back trailing content (e.g. the last incomplete line). Called at the end of an agent turn.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/brute_cli/markdown_renderer.rb', line 56

def flush
  return if @buffer.empty?

  rendered  = parse_markdown(@buffer)
  lines     = rendered.lines
  new_lines = (lines || [])[@committed_lines..] || []

  return if new_lines.empty?

  out.print "\r"
  out.print CLEAR_TO_EOL
  new_lines.each { |line| out.print line }

  @committed_lines = lines.length
end

#render(buffer) ⇒ Object

Render the full buffer, print only lines beyond what’s already been committed to screen. Holds back the last line if it does not end with a newline (it may be a partial/in-progress line).



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/brute_cli/markdown_renderer.rb', line 35

def render(buffer)
  @buffer = buffer
  rendered  = parse_markdown(buffer)
  lines     = rendered.lines

  # If the source buffer doesn't end with a newline, the last
  # rendered line is potentially incomplete — hold it back.
  printable = buffer.end_with?("\n") ? lines : lines[0...-1]
  new_lines = (printable || [])[@committed_lines..] || []

  return if new_lines.empty?

  out.print "\r"
  out.print CLEAR_TO_EOL
  new_lines.each { |line| out.print line }

  @committed_lines += new_lines.length
end

#resetObject



72
73
74
75
# File 'lib/brute_cli/markdown_renderer.rb', line 72

def reset
  @committed_lines = 0
  @buffer = +""
end