Module: Spoom::Cli::Helper
Constant Summary collapse
- HIGHLIGHT_COLOR =
Color used to highlight expressions in backticks
T.let(Spoom::Color::BLUE, Spoom::Color)
Instance Method Summary collapse
- #blue(string) ⇒ Object
- #color? ⇒ Boolean
- #colorize(string, *color) ⇒ Object
- #context ⇒ Object
- #context_requiring_sorbet! ⇒ Object
- #cyan(string) ⇒ Object
- #exec_path ⇒ Object
- #gray(string) ⇒ Object
- #green(string) ⇒ Object
- #highlight(string) ⇒ Object
- #red(string) ⇒ Object
- #say(message) ⇒ Object
- #say_error(message, status: "Error", nl: true) ⇒ Object
- #say_warning(message, status: "Warning", nl: true) ⇒ Object
- #yellow(string) ⇒ Object
Methods included from Spoom::Colorize
Instance Method Details
#blue(string) ⇒ Object
139 140 141 |
# File 'lib/spoom/cli/helper.rb', line 139 def blue(string) colorize(string, Color::BLUE) end |
#color? ⇒ Boolean
103 104 105 |
# File 'lib/spoom/cli/helper.rb', line 103 def color? [:color] end |
#colorize(string, *color) ⇒ Object
132 133 134 135 136 |
# File 'lib/spoom/cli/helper.rb', line 132 def colorize(string, *color) return string unless color? T.unsafe(self).set_color(string, *color) end |
#context ⇒ Object
71 72 73 |
# File 'lib/spoom/cli/helper.rb', line 71 def context @context ||= T.let(Context.new(exec_path), T.nilable(Context)) end |
#context_requiring_sorbet! ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/spoom/cli/helper.rb', line 77 def context_requiring_sorbet! context = self.context unless context.has_sorbet_config? say_error( "not in a Sorbet project (`#{Spoom::Sorbet::CONFIG_PATH}` not found)\n\n" \ "When running spoom from another path than the project's root, " \ "use `--path PATH` to specify the path to the root.", ) Kernel.exit(1) end context end |
#cyan(string) ⇒ Object
144 145 146 |
# File 'lib/spoom/cli/helper.rb', line 144 def cyan(string) colorize(string, Color::CYAN) end |
#exec_path ⇒ Object
92 93 94 |
# File 'lib/spoom/cli/helper.rb', line 92 def exec_path [:path] end |
#gray(string) ⇒ Object
149 150 151 |
# File 'lib/spoom/cli/helper.rb', line 149 def gray(string) colorize(string, Color::LIGHT_BLACK) end |
#green(string) ⇒ Object
154 155 156 |
# File 'lib/spoom/cli/helper.rb', line 154 def green(string) colorize(string, Color::GREEN) end |
#highlight(string) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/spoom/cli/helper.rb', line 108 def highlight(string) return string unless color? res = StringIO.new word = StringIO.new in_ticks = T.let(false, T::Boolean) string.chars.each do |c| if c == "`" && !in_ticks in_ticks = true elsif c == "`" && in_ticks in_ticks = false res << colorize(word.string, HIGHLIGHT_COLOR) word = StringIO.new elsif in_ticks word << c else res << c end end res.string end |
#red(string) ⇒ Object
159 160 161 |
# File 'lib/spoom/cli/helper.rb', line 159 def red(string) colorize(string, Color::RED) end |
#say(message) ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/spoom/cli/helper.rb', line 20 def say() buffer = StringIO.new buffer << highlight() buffer << "\n" unless .end_with?("\n") $stdout.print(buffer.string) $stdout.flush end |
#say_error(message, status: "Error", nl: true) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/spoom/cli/helper.rb', line 39 def say_error(, status: "Error", nl: true) buffer = StringIO.new buffer << "#{red(status)}: " if status buffer << highlight() buffer << "\n" if nl && !.end_with?("\n") $stderr.print(buffer.string) $stderr.flush end |
#say_warning(message, status: "Warning", nl: true) ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/spoom/cli/helper.rb', line 59 def say_warning(, status: "Warning", nl: true) buffer = StringIO.new buffer << "#{yellow(status)}: " if status buffer << highlight() buffer << "\n" if nl && !.end_with?("\n") $stderr.print(buffer.string) $stderr.flush end |
#yellow(string) ⇒ Object
164 165 166 |
# File 'lib/spoom/cli/helper.rb', line 164 def yellow(string) colorize(string, Color::YELLOW) end |