Module: AIGit::UI

Defined in:
lib/ai_git/ui.rb

Overview

Terminal output helpers: ANSI colors (auto-disabled when output is not a TTY or NO_COLOR is set) and key/value lines.

Constant Summary collapse

CODES =
{
  bold: 1, dim: 2, red: 31, green: 32, yellow: 33, blue: 34, cyan: 36, gray: 90
}.freeze

Class Method Summary collapse

Class Method Details

.bold(text) ⇒ Object



31
32
33
# File 'lib/ai_git/ui.rb', line 31

def bold(text)
  paint(text, :bold)
end

.color?Boolean

Colors are on only for an interactive terminal and when the user has not opted out via NO_COLOR (https://no-color.org) or AI_GIT_NO_COLOR.

Returns:

  • (Boolean)


15
16
17
18
19
20
# File 'lib/ai_git/ui.rb', line 15

def color?
  return false if ENV["NO_COLOR"] && !ENV["NO_COLOR"].empty?
  return false if ENV["AI_GIT_NO_COLOR"] && !ENV["AI_GIT_NO_COLOR"].empty?

  $stdout.tty?
end

.dim(text) ⇒ Object



35
36
37
# File 'lib/ai_git/ui.rb', line 35

def dim(text)
  paint(text, :dim)
end

.error(text) ⇒ Object



55
56
57
# File 'lib/ai_git/ui.rb', line 55

def error(text)
  $stderr.puts paint(text, :red) # rubocop:disable Style/StderrPuts
end

.heading(text) ⇒ Object



43
44
45
# File 'lib/ai_git/ui.rb', line 43

def heading(text)
  puts paint(text, :bold, :cyan)
end

.info(text) ⇒ Object



47
48
49
# File 'lib/ai_git/ui.rb', line 47

def info(text)
  puts text
end

.kv(key, value) ⇒ Object



39
40
41
# File 'lib/ai_git/ui.rb', line 39

def kv(key, value)
  puts "#{paint("#{key}:", :bold)} #{value}"
end

.paint(text, *styles) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/ai_git/ui.rb', line 22

def paint(text, *styles)
  return text.to_s unless color?

  codes = styles.map { |style| CODES[style] }.compact
  return text.to_s if codes.empty?

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

.success(text) ⇒ Object



51
52
53
# File 'lib/ai_git/ui.rb', line 51

def success(text)
  puts paint(text, :green)
end