Module: Commiti::TerminalUI

Defined in:
lib/services/helpers/terminal_ui.rb

Constant Summary collapse

COLORS =
{
  green: 32,
  red: 31,
  yellow: 33,
  blue: 34,
  cyan: 36,
  gray: 90,
  bold: 1
}.freeze
SYMBOLS =
{
  success: '',
  fail: '',
  info: '',
  warn: ''
}.freeze

Class Method Summary collapse

Class Method Details

.color(text, *styles) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/services/helpers/terminal_ui.rb', line 30

def self.color(text, *styles)
  return text unless supports_ansi?

  codes = styles.filter_map { |style| COLORS[style] }
  return text if codes.empty?

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

.header(text) ⇒ Object



54
55
56
# File 'lib/services/helpers/terminal_ui.rb', line 54

def self.header(text)
  color(text, :bold, :cyan)
end

.separator(length = 60) ⇒ Object



50
51
52
# File 'lib/services/helpers/terminal_ui.rb', line 50

def self.separator(length = 60)
  color('' * length, :gray)
end

.status(kind, text) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/services/helpers/terminal_ui.rb', line 39

def self.status(kind, text)
  symbol = SYMBOLS.fetch(kind, '*')
  color_style = case kind
                when :success then :green
                when :fail then :red
                when :warn then :yellow
                else :blue
                end
  "#{color(symbol, color_style)} #{text}"
end

.supports_ansi?Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
# File 'lib/services/helpers/terminal_ui.rb', line 22

def self.supports_ansi?
  return false unless $stdout.tty?
  return false if ENV.key?('NO_COLOR')

  term = ENV.fetch('TERM', '').downcase
  term != 'dumb'
end