Skip to content
Kward Search API index

Module: Kward::ANSI

Defined in:
lib/kward/ansi.rb

Overview

ANSI SGR styling and terminal-text helpers.

Terminal control output sequences live in TerminalSequences, and input key sequences live in TerminalKeys. This module owns text-level concerns: colorizing strings, stripping/sanitizing escape sequences, visible wrapping, and lightweight Markdown rendering for terminal output.

Defined Under Namespace

Classes: MarkdownStream

Constant Summary collapse

SGR_PATTERN =
/\e\[[0-9;:]*m/.freeze
STYLES =
{
  reset: 0,
  bold: 1,
  dim: 2,
  italic: 3,
  strikethrough: 9,
  red: 31,
  green: 32,
  yellow: 33,
  blue: 34,
  magenta: 35,
  cyan: 36,
  gray: 90,
  grey: 90,
  primary_green: "38;2;138;160;106",
  bright_accent_green: "38;2;155;255;0",
  augen: "38;2;155;255;0",
  dark_forest_green: "38;2;78;88;53",
  stone: "38;2;196;192;178",
  metal_dark: "38;2;42;42;42",
  background: "38;2;22;24;22"
}.freeze

Class Method Summary collapse

Class Method Details

.blockquote(text, enabled: enabled?) ) ⇒ Object



250
251
252
# File 'lib/kward/ansi.rb', line 250

def blockquote(text, enabled: enabled?)
  "#{colorize("", :gray, enabled: enabled)} #{inline_markdown(text, enabled: enabled)}"
end

.colorize(text, *styles, enabled: enabled?) ) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/kward/ansi.rb', line 46

def colorize(text, *styles, enabled: enabled?)
  string = text.to_s
  return string unless enabled

  codes = styles.flatten.map { |style| STYLES.fetch(style, style) }.compact
  return string if codes.empty?

  "\e[#{codes.join(";")}m#{string}\e[0m"
end

.disabled_color?(env) ⇒ Boolean

Returns:

  • (Boolean)


311
312
313
314
315
316
# File 'lib/kward/ansi.rb', line 311

def disabled_color?(env)
  return true if env.key?("NO_COLOR") && !env["NO_COLOR"].to_s.empty?
  return true if env["CLICOLOR"] == "0"

  env["TERM"] == "dumb"
end

.enabled?(output = $stdout, env: ENV) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
# File 'lib/kward/ansi.rb', line 36

def enabled?(output = $stdout, env: ENV)
  setting = env["KWARD_COLOR"].to_s.downcase
  return true if %w[always force forced true yes 1].include?(setting)
  return false if %w[never false no 0].include?(setting)
  return true if forced_color?(env)
  return false if disabled_color?(env)

  output.respond_to?(:tty?) && output.tty?
end

.escape_sequence_at(string, index) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/kward/ansi.rb', line 130

def escape_sequence_at(string, index)
  chunk = string[index..]
  chunk.match(/\A\e\][^\a]*(?:\a|\e\\)/m)&.[](0) ||
    chunk.match(/\A\e[P_X^][\s\S]*?\e\\/m)&.[](0) ||
    chunk.match(/\A\e\[[0-9;:?]*[ -\/]*[@-~]/)&.[](0) ||
    chunk[0, 2]
end

.forced_color?(env) ⇒ Boolean

Returns:

  • (Boolean)


305
306
307
308
309
# File 'lib/kward/ansi.rb', line 305

def forced_color?(env)
  force_color = env["FORCE_COLOR"]
  clicolor_force = env["CLICOLOR_FORCE"]
  (force_color && force_color != "0") || (clicolor_force && clicolor_force != "0")
end

.inline_bold(text, enabled: enabled?) ) ⇒ Object



280
281
282
283
284
# File 'lib/kward/ansi.rb', line 280

def inline_bold(text, enabled: enabled?)
  text.gsub(/\*\*([^\n]+?)\*\*/) do
    colorize(Regexp.last_match(1), :bold, enabled: enabled)
  end
end

.inline_code(line, enabled: enabled?) ) ⇒ Object



301
302
303
# File 'lib/kward/ansi.rb', line 301

def inline_code(line, enabled: enabled?)
  inline_markdown(line, enabled: enabled)
end

.inline_emphasis(text, enabled: enabled?) ) ⇒ Object



274
275
276
277
278
# File 'lib/kward/ansi.rb', line 274

def inline_emphasis(text, enabled: enabled?)
  rendered = inline_bold(text, enabled: enabled)
  rendered = inline_strikethrough(rendered, enabled: enabled)
  inline_italic(rendered, enabled: enabled)
end

.inline_italic(text, enabled: enabled?) ) ⇒ Object



292
293
294
295
296
297
298
299
# File 'lib/kward/ansi.rb', line 292

def inline_italic(text, enabled: enabled?)
  rendered = text.gsub(/(^|[\s\(\[{])\*([^*\n]+?)\*(?=$|[\s\)\]},.!?:;])/) do
    "#{Regexp.last_match(1)}#{colorize(Regexp.last_match(2), :italic, enabled: enabled)}"
  end
  rendered.gsub(/(^|[\s\(\[{])_([^_\n]+?)_(?=$|[\s\)\]},.!?:;])/) do
    "#{Regexp.last_match(1)}#{colorize(Regexp.last_match(2), :italic, enabled: enabled)}"
  end
end


264
265
266
267
268
269
270
271
272
# File 'lib/kward/ansi.rb', line 264

def inline_links(text, enabled: enabled?)
  text.split(/(\[[^\]\n]+\]\([^)\s\n]+\))/).map do |part|
    if (match = part.match(/\A\[([^\]\n]+)\]\(([^)\s\n]+)\)\z/))
      "#{colorize(match[1], :cyan, enabled: enabled)} (#{colorize(match[2], :dim, enabled: enabled)})"
    else
      inline_emphasis(part, enabled: enabled)
    end
  end.join
