Class: Chronolog::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/chronolog/cli.rb

Constant Summary collapse

CLI_CMDS =
%w[quit help].freeze
LIB_CMDS =
%w[print start started stop stopped].freeze
CMDS =
(CLI_CMDS + LIB_CMDS).sort.freeze

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ CLI

Returns a new instance of CLI.



9
10
11
12
13
# 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

Instance Method Details

#run(cmd) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# 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