Class: Rigor::CLI::AnnotateCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/rigor/cli/annotate_command.rb

Overview

Executes rigor annotate FILE.

For every source line the command finds the expression the line evaluates to — the last statement that ends on the line (so 1; 2; 3 reports 3), or, for a line that no statement closes, the widest expression ending there (so the if nil header reports its condition). It infers that expression's type and appends a #=> <type> comment (the xmpfilter / seeing_is_believing convention).

The annotated source is re-parsed with Prism — a sanity gate, since the appended text is always a comment — and printed to stdout. When colour is enabled and bat (https://github.com/sharkdp/bat) is on PATH it is used for highlighting; otherwise IRB-style highlighting via PrismColorizer.

Constant Summary collapse

USAGE =
"Usage: rigor annotate [options] FILE"
ANNOTATION_PATTERN =

Trailing #=> … annotation comment. Matched and stripped before re-annotating so re-running is idempotent — this follows xmpfilter's convention of owning the #=> marker, and also absorbs the older #=> dump_type: <type> spelling (idempotency across re-runs). The leading \s requirement keeps a #=> inside a string literal (no preceding whitespace ambiguity aside) from matching mid-expression.

/\s+#=>(?:\s.*)?\z/
BAT_ARGS =

Arguments for highlighting through bat: the annotated text arrives on stdin, so the language must be explicit; --style=plain drops the grid/header chrome so the output matches the PrismColorizer fallback line-for-line; paging stays off because the CLI may itself sit in a pipeline.

%w[--language=ruby --style=plain --paging=never --color=always].freeze

Instance Method Summary collapse

Methods inherited from Command

#initialize

Constructor Details

This class inherits a constructor from Rigor::CLI::Command

Instance Method Details

#runInteger

Returns CLI exit status.

Returns:

  • (Integer)

    CLI exit status.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rigor/cli/annotate_command.rb', line 45

def run
  options = parse_options
  file = @argv.shift
  if file.nil?
    @err.puts(USAGE)
    return CLI::EXIT_USAGE
  end
  unless File.file?(file)
    @err.puts("annotate: file not found: #{file}")
    return 1
  end

  execute(file, options)
end