Class: I18nJS::CLI::UI

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n-js/cli/ui.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdout:, stderr:, colored: nil) ⇒ UI

Returns a new instance of UI.



9
10
11
12
13
# File 'lib/i18n-js/cli/ui.rb', line 9

def initialize(stdout:, stderr:, colored: nil)
  @stdout = stdout
  @stderr = stderr
  @colored = colored
end

Instance Attribute Details

#coloredObject

Returns the value of attribute colored.



7
8
9
# File 'lib/i18n-js/cli/ui.rb', line 7

def colored
  @colored
end

#stderrObject (readonly)

Returns the value of attribute stderr.



6
7
8
# File 'lib/i18n-js/cli/ui.rb', line 6

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



6
7
8
# File 'lib/i18n-js/cli/ui.rb', line 6

def stdout
  @stdout
end

Instance Method Details

#ansi(text, code) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/i18n-js/cli/ui.rb', line 51

def ansi(text, code)
  if colored?
    "\e[#{code}m#{text}\e[0m"
  else
    text
  end
end

#colored?Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
# File 'lib/i18n-js/cli/ui.rb', line 41

def colored?
  colored_output = if colored.nil?
                     stdout.tty?
                   else
                     colored
                   end

  colored_output && !no_color?
end

#exit_with(*message) ⇒ Object



28
29
30
31
# File 'lib/i18n-js/cli/ui.rb', line 28

def exit_with(*message)
  stdout_print(message)
  exit(0)
end

#fail_with(*message) ⇒ Object



23
24
25
26
# File 'lib/i18n-js/cli/ui.rb', line 23

def fail_with(*message)
  stderr_print(message)
  exit(1)
end

#no_color?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/i18n-js/cli/ui.rb', line 59

def no_color?
  !ENV["NO_COLOR"].nil? && ENV["NO_COLOR"] == "1"
end

#red(text) ⇒ Object



37
38
39
# File 'lib/i18n-js/cli/ui.rb', line 37

def red(text)
  ansi(text, 31)
end

#stderr_print(*message) ⇒ Object



19
20
21
# File 'lib/i18n-js/cli/ui.rb', line 19

def stderr_print(*message)
  stderr << "#{message.join(' ')}\n"
end

#stdout_print(*message) ⇒ Object



15
16
17
# File 'lib/i18n-js/cli/ui.rb', line 15

def stdout_print(*message)
  stdout << "#{message.join(' ')}\n"
end

#yellow(text) ⇒ Object



33
34
35
# File 'lib/i18n-js/cli/ui.rb', line 33

def yellow(text)
  ansi(text, 33)
end