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
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
- .alert(tag, message) ⇒ Object (also: ui_alert)
- .call_type(callee) ⇒ Object
- .debug(tag, message) ⇒ Object (also: ui_debug)
- .detail(tag, message, indent = 1) ⇒ Object (also: ui_detail)
- .emoji(type) ⇒ Object
- .emoji_for(type) ⇒ Object
- .error(tag, message) ⇒ Object (also: ui_error)
- .info(tag, message) ⇒ Object (also: ui_info)
- .print_err(text) ⇒ Object
- .print_out(text) ⇒ Object
- .progress(tag, message) ⇒ Object (also: ui_progress)
- .puts_err(text) ⇒ Object
- .puts_out(text) ⇒ Object
- .step(tag, message) ⇒ Object (also: ui_step)
- .success(tag, message) ⇒ Object (also: ui_success)
- .tag_char(type, which) ⇒ Object
- .tag_close(type = :default) ⇒ Object
- .tag_color(type = :default) ⇒ Object
- .tag_format(tag, type = :default) ⇒ Object (also: ui_tag_format)
- .tag_open(type = :default) ⇒ Object
- .tag_style(type = :default) ⇒ Object
- .title(tag, message, indent = 0) ⇒ Object (also: ui_title)
- .trace(tag, message, use_print = false) ⇒ Object (also: ui_trace)
- .ui_format(type, tag, message, tag_type = nil) ⇒ Object
- .with_color_of(type = :default) ⇒ Object
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, ) puts_out ui_format(call_type(__callee__), tag, ) 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, ) puts_err ui_format(call_type(__callee__), tag, .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, , indent = 1) puts_out ui_format(call_type(__callee__), tag, "#{' ' * indent}#{}") 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, ) puts_err ui_format(call_type(__callee__), tag, ) end |
.info(tag, message) ⇒ Object Also known as: ui_info
141 142 143 |
# File 'lib/cf_script/ui.rb', line 141 def info(tag, ) puts_out ui_format(call_type(__callee__), tag, ) end |
.print_err(text) ⇒ Object
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 |
.print_out(text) ⇒ Object
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, ) puts_out ui_format(call_type(__callee__), tag, ) 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, ) puts_out ui_format(call_type(__callee__), tag, ) end |
.success(tag, message) ⇒ Object Also known as: ui_success
129 130 131 |
# File 'lib/cf_script/ui.rb', line 129 def success(tag, ) puts_out ui_format(call_type(__callee__), tag, ) 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. 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, , indent = 0) puts_out ui_format(call_type(__callee__), tag, "#{' ' * indent}#{}") 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, , use_print = false) if CfScript.config.runtime.trace text = ui_format(call_type(__callee__), tag, ) 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, , tag_type = nil) list = [] list << emoji_for(type) list << tag_format(tag, tag_type ? tag_type : type) list << .colorize(with_color_of(type)) list.compact.reject(&:empty?).join(' ') end |