Module: Spoom::Cli::Helper
- Extended by:
- T::Helpers, T::Sig
- Includes:
- Thor::Shell
- Defined in:
- lib/spoom/cli/helper.rb
Constant Summary collapse
- HIGHLIGHT_COLOR =
Color used to highlight expressions in backticks
:blue
Instance Method Summary collapse
- #blue(string) ⇒ Object
- #color? ⇒ Boolean
- #colorize(string, color) ⇒ Object
- #exec_path ⇒ Object
- #gray(string) ⇒ Object
- #green(string) ⇒ Object
- #highlight(string) ⇒ Object
- #in_sorbet_project! ⇒ Object
- #in_sorbet_project? ⇒ Boolean
- #red(string) ⇒ Object
- #say(message) ⇒ Object
- #say_error(message, status: "Error", nl: true) ⇒ Object
- #sorbet_config ⇒ Object
- #sorbet_config_file ⇒ Object
- #yellow(string) ⇒ Object
Instance Method Details
#blue(string) ⇒ Object
127 128 129 |
# File 'lib/spoom/cli/helper.rb', line 127 def blue(string) colorize(string, :blue) end |
#color? ⇒ Boolean
92 93 94 |
# File 'lib/spoom/cli/helper.rb', line 92 def color? [:color] end |
#colorize(string, color) ⇒ Object
121 122 123 124 |
# File 'lib/spoom/cli/helper.rb', line 121 def colorize(string, color) return string unless color? string.colorize(color) end |
#exec_path ⇒ Object
71 72 73 |
# File 'lib/spoom/cli/helper.rb', line 71 def exec_path [:path] end |
#gray(string) ⇒ Object
132 133 134 |
# File 'lib/spoom/cli/helper.rb', line 132 def gray(string) colorize(string, :light_black) end |
#green(string) ⇒ Object
137 138 139 |
# File 'lib/spoom/cli/helper.rb', line 137 def green(string) colorize(string, :green) end |
#highlight(string) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/spoom/cli/helper.rb', line 97 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 |
#in_sorbet_project! ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/spoom/cli/helper.rb', line 58 def in_sorbet_project! unless in_sorbet_project? say_error( "not in a Sorbet project (`#{sorbet_config_file}` 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 end |
#in_sorbet_project? ⇒ Boolean
50 51 52 |
# File 'lib/spoom/cli/helper.rb', line 50 def in_sorbet_project? File.file?(sorbet_config_file) end |
#red(string) ⇒ Object
142 143 144 |
# File 'lib/spoom/cli/helper.rb', line 142 def red(string) colorize(string, :red) end |
#say(message) ⇒ Object
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
38 39 40 41 42 43 44 45 46 |
# File 'lib/spoom/cli/helper.rb', line 38 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 |
#sorbet_config ⇒ Object
81 82 83 |
# File 'lib/spoom/cli/helper.rb', line 81 def sorbet_config Sorbet::Config.parse_file(sorbet_config_file) end |
#sorbet_config_file ⇒ Object
76 77 78 |
# File 'lib/spoom/cli/helper.rb', line 76 def sorbet_config_file Pathname.new("#{exec_path}/#{Spoom::Sorbet::CONFIG_PATH}").cleanpath.to_s end |
#yellow(string) ⇒ Object
147 148 149 |
# File 'lib/spoom/cli/helper.rb', line 147 def yellow(string) colorize(string, :yellow) end |