end

.inline_markdown(line, enabled: enabled?) ) ⇒ Object



254
255
256
257
258
259
260
261
262
# File 'lib/kward/ansi.rb', line 254

def inline_markdown(line, enabled: enabled?)
  line.to_s.split(/(`[^`\n]+`)/).map do |part|
    if part.start_with?("`") && part.end_with?("`") && part.length > 1
      "`#{colorize(part[1...-1], :dim, enabled: enabled)}`"
    else
      inline_links(part, enabled: enabled)
    end
  end.join
end

.inline_strikethrough(text, enabled: enabled?) ) ⇒ Object



286
287
288
289
290
# File 'lib/kward/ansi.rb', line 286

def inline_strikethrough(text, enabled: enabled?)
  text.gsub(/~~([^\n]+?)~~/) do
    colorize(Regexp.last_match(1), :strikethrough, enabled: enabled)
  end
end

.markdown(text, enabled: enabled?) ) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/kward/ansi.rb', line 138

def markdown(text, enabled: enabled?)
  string = text.to_s
  lines = string.lines(chomp: true)
  rendered = []
  in_fence = false

  lines.each do |line|
    if (match = line.match(/\A\s*```([^`]*)\s*\z/))
      if in_fence
        rendered << colorize("" + "" * 39, :gray, enabled: enabled)
        in_fence = false
      else
        language = match[1].to_s.strip
        label = language.empty? ? "code" : "code #{language}"
        rendered << colorize("┌─ #{label}", :gray, enabled: enabled)
        in_fence = true
      end
      next
    end

    if in_fence
      rendered << colorize("#{line}", :dim, enabled: enabled)
    else
      rendered << markdown_line(line, enabled: enabled)
    end
  end

  rendered << colorize("" + "" * 39, :gray, enabled: enabled) if in_fence
  rendered.join("\n") + (string.end_with?("\n") ? "\n" : "")
end

.markdown_heading(marker, text, enabled: enabled?) ) ⇒ Object



240
241
242
# File 'lib/kward/ansi.rb', line 240

def markdown_heading(marker, text, enabled: enabled?)
  "#{marker}#{colorize(text, :bold, enabled: enabled)}"
end

.markdown_line(line, enabled: enabled?) ) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
# File 'lib/kward/ansi.rb', line 228

def markdown_line(line, enabled: enabled?)
  if (match = line.match(/\A(\#{1,6}\s+)(.+)\z/))
    markdown_heading(match[1], match[2], enabled: enabled)
  elsif (match = line.match(/\A(\s*)[-*]\s+\[([ xX])\]\s+(.+)\z/))
    task_list_item(match[1], match[2], match[3], enabled: enabled)
  elsif (match = line.match(/\A>\s?(.*)\z/))
    blockquote(match[1], enabled: enabled)
  else
    inline_markdown(line, enabled: enabled)
  end
end

.sanitize_transcript(text) ⇒ Object

Drops unsafe terminal controls from transcript text while preserving SGR color.



68
69
70
71
72
73
74
75
76
# File 'lib/kward/ansi.rb', line 68

def sanitize_transcript(text)
  scan_escape_tokens(text).each_with_object(+"") do |token, sanitized|
    if token[:escape]
      sanitized << token[:text] if token[:text].match?(SGR_PATTERN)
    else
      sanitized << token[:text]
    end
  end
end

.scan_escape_tokens(text) ⇒ Object

Splits text into visible chunks and terminal escape sequence chunks.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/kward/ansi.rb', line 112

def scan_escape_tokens(text)
  string = text.to_s
  tokens = []
  index = 0
  while index < string.length
    if string[index] == "\e" && (escape = escape_sequence_at(string, index))
      tokens << { text: escape, escape: true }
      index += escape.length
      next
    end

    next_escape = string.index("\e", index) || string.length
    tokens << { text: string[index...next_escape], escape: false } if next_escape > index
    index = next_escape
  end
  tokens
end

.strip(text) ⇒ Object



56
57
58
# File 'lib/kward/ansi.rb', line 56

def strip(text)
  strip_control_sequences(text)
end

.strip_control_sequences(text) ⇒ Object

Removes terminal escape/control sequences while preserving visible text.



61
62
63
64
65
# File 'lib/kward/ansi.rb', line 61

def strip_control_sequences(text)
  scan_escape_tokens(text).each_with_object(+"") do |token, stripped|
    stripped << token[:text] unless token[:escape]
  end
end

.task_list_item(indent, marker, text, enabled: enabled?) ) ⇒ Object



244
245
246
247
248
# File 'lib/kward/ansi.rb', line 244

def task_list_item(indent, marker, text, enabled: enabled?)
  checked = marker.downcase == "x"
  box = checked ? colorize("", :green, enabled: enabled) : colorize("", :gray, enabled: enabled)
  "#{indent}#{box} #{inline_markdown(text, enabled: enabled)}"
end

.wrap_visible(text, width) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/kward/ansi.rb', line 78

def wrap_visible(text, width)
  line_width = [width.to_i, 1].max
  rows = []
  current = +""
  visible_width = 0

  scan_escape_tokens(text).each do |token|
    if token[:escape]
      next unless token[:text].match?(SGR_PATTERN)

      if current.empty? && rows.any?
        rows[-1] << token[:text]
      else
        current << token[:text]
      end
      next
    end

    token[:text].each_char do |char|
      current << char
      visible_width += 1
      if visible_width >= line_width
        rows << current
        current = +""
        visible_width = 0
      end
    end
  end

  rows << current unless current.empty?
  rows
end