Module: CfScript::UI

Included in:
CfScript, Command, Command::Base, Scope::App, Scope::Base, Scope::Script
Defined in:
lib/cf_script/ui.rb

Defined Under Namespace

Modules: NameTag, NilTag

Constant Summary collapse

TAGS =
{
  error: ['{', '}'],
  trace: ['<', '>'],
  debug: ['(', ')'],
  other: ['[', ']'],
}
COLORS =
{
  success:  { color: :green },
  error:    { color: :red },
  alert:    { color: :yellow },
  info:     { color: :cyan },

  inactive: { color: :light_red },
  active:   { color: :light_green },

  progress: { color: :blue },
  step:     { color: :light_blue },

  title:    { color: :magenta },
  detail:   { color: :light_magenta },

  trace:    { color: :light_black },
  debug:    { color: :white },

  # NOTE: default != no color
  default:  { color: :default },
}
EMOJI =
{
  success:  "\xE2\x9C\x85",
  error:    "\xE2\x9D\x8C",
  alert:    "\xE2\x9A\xA1",
  info:     "\xE2\x9D\x95",

  trace:    "\xE2\x86\xAA",
  debug:    "\xE2\x9A\xAA",

  default:  "\xE2\x9E\x96",

  lobster_bisque:   "🍲", # h/t @dbelloti
}

Class Method Summary collapse

Class Method Details

.alert(tag, message) ⇒ Object Also known as: ui_alert



137
138
139
# File 'lib/cf_script/ui.rb', line 137

def alert(tag, message)
  puts_out ui_format(call_type(__callee__), tag, message)
end

.call_type(callee) ⇒ Object



125
126
127
# File 'lib/cf_script/ui.rb', line 125

def call_type(callee)
  callee.to_s =~ /\Aui_(.+)\z/ ? $1.to_sym : callee
end

.debug(tag, message) ⇒ Object Also known as: ui_debug



168
169
170
171
# File 'lib/cf_script/ui.rb', line 168

def debug(tag, message)
  puts_err ui_format(call_type(__callee__), tag, message.inspect)
 #binding.pry; __pry__.???
end

.detail(tag, message, indent = 1) ⇒ Object Also known as: ui_detail



157
158
159
# File 'lib/cf_script/ui.rb', line 157

def detail(tag, message, indent = 1)
  puts_out ui_format(call_type(__callee__), tag, "#{'  ' * indent}#{message}")
end

.emoji(type) ⇒ Object



75
76
77
# File 'lib/cf_script/ui.rb', line 75

def emoji(type)
  "#{(EMOJI.key?(type) ? EMOJI[type] : EMOJI[:default])} "
end

.emoji_for(type) ⇒ Object



79
80
81
# File 'lib/cf_script/ui.rb', line 79

def emoji_for(type)
  emoji(type) if CfScript.config.ui.emoji
end

.error(tag, message) ⇒ Object Also known as: ui_error



133
134
135
# File 'lib/cf_script/ui.rb', line 133

def error(tag, message)
  puts_err ui_format(call_type(__callee__), tag, message)
end

.info(tag, message) ⇒ Object Also known as: ui_info



141
142
143
# File 'lib/cf_script/ui.rb', line 141

def info(tag, message)
  puts_out ui_format(call_type(__callee__), tag, message)
end


66
67
68
69
# File 'lib/cf_script/ui.rb', line 66

def print_err(text)
  CfScript.stderr.print text
  nil # Make it explicit
end


56
57
58
59
# File 'lib/cf_script/ui.rb', line 56

def print_out(text)
  CfScript.stdout.print text
  nil # Make it explicit
end

.progress(tag, message) ⇒ Object Also known as: ui_progress



145
146
147
# File 'lib/cf_script/ui.rb', line 145

def progress(tag, message)
  puts_out ui_format(call_type(__callee__), tag, message)
end

.puts_err(text) ⇒ Object



61
62
63
64
# File 'lib/cf_script/ui.rb', line 61

def puts_err(text)
  CfScript.stderr.puts text
  nil # Make it explicit
end

.puts_out(text) ⇒ Object



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

def puts_out(text)
  CfScript.stdout.puts text
  nil # Make it explicit
end

.step(tag, message) ⇒ Object Also known as: ui_step



149
150
151
# File 'lib/cf_script/ui.rb', line 149

def step(tag, message)
  puts_out ui_format(call_type(__callee__), tag, message)
end

.success(tag, message) ⇒ Object Also known as: ui_success



129
130
131
# File 'lib/cf_script/ui.rb', line 129

def success(tag, message)
  puts_out ui_format(call_type(__callee__), tag, message)
end

.tag_char(type, which) ⇒ Object



91
92
93
# File 'lib/cf_script/ui.rb', line 91

def tag_char(type, which)
  (TAGS.key?(type) ? TAGS[type] : TAGS[:other]).send(which)
end

.tag_close(type = :default) ⇒ Object



99
100
101
# File 'lib/cf_script/ui.rb', line 99

def tag_close(type = :default)
  tag_char(tag_style(type), :last)
end

.tag_color(type = :default) ⇒ Object



87
88
89
# File 'lib/cf_script/ui.rb', line 87

def tag_color(type = :default)
  type
end

.tag_format(tag, type = :default) ⇒ Object Also known as: ui_tag_format



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/cf_script/ui.rb', line 103

def tag_format(tag, type = :default)
  if tag && CfScript.config.ui.tags
    list = []

    list << tag_open(type)
    list << tag
    list << tag_close(type)

    list.join.colorize with_color_of(tag_color(type))
  end
end

.tag_open(type = :default) ⇒ Object



95
96
97
# File 'lib/cf_script/ui.rb', line 95

def tag_open(type = :default)
  tag_char(tag_style(type), :first)
end

.tag_style(type = :default) ⇒ Object



83
84
85
# File 'lib/cf_script/ui.rb', line 83

def tag_style(type = :default)
  type
end

.title(tag, message, indent = 0) ⇒ Object Also known as: ui_title



153
154
155
# File 'lib/cf_script/ui.rb', line 153

def title(tag, message, indent = 0)
  puts_out ui_format(call_type(__callee__), tag, "#{'  ' * indent}#{message}")
end

.trace(tag, message, use_print = false) ⇒ Object Also known as: ui_trace



161
162
163
164
165
166
# File 'lib/cf_script/ui.rb', line 161

def trace(tag, message, use_print = false)
  if CfScript.config.runtime.trace
    text = ui_format(call_type(__callee__), tag, message)
    use_print ? print_out(text) : puts_out(text)
  end
end

.ui_format(type, tag, message, tag_type = nil) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/cf_script/ui.rb', line 115

def ui_format(type, tag, message, tag_type = nil)
  list = []

  list << emoji_for(type)
  list << tag_format(tag, tag_type ? tag_type : type)
  list << message.colorize(with_color_of(type))

  list.compact.reject(&:empty?).join(' ')
end

.with_color_of(type = :default) ⇒ Object



71
72
73
# File 'lib/cf_script/ui.rb', line 71

def with_color_of(type = :default)
  COLORS[CfScript.config.ui.color ? type : :default]
end