Class: Laser::Cutter::CLI::Command

Inherits:
Dry::CLI::Command
  • Object
show all
Includes:
Dry::CLI::UI
Defined in:
lib/laser/cutter/cli/command.rb

Overview

What every command inherits: the ui helpers, and the flags they all share.

Direct Known Subclasses

Examples, Generate, Help, PageSizes, Version

Constant Summary collapse

BAR_WIDTH =

Cells in a progress bar.

60

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.finalize!(subclass) ⇒ Object



34
35
36
37
# File 'lib/laser/cutter/cli/command.rb', line 34

def self.finalize!(subclass)
  subclass.option :verbose, type: :boolean, default: false, aliases: ['-v'],
                            desc: 'Print the configuration, and the full backtrace on failure'
end

.inherited(subclass) ⇒ void

This method returns an undefined value.

Gives every command the -v, --verbose flag.

Parameters:

  • subclass (Class) —

    the new command



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/laser/cutter/cli/command.rb', line 17

def self.inherited(subclass)
  super

  # Add --verbose flag to the command, but at the very end of the class definition.
  trace = TracePoint.new(:end) do |tp|
    # skip nested classes closing inside the body
    next unless tp.self == subclass

    # disable the trace point
    trace.disable

    # add the verbose option to the subclass as the last option
    subclass.class_exec { finalize!(subclass) }
  end
  trace.enable(target_thread: Thread.current)
end

Instance Method Details

#progress(label, total:) {|bar| ... } ⇒ Object

A green bar of BAR_WIDTH cells, or fewer when the terminal is narrower.

Parameters:

  • label (String)
  • total (Integer)

Yield Parameters:

  • bar (#advance)

Returns:

  • (Object) —

    what the block returns



52
53
54
55
56
# File 'lib/laser/cutter/cli/command.rb', line 52

def progress(label, total:, &)
  Dry::CLI::UI::Console
    .new(out: out, err: err, box_width: CLI.help_width, width: progress_width(label))
    .progress(label, total: total, color: :green, &)
end

#ui ⇒ Dry::CLI::UI::Console

Boxes are as wide as help is.

Returns:

  • (Dry::CLI::UI::Console)


42
43
44
# File 'lib/laser/cutter/cli/command.rb', line 42

def ui
  @ui ||= CLI.console(out: out, err: err)
end