Module: Spoom::Cli::Helper
- Extended by:
- T::Helpers
- Includes:
- Spoom::Colorize
- Included in:
- Config, Deadcode, Main, Srb::Assertions, Srb::Bump, Srb::Coverage, Srb::LSP, Srb::Sigs, Srb::Tc
- Defined in:
- lib/spoom/cli/helper.rb
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
: (String string) -> String.
- #collect_files(paths) ⇒ Object
-
#color? ⇒ Boolean
Is the ‘–color` option true? : -> bool.
-
#colorize(string, *color) ⇒ Object
Colorize a string if ‘color?` : (String string, *Color color) -> String.
-
#context ⇒ Object
Returns the context at ‘–path` (by default the current working directory) : -> Context.
-
#context_requiring_sorbet! ⇒ Object
Raise if ‘spoom` is not ran inside a context with a `sorbet/config` file : -> Context.
-
#cyan(string) ⇒ Object
: (String string) -> String.
-
#exec_path ⇒ Object
Return the path specified through ‘–path` : -> String.
-
#gray(string) ⇒ Object
: (String string) -> String.
-
#green(string) ⇒ Object
: (String string) -> String.
-
#highlight(string) ⇒ Object
: (String string) -> String.
-
#red(string) ⇒ Object
: (String string) -> String.
-
#say(message) ⇒ Object
Print ‘message` on `$stdout` : (String message) -> void.
-
#say_error(message, status: "Error", nl: true) ⇒ Object
Print ‘message` on `$stderr`.
-
#say_warning(message, status: "Warning", nl: true) ⇒ Object
Print ‘message` on `$stderr`.
-
#yellow(string) ⇒ Object
: (String string) -> String.
Methods included from Spoom::Colorize
Instance Method Details
#blue(string) ⇒ Object
: (String string) -> String
147 148 149 |
# File 'lib/spoom/cli/helper.rb', line 147 def blue(string) colorize(string, Color::BLUE) end |
#collect_files(paths) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/spoom/cli/helper.rb', line 85 def collect_files(paths) paths << exec_path if paths.empty? files = paths.flat_map do |path| if File.file?(path) path else Dir.glob("#{path}/**/*.rb") end end if files.empty? say_error("No files found") exit(1) end files end |
#color? ⇒ Boolean
Is the ‘–color` option true? : -> bool
111 112 113 |
# File 'lib/spoom/cli/helper.rb', line 111 def color? [:color] end |
#colorize(string, *color) ⇒ Object
Colorize a string if ‘color?` : (String string, *Color color) -> String
140 141 142 143 144 |
# File 'lib/spoom/cli/helper.rb', line 140 def colorize(string, *color) return string unless color? T.unsafe(self).set_color(string, *color) end |
#context ⇒ Object
Returns the context at ‘–path` (by default the current working directory) : -> Context
58 59 60 |
# File 'lib/spoom/cli/helper.rb', line 58 def context @context ||= T.let(Context.new(exec_path), T.nilable(Context)) end |
#context_requiring_sorbet! ⇒ Object
Raise if ‘spoom` is not ran inside a context with a `sorbet/config` file : -> Context
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/spoom/cli/helper.rb', line 64 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
: (String string) -> String
152 153 154 |
# File 'lib/spoom/cli/helper.rb', line 152 def cyan(string) colorize(string, Color::CYAN) end |
#exec_path ⇒ Object
Return the path specified through ‘–path` : -> String
79 80 81 |
# File 'lib/spoom/cli/helper.rb', line 79 def exec_path [:path] end |
#gray(string) ⇒ Object
: (String string) -> String
157 158 159 |
# File 'lib/spoom/cli/helper.rb', line 157 def gray(string) colorize(string, Color::LIGHT_BLACK) end |
#green(string) ⇒ Object
: (String string) -> String
162 163 164 |
# File 'lib/spoom/cli/helper.rb', line 162 def green(string) colorize(string, Color::GREEN) end |
#highlight(string) ⇒ Object
: (String string) -> String
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/spoom/cli/helper.rb', line 116 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
: (String string) -> String
167 168 169 |
# File 'lib/spoom/cli/helper.rb', line 167 def red(string) colorize(string, Color::RED) end |
#say(message) ⇒ Object
Print ‘message` on `$stdout` : (String message) -> void
19 20 21 22 23 24 25 26 |
# File 'lib/spoom/cli/helper.rb', line 19 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
Print ‘message` on `$stderr`
The message is prefixed by a status (default: ‘Error`). : (String message, ?status: String?, ?nl: bool) -> void
32 33 34 35 36 37 38 39 40 |
# File 'lib/spoom/cli/helper.rb', line 32 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
Print ‘message` on `$stderr`
The message is prefixed by a status (default: ‘Warning`). : (String message, ?status: String?, ?nl: bool) -> void
46 47 48 49 50 51 52 53 54 |
# File 'lib/spoom/cli/helper.rb', line 46 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
: (String string) -> String
172 173 174 |
# File 'lib/spoom/cli/helper.rb', line 172 def yellow(string) colorize(string, Color::YELLOW) end |