Module: DataShifter::Internal::Colors

Defined in:
lib/data_shifter/internal/colors.rb

Overview

ANSI color utilities for CLI output. Automatically detects TTY and respects NO_COLOR environment variable.

Constant Summary collapse

CODES =
{
  reset: "\e[0m",
  bold: "\e[1m",
  dim: "\e[2m",
  green: "\e[32m",
  yellow: "\e[33m",
  red: "\e[31m",
  cyan: "\e[36m",
}.freeze

Class Method Summary collapse

Class Method Details

.bold(text, io: $stdout) ⇒ Object



34
35
36
# File 'lib/data_shifter/internal/colors.rb', line 34

def bold(text, io: $stdout)
  wrap(text, :bold, io:)
end

.cyan(text, io: $stdout) ⇒ Object



54
55
56
# File 'lib/data_shifter/internal/colors.rb', line 54

def cyan(text, io: $stdout)
  wrap(text, :cyan, io:)
end

.dim(text, io: $stdout) ⇒ Object



38
39
40
# File 'lib/data_shifter/internal/colors.rb', line 38

def dim(text, io: $stdout)
  wrap(text, :dim, io:)
end

.enabled?(io = $stdout) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
# File 'lib/data_shifter/internal/colors.rb', line 20

def enabled?(io = $stdout)
  return false if ENV["NO_COLOR"]
  return false unless io.respond_to?(:tty?)

  io.tty?
end

.error(text, io: $stdout) ⇒ Object



66
67
68
# File 'lib/data_shifter/internal/colors.rb', line 66

def error(text, io: $stdout)
  wrap(text, :bold, :red, io:)
end

.green(text, io: $stdout) ⇒ Object



42
43
44
# File 'lib/data_shifter/internal/colors.rb', line 42

def green(text, io: $stdout)
  wrap(text, :green, io:)
end

.red(text, io: $stdout) ⇒ Object



50
51
52
# File 'lib/data_shifter/internal/colors.rb', line 50

def red(text, io: $stdout)
  wrap(text, :red, io:)
end

.success(text, io: $stdout) ⇒ Object



58
59
60
# File 'lib/data_shifter/internal/colors.rb', line 58

def success(text, io: $stdout)
  wrap(text, :bold, :green, io:)
end

.warning(text, io: $stdout) ⇒ Object



62
63
64
# File 'lib/data_shifter/internal/colors.rb', line 62

def warning(text, io: $stdout)
  wrap(text, :bold, :yellow, io:)
end

.wrap(text, *styles, io: $stdout) ⇒ Object



27
28
29
30
31
32
# File 'lib/data_shifter/internal/colors.rb', line 27

def wrap(text, *styles, io: $stdout)
  return text unless enabled?(io)

  codes = styles.map { |s| CODES[s] }.compact.join
  "#{codes}#{text}#{CODES[:reset]}"
end

.yellow(text, io: $stdout) ⇒ Object



46
47
48
# File 'lib/data_shifter/internal/colors.rb', line 46

def yellow(text, io: $stdout)
  wrap(text, :yellow, io:)
end