Module: Legion::CLI::Chat::MarkdownRenderer

Defined in:
lib/legion/cli/chat/markdown_renderer.rb

Constant Summary collapse

BOLD =
"\e[1m"
DIM =
"\e[2m"
ITALIC =
"\e[3m"
RESET =
"\e[0m"
CYAN =
"\e[36m"
GREEN =
"\e[32m"
YELLOW =
"\e[33m"
CODE_BG =
"\e[48;5;236m\e[38;5;253m"
RULE =
"\e[2m#{'' * 40}\e[0m".freeze
CODE_FENCE =
/^```(\w*)\s*$/

Class Method Summary collapse

Class Method Details

.render(text, color: true) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/legion/cli/chat/markdown_renderer.rb', line 22

def render(text, color: true)
  return text unless color

  lines = text.lines
  output = String.new
  i = 0

  while i < lines.length
    line = lines[i]

    if line.match?(CODE_FENCE)
      lang = line.match(CODE_FENCE)[1]
      code_lines = []
      i += 1
      while i < lines.length && !lines[i].match?(/^```\s*$/)
        code_lines << lines[i]
        i += 1
      end
      i += 1 # skip closing ```
      output << render_code_block(code_lines.join, lang)
    else
      output << render_line(line)
      i += 1
    end
  end

  output
end