Module: Spoom::Cli::Helper
- Includes:
- Spoom::Colorize
- Included in:
- Config, Deadcode, Main, Srb::Assertions, Srb::Bump, Srb::Coverage, Srb::LSP, Srb::Metrics, Srb::Sigs, Srb::Tc
- Defined in:
- lib/spoom/cli/helper.rb
Overview
@requires_ancestor: Thor
Constant Summary collapse
Instance Method Summary collapse
-
#blue(string) ⇒ Object
: (String string) -> String.
- #collect_files(paths, include_rbi_files: false) ⇒ 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
146 147 148 |
# File 'lib/spoom/cli/helper.rb', line 146 def blue(string) colorize(string, Color::BLUE) end |
#collect_files(paths, include_rbi_files: false) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/spoom/cli/helper.rb', line 82 def collect_files(paths, include_rbi_files: false) paths << exec_path if paths.empty? files = paths.flat_map do |path| if File.file?(path) path else exts = ["rb"] exts << "rbi" if include_rbi_files Dir.glob("#{path}/**/*.{#{exts.join(",")}}") end end if files.empty? say_error("No files found") exit(1) end files end |
#color? ⇒ Boolean
Is the ‘–color` option true? : -> bool
110 111 112 |
# File 'lib/spoom/cli/helper.rb', line 110 def color? [:color] end |
#colorize(string, *color) ⇒ Object
Colorize a string if ‘color?` : (String string, *Color color) -> String
139 140 141 142 143 |
# File 'lib/spoom/cli/helper.rb', line 139 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
55 56 57 |
# File 'lib/spoom/cli/helper.rb', line 55 def context @context ||= Context.new(exec_path) #: Context? end |
#context_requiring_sorbet! ⇒ Object
Raise if ‘spoom` is not ran inside a context with a `sorbet/config` file : -> Context
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/spoom/cli/helper.rb', line 61 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
151 152 153 |
# File 'lib/spoom/cli/helper.rb', line 151 def cyan(string) colorize(string, Color::CYAN) end |
#exec_path ⇒ Object
Return the path specified through ‘–path` : -> String
76 77 78 |
# File 'lib/spoom/cli/helper.rb', line 76 def exec_path [:path] end |
#gray(string) ⇒ Object
: (String string) -> String
156 157 158 |
# File 'lib/spoom/cli/helper.rb', line 156 def gray(string) colorize(string, Color::LIGHT_BLACK) end |
#green(string) ⇒ Object
: (String string) -> String
161 162 163 |
# File 'lib/spoom/cli/helper.rb', line 161 def green(string) colorize(string, Color::GREEN) end |
#highlight(string) ⇒ Object
: (String string) -> String
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/spoom/cli/helper.rb', line 115 def highlight(string) return string unless color? res = StringIO.new word = StringIO.new in_ticks = false #: bool 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
166 167 168 |
# File 'lib/spoom/cli/helper.rb', line 166 def red(string) colorize(string, Color::RED) end |
#say(message) ⇒ Object
Print ‘message` on `$stdout` : (String message) -> void
16 17 18 19 20 21 22 23 |
# File 'lib/spoom/cli/helper.rb', line 16 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
29 30 31 32 33 34 35 36 37 |
# File 'lib/spoom/cli/helper.rb', line 29 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
43 44 45 46 47 48 49 50 51 |
# File 'lib/spoom/cli/helper.rb', line 43 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
171 172 173 |
# File 'lib/spoom/cli/helper.rb', line 171 def yellow(string) colorize(string, Color::YELLOW) end |