lib/spoom/cli/run.rb in spoom-1.0.7 vs lib/spoom/cli/run.rb in spoom-1.0.8
- old
+ new
@@ -6,62 +6,92 @@
class Run < Thor
include Helper
default_task :tc
- desc "tc", "run srb tc"
- option :limit, type: :numeric, aliases: :l
- option :code, type: :numeric, aliases: :c
- option :sort, type: :string, aliases: :s
- def tc
+ SORT_CODE = "code"
+ SORT_LOC = "loc"
+ SORT_ENUM = [SORT_CODE, SORT_LOC]
+
+ DEFAULT_FORMAT = "%C - %F:%L: %M"
+
+ desc "tc", "Run `srb tc`"
+ option :limit, type: :numeric, aliases: :l, desc: "Limit displayed errors"
+ option :code, type: :numeric, aliases: :c, desc: "Filter displayed errors by code"
+ option :sort, type: :string, aliases: :s, desc: "Sort errors", enum: SORT_ENUM, lazy_default: SORT_LOC
+ option :format, type: :string, aliases: :f, desc: "Format line output"
+ option :uniq, type: :boolean, aliases: :u, desc: "Remove duplicated lines"
+ option :count, type: :boolean, default: true, desc: "Show errors count"
+ option :sorbet, type: :string, desc: "Path to custom Sorbet bin"
+ def tc(*arg)
in_sorbet_project!
path = exec_path
limit = options[:limit]
sort = options[:sort]
code = options[:code]
- colors = options[:color]
+ uniq = options[:uniq]
+ format = options[:format]
+ count = options[:count]
+ sorbet = options[:sorbet]
unless limit || code || sort
- return Spoom::Sorbet.srb_tc(path: path, capture_err: false).last
+ output, status = T.unsafe(Spoom::Sorbet).srb_tc(*arg, path: path, capture_err: false, sorbet_bin: sorbet)
+ $stderr.print(output)
+ exit(status)
end
- output, status = Spoom::Sorbet.srb_tc(path: path, capture_err: true)
+ output, status = T.unsafe(Spoom::Sorbet).srb_tc(*arg, path: path, capture_err: true, sorbet_bin: sorbet)
if status
$stderr.print(output)
- return 0
+ exit(0)
end
errors = Spoom::Sorbet::Errors::Parser.parse_string(output)
errors_count = errors.size
- errors = sort == "code" ? errors.sort_by { |e| [e.code, e.file, e.line, e.message] } : errors.sort
+ errors = case sort
+ when SORT_CODE
+ Spoom::Sorbet::Errors.sort_errors_by_code(errors)
+ when SORT_LOC
+ errors.sort
+ else
+ errors # preserve natural sort
+ end
+
errors = errors.select { |e| e.code == code } if code
errors = T.must(errors.slice(0, limit)) if limit
- errors.each do |e|
- code = colorize_code(e.code, colors)
- message = colorize_message(e.message, colors)
- $stderr.puts "#{code} - #{e.file}:#{e.line}: #{message}"
+ lines = errors.map { |e| format_error(e, format || DEFAULT_FORMAT) }
+ lines = lines.uniq if uniq
+
+ lines.each do |line|
+ $stderr.puts line
end
- if errors_count == errors.size
- $stderr.puts "Errors: #{errors_count}"
- else
- $stderr.puts "Errors: #{errors.size} shown, #{errors_count} total"
+ if count
+ if errors_count == errors.size
+ $stderr.puts "Errors: #{errors_count}"
+ else
+ $stderr.puts "Errors: #{errors.size} shown, #{errors_count} total"
+ end
end
- 1
+ exit(1)
end
no_commands do
- def colorize_code(code, colors = true)
- return code.to_s unless colors
- code.to_s.light_black
+ def format_error(error, format)
+ line = format
+ line = line.gsub(/%C/, colorize(error.code.to_s, :yellow))
+ line = line.gsub(/%F/, error.file)
+ line = line.gsub(/%L/, error.line.to_s)
+ line = line.gsub(/%M/, colorize_message(error.message))
+ line
end
- def colorize_message(message, colors = true)
- return message unless colors
+ def colorize_message(message)
+ return message unless color?
cyan = T.let(false, T::Boolean)
word = StringIO.new
message.chars.each do |c|
if c == '`'