Skip to content

Commit

Permalink
Add basic CLI/REPL
Browse files Browse the repository at this point in the history
  • Loading branch information
tygerbytes committed Jul 22, 2017
1 parent 34c1a65 commit 3fc9ad1
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 7 deletions.
13 changes: 8 additions & 5 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

task :default => :build
task default: :build

task :build => [:gen_all_run_types, :spec]
task build: %i[gen_all_run_types spec]

desc 'Generate the all_run_types.g.rb file'
task :gen_all_run_types do
Expand All @@ -19,16 +21,17 @@ task :gen_all_run_types do
filename_sans_extension = filename[0, filename.length - 3]
parts = filename_sans_extension.to_s.downcase.split(/_|\./)
run_type = ''
parts.each { |part|
parts.each do |part|
run_type += part[0].upcase + part[-(part.length - 1), part.length - 1]
}
end
run_type
end
puts all_run_types.join(' ')

# Write run types to the generated file, all_run_types.g.rb
template = File.read(File.join(run_types_path, 'all_run_types.template'))
template.gsub!('__RUN_TYPES__', all_run_types.join(' '))
template.gsub!('__RUN_TYPE_NAMES__', all_run_types.join(' '))
template.gsub!('__RUN_TYPES__', all_run_types.join(', '))
File.write(File.join(run_types_path, 'all_run_types.g.rb'), template)
puts "\e[32mDone\e[0m\n\n"
end
15 changes: 15 additions & 0 deletions bin/runbypace
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env ruby

begin
require "bundler/setup"
rescue LoadError
end

begin
require 'runby_pace'
rescue LoadError
require_relative '../lib/runby_pace'
end

cli = Runby::Cli.new(ARGV)
exit cli.run
57 changes: 57 additions & 0 deletions lib/runby_pace/cli.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

require 'readline'

module Runby
# Command line interface and REPL for RunbyPace
class Cli
def initialize(args = ARGV)
@args = args
end

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

def targets(five_k_time, distance_units = :mi)
five_k_time = Runby.sanitize(five_k_time).as(RunbyTime)
puts "\nIf you can run a 5K in #{five_k_time}, your training paces should be:"
RunTypes.all_classes.each do |run_type|
run = run_type.new
puts " #{run.description}: #{run.lookup_pace(five_k_time, distance_units)}"
end
nil
end

# -- Shortcuts
def d(*args)
Distance.new(*args)
end

def du(*args)
DistanceUnit.new(*args)
end

def p(*args)
Pace.new(*args)
end

def s(*args)
Speed.new(*args)
end

def t(*args)
RunbyTime.new(*args)
end
end
end
6 changes: 5 additions & 1 deletion lib/runby_pace/run_types/all_run_types.template
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ module Runby
# Encapsulates data and behavior relating to all run types.
module RunTypes
def self.all
%w(__RUN_TYPES__)
%w[__RUN_TYPE_NAMES__]
end

def self.all_classes
[__RUN_TYPES__]
end
end
end
2 changes: 1 addition & 1 deletion lib/runby_pace/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Runby
VERSION = "0.6.#{`git rev-list --count HEAD`}"
VERSION = "0.61.#{`git rev-list --count HEAD`}"
end

0 comments on commit 3fc9ad1

Please sign in to comment.