module Twigg::Console

A collection of useful methods for code that is running in a console.

Functionality includes printing, process lifecycle management and formatting.

Private Instance Methods

consume_option(switches, args) click to toggle source

Given `switches` (which may be either a single switch or an array of switches) and an array of arguments, `args`, scans through the arguments looking for the switches and the corresponding values.

This can be used, for example, to extract the value “/etc/twiggrc” from an argument list like “–verbose –debug –config /etc/twiggrc help”.

In the event that the switches appear multiple times in the list, the right-most wins. If a switch is found without a corresponding option an exception is raised.

Consumes matching options (ie. deletes them from `args) and returns the corresponding (rightmost) value, or `nil` in the event there is no match.

# File lib/twigg/console.rb, line 57
def consume_option(switches, args)
  # consume from left to right; rightmost will win
  while index = args.find_index { |arg| Array(switches).include?(arg) }
    switch, value = args.slice!(index, 2)
    raise ArgumentError, "missing option (expected after #{switch})" unless value
  end

  value
end
die(msg = nil) click to toggle source

Exit with an exit code of 1, printing the optional `msg`, prefixed with “error: ”, to standard error if present

# File lib/twigg/console.rb, line 17
def die(msg = nil)
  error(msg) if msg
  exit 1
end
error(msg) click to toggle source

Print `msg` to the standard error, prefixed with “error: ”

# File lib/twigg/console.rb, line 23
def error(msg)
  stderr("error: #{msg}")
end
stderr(*msgs) click to toggle source

Print `msgs` to standard error

# File lib/twigg/console.rb, line 11
def stderr(*msgs)
  STDERR.puts(*msgs)
end
strip_heredoc(doc, indent: 0) click to toggle source

Given a “heredoc” `doc`, find the non-empty line with the smallest indent, and strip that amount of whitespace from the beginning of each line. Subsequently, prepend a fixed number (`indent`) of spaces to each line.

This allows us to write nicely indented copy that sits well with the surrounding code, irrespective of the level of indentation of the code, without emitting excessive whitespace to the user at runtime.

# File lib/twigg/console.rb, line 39
def strip_heredoc(doc, indent: 0)
  strip_count = doc.scan(/^[ \t]*(?=\S)/).map(&:size).min || 0
  doc.gsub(/^[ \t]{#{strip_count}}/, ' ' * indent)
end
warn(msg) click to toggle source

Print `msg` to the standard error, prefixed with “warning: ”

# File lib/twigg/console.rb, line 28
def warn(msg)
  stderr "warning: #{msg}"
end