class Chronolog::CLI
Constants
- CLI_CMDS
- CMDS
- LIB_CMDS
Public Class Methods
new(path)
click to toggle source
# File lib/chronolog/cli.rb, line 9 def initialize(path) @interactive = false raise usage if path.nil? @chronolog = Chronolog::Engine.new(File.open(path, "a+")) end
Public Instance Methods
run(cmd)
click to toggle source
# File lib/chronolog/cli.rb, line 15 def run(cmd) if cmd.empty? @interactive = true comp = proc { |s| CMDS.grep(/^#{Regexp.escape(s)}/) } Readline.completion_append_character = "" Readline.completion_proc = comp exec(cmd) while (cmd = Readline.readline("> ", true)) else exec(cmd) end rescue Interrupt exit end
Protected Instance Methods
exec(cmd)
click to toggle source
# File lib/chronolog/cli.rb, line 31 def exec(cmd) exit if cmd =~ /^q/ args = cmd.split name = args.shift return usage unless LIB_CMDS.include?(name) @chronolog.send(name, **parse(args)) rescue StandardError => e print_error(e.message) end
parse(args)
click to toggle source
# File lib/chronolog/cli.rb, line 41 def parse(args) key = :time args.each_with_object({}) do |arg, opts| case arg when "at", "on", "for" key = :time when "in" key = :unit else opts[key] = "#{opts[key]} #{arg}".lstrip if key end end end
print_error(text)
click to toggle source
# File lib/chronolog/cli.rb, line 64 def print_error(text) error = Rainbow("Error:").red puts "#{error} #{text}" end
usage()
click to toggle source
# File lib/chronolog/cli.rb, line 55 def usage unless @interactive puts "Usage: chronolog <file.log> [command]" puts end commands = (@interactive ? CMDS : LIB_CMDS).join("', '") puts "Available commands: '#{commands}'" end