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



119
120
121
# File 'lib/spoom/cli/helper.rb', line 119

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

#color?Boolean

Returns:

  • (Boolean)


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

def color?
  options[:color]
end

#colorize(string, *color) ⇒ Object



112
113
114
115
116
# File 'lib/spoom/cli/helper.rb', line 112

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

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

#contextObject



51
52
53
# File 'lib/spoom/cli/helper.rb', line 51

def context
  @context ||= T.let(Context.new(exec_path), T.nilable(Context))
end

#context_requiring_sorbet!Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/spoom/cli/helper.rb', line 57

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



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

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



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

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

#green(string) ⇒ Object



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

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

#highlight(string) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/spoom/cli/helper.rb', line 88

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

#red(string) ⇒ Object



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

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

#yellow(string) ⇒ Object



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

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