Class: Rigor::CLI::AnnotateCommand
- 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` (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 pre-v0.2.0 `#=> dump_type: <type>` spelling. 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
-
#run ⇒ Integer
CLI exit status.
Methods inherited from Command
Constructor Details
This class inherits a constructor from Rigor::CLI::Command
Instance Method Details
#run ⇒ Integer
Returns CLI exit status.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/rigor/cli/annotate_command.rb', line 54 def run = 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, ) end |