Module: Spoom::Cli::Helper

Extended by:
T::Helpers, T::Sig
Includes:
Spoom::Colorize
Included in:
Bump, Config, Coverage, LSP, Main, Run
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

Methods included from Spoom::Colorize

#set_color

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

Returns:

  • (Boolean)


107
108
109
# File 'lib/spoom/cli/helper.rb', line 107

def color?
  options[: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_pathObject



72
73
74
# File 'lib/spoom/cli/helper.rb', line 72

def exec_path
  options[: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

Returns:

  • (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(message)
  buffer = StringIO.new
  buffer << highlight(message)
  buffer << "\n" unless message.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(message, status: "Error", nl: true)
  buffer = StringIO.new
  buffer << "#{red(status)}: " if status
  buffer << highlight(message)
  buffer << "\n" if nl && !message.end_with?("\n")

  $stderr.print(buffer.string)
  $stderr.flush
end

#sorbet_configObject



82
83
84
# File 'lib/spoom/cli/helper.rb', line 82

def sorbet_config
  Sorbet::Config.parse_file(sorbet_config_file)
end

#sorbet_config_fileObject



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