Module: RubyCoded::Chat::Renderer::RichTextInline

Included in:
RichText
Defined in:
lib/ruby_coded/chat/renderer/rich_text_inline.rb

Overview

Inline markdown parsing (code, bold, italic) into ratatui spans.

Instance Method Summary collapse

Instance Method Details

#append_bold_span!(spans, text, role: :assistant) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/ruby_coded/chat/renderer/rich_text_inline.rb', line 61

def append_bold_span!(spans, text, role: :assistant)
  close = text.index("**", 2)
  return nil unless close

  content = text[2...close]
  spans << @tui.span(content: content, style: merge_style(base_text_style(role), modifiers: [:bold]))
  close + 2
end

#append_code_span!(spans, text) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/ruby_coded/chat/renderer/rich_text_inline.rb', line 52

def append_code_span!(spans, text)
  close = text.index("`", 1)
  return nil unless close

  content = text[1...close]
  spans << @tui.span(content: content, style: inline_code_style)
  close + 1
end

#append_italic_span!(spans, text, role: :assistant) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/ruby_coded/chat/renderer/rich_text_inline.rb', line 70

def append_italic_span!(spans, text, role: :assistant)
  close = text.index("*", 1)
  return nil unless close

  content = text[1...close]
  spans << @tui.span(content: content, style: merge_style(base_text_style(role), modifiers: [:italic]))
  close + 1
end

#append_styled_span!(spans, text, role: :assistant) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/ruby_coded/chat/renderer/rich_text_inline.rb', line 44

def append_styled_span!(spans, text, role: :assistant)
  return append_code_span!(spans, text) if text.start_with?("`")
  return append_bold_span!(spans, text, role: role) if text.start_with?("**")
  return append_italic_span!(spans, text, role: role) if text.start_with?("*")

  nil
end

#inline_spans(text, role: :assistant) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ruby_coded/chat/renderer/rich_text_inline.rb', line 8

def inline_spans(text, role: :assistant)
  spans = []
  remaining = text.dup

  while !remaining.empty?
    marker, index = next_inline_marker(remaining)
    unless marker
      spans << @tui.span(content: remaining, style: base_text_style(role))
      break
    end

    prefix = remaining[0...index]
    spans << @tui.span(content: prefix, style: base_text_style(role)) unless prefix.empty?

    consumed = append_styled_span!(spans, remaining[index..], role: role)
    if consumed
      remaining = remaining[(index + consumed)..]
    else
      spans << @tui.span(content: remaining[index], style: base_text_style(role))
      remaining = remaining[(index + 1)..]
    end
  end

  spans = [@tui.span(content: "", style: base_text_style(role))] if spans.empty?
  spans
end

#next_inline_marker(text) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/ruby_coded/chat/renderer/rich_text_inline.rb', line 35

def next_inline_marker(text)
  markers = ["`", "**", "*"]
  positions = markers.filter_map do |marker|
    index = text.index(marker)
    [marker, index] if index
  end
  positions.min_by { |(_, index)| index }
end