class Runby::Cli::Cli

Command line interface and REPL for RunbyPace

Public Class Methods

new(args = ARGV) click to toggle source
# File lib/runby_pace/cli/cli.rb, line 11
def initialize(args = ARGV)
  @args = args
  @config = Config.new
  @options = parse_options args
end

Public Instance Methods

di(*args) click to toggle source

– Shortcuts for the REPL

# File lib/runby_pace/cli/cli.rb, line 46
def di(*args)
  Distance.new(*args)
end
du(*args) click to toggle source
# File lib/runby_pace/cli/cli.rb, line 50
def du(*args)
  DistanceUnit.new(*args)
end
pc(*args) click to toggle source
# File lib/runby_pace/cli/cli.rb, line 54
def pc(*args)
  Pace.new(*args)
end
print_targets(five_k_time, distance_units = :mi) click to toggle source
run() click to toggle source
# File lib/runby_pace/cli/cli.rb, line 17
def run
  puts 'Runby Pace REPL!'
  bnd = binding
  while (input = Readline.readline('🏃 ', true))
    begin
      result = bnd.eval input
    rescue StandardError => e
      puts "#{e.class}: #{e.message}"
    else
      puts result
    end
  end
end
sp(*args) click to toggle source
# File lib/runby_pace/cli/cli.rb, line 58
def sp(*args)
  Speed.new(*args)
end
tm(*args) click to toggle source
# File lib/runby_pace/cli/cli.rb, line 62
def tm(*args)
  RunbyTime.new(*args)
end

Private Instance Methods

manage_config(config) click to toggle source
# File lib/runby_pace/cli/cli.rb, line 93
def manage_config(config)
  c = parse_config_setting config
  unless c.key
    # No key specified. Print all settings.
    @config.pretty_print
    return
  end
  if c.value
    # Set setting "key" to new "value"
    @config[c.key] = c.value
    return
  end
  if c.clear_setting
    @config[c.key] = nil
  else
    # Print the value of setting "key"
    p @config[c.key]
  end
end
parse_config_setting(setting) click to toggle source
# File lib/runby_pace/cli/cli.rb, line 113
def parse_config_setting(setting)
  setting = '' if setting.nil?
  Class.new do
    attr_reader :key, :value, :clear_setting
    def initialize(setting)
      tokens = setting.split('=')
      @key = tokens[0]
      @value = tokens[1]
      @clear_setting = (@value.nil? && setting.include?('='))
    end
  end.new(setting)
end
parse_options(options) click to toggle source
# File lib/runby_pace/cli/cli.rb, line 68
def parse_options(options)
  args = { targets: nil }

  OptionParser.new do |opts|
    opts.banner = 'Usage: runbypace.rb [options]'

    opts.on('-h', '--help', 'Display this help message') do
      puts opts
      exit
    end

    opts.on('-c', '--config [SETTING][=NEW_VALUE]', 'Get or set a configuration value') do |config|
      manage_config config
      exit
    end

    opts.on('-t', '--targets [5K race time]', 'Show target paces') do |targets|
      args[:targets] = targets
      print_targets targets
      exit
    end
  end.parse!(options)
  args
end