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



129
130
131
# File 'lib/spoom/cli/helper.rb', line 129

def blue(string)
  colorize(string, Color::BLUE)
end

#color?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/spoom/cli/helper.rb', line 93

def color?
  options[:color]
end

#colorize(string, *color) ⇒ Object



122
123
124
125
126
# File 'lib/spoom/cli/helper.rb', line 122

def colorize(string, *color)
  return string unless color?

  T.unsafe(self).set_color(string, *color)
end

#cyan(string) ⇒ Object



134
135
136
# File 'lib/spoom/cli/helper.rb', line 134

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



139
140
141
# File 'lib/spoom/cli/helper.rb', line 139

def gray(string)
  colorize(string, Color::LIGHT_BLACK)
end

#green(string) ⇒ Object



144
145
146
# File 'lib/spoom/cli/helper.rb', line 144

def green(string)
  colorize(string, Color::GREEN)
end

#highlight(string) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/spoom/cli/helper.rb', line 98

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



149
150
151
# File 'lib/spoom/cli/helper.rb', line 149

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



154
155
156
# File 'lib/spoom/cli/helper.rb', line 154

def yellow(string)
  colorize(string, Color::YELLOW)
end