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
- #check_sorbet_segfault(exit_code, &block) ⇒ Object
- #color? ⇒ Boolean
- #colorize(string, *color) ⇒ Object
- #cyan(string) ⇒ 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
Methods included from Spoom::Colorize
Instance Method Details
#blue(string) ⇒ Object
142 143 144 |
# File 'lib/spoom/cli/helper.rb', line 142 def blue(string) colorize(string, Color::BLUE) end |
#check_sorbet_segfault(exit_code, &block) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/spoom/cli/helper.rb', line 87 def check_sorbet_segfault(exit_code, &block) return unless exit_code == Spoom::Sorbet::SEGFAULT_CODE say_error(<<~ERR, status: nil) #{red("!!! Sorbet exited with code #{exit_code} - SEGFAULT !!!")} This is most likely related to a bug in Sorbet. ERR block&.call exit(exit_code) end |
#color? ⇒ Boolean
107 108 109 |
# File 'lib/spoom/cli/helper.rb', line 107 def color? [:color] end |
#colorize(string, *color) ⇒ Object
136 137 138 139 |
# File 'lib/spoom/cli/helper.rb', line 136 def colorize(string, *color) return string unless color? T.unsafe(self).set_color(string, *color) end |
#cyan(string) ⇒ Object
147 148 149 |
# File 'lib/spoom/cli/helper.rb', line 147 def cyan(string) colorize(string, Color::CYAN) end |
#exec_path ⇒ Object
72 73 74 |
# File 'lib/spoom/cli/helper.rb', line 72 def exec_path [:path] end |
#gray(string) ⇒ Object
152 153 154 |
# File 'lib/spoom/cli/helper.rb', line 152 def gray(string) colorize(string, Color::LIGHT_BLACK) end |
#green(string) ⇒ Object
157 158 159 |
# File 'lib/spoom/cli/helper.rb', line 157 def green(string) colorize(string, Color::GREEN) end |
#highlight(string) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/spoom/cli/helper.rb', line 112 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
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/spoom/cli/helper.rb', line 59 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
51 52 53 |
# File 'lib/spoom/cli/helper.rb', line 51 def in_sorbet_project? File.file?(sorbet_config_file) end |
#red(string) ⇒ Object
162 163 164 |
# File 'lib/spoom/cli/helper.rb', line 162 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 |
#sorbet_config ⇒ Object
82 83 84 |
# File 'lib/spoom/cli/helper.rb', line 82 def sorbet_config Sorbet::Config.parse_file(sorbet_config_file) end |
#sorbet_config_file ⇒ Object
77 78 79 |
# File 'lib/spoom/cli/helper.rb', line 77 def sorbet_config_file Pathname.new("#{exec_path}/#{Spoom::Sorbet::CONFIG_PATH}").cleanpath.to_s end |
#yellow(string) ⇒ Object
167 168 169 |
# File 'lib/spoom/cli/helper.rb', line 167 def yellow(string) colorize(string, Color::YELLOW) end |