class StackLoop::App

Attributes

collect_file[RW]
command[RW]
default_args[RW]
stack_file[RW]

Public Class Methods

new() click to toggle source
# File lib/stack_loop/app.rb, line 35
def initialize
  @default_args = ""
end

Public Instance Methods

command_hash() click to toggle source
# File lib/stack_loop/app.rb, line 151
def command_hash
  @command_hash ||= Hash[ commands.map{|cmd| [cmd.name, cmd] } ]
end
command_line(argset) click to toggle source
# File lib/stack_loop/app.rb, line 137
def command_line(argset)
  command_list + argset
end
command_list() click to toggle source
# File lib/stack_loop/app.rb, line 129
def command_list
  @command_list ||= command.split(/\s/)
end
command_names() click to toggle source
# File lib/stack_loop/app.rb, line 147
def command_names
  @command_names ||= command_hash.keys
end
commands() click to toggle source
# File lib/stack_loop/app.rb, line 19
def commands
  [
    Command.new("run"){ run_stack_loop },
    Command.new("try"){ run_top },
    Command.new("collect"){ push_new; run_stack_loop },
    Command.new("list"){ read_stack; @stack.reverse_each{|argset| puts "- #{command_line(argset).join(" ")}"} },
    Command.new("pop"){ pop_argset },
    Command.new("push"){|args| push_argset(args)},
    Command.new("edit"){ edit_stack },
    Command.new("quit"){ throw :quit },
    Command.new("help"){ puts "Don't know that one - try: #{command_names.join(", ")}" }
  ]
end
current_command_line() click to toggle source
# File lib/stack_loop/app.rb, line 133
def current_command_line
  command_line(get_argset)
end
edit_stack() click to toggle source
# File lib/stack_loop/app.rb, line 93
def edit_stack
  write_stack
  execute_editor_command(stack_file)
  read_stack
end
execute_editor_command(path) click to toggle source
# File lib/stack_loop/app.rb, line 99
def execute_editor_command(path)
  editor = ENV["EDITOR"] || "vim"
  system(editor, path)
end
get_argset() click to toggle source
# File lib/stack_loop/app.rb, line 67
def get_argset
  read_stack
  if @stack.empty?
    [default_args]
  else
    @stack.last
  end
end
pop_argset() click to toggle source
# File lib/stack_loop/app.rb, line 82
def pop_argset
  read_stack
  @stack.pop
  write_stack
end
prompt() click to toggle source
# File lib/stack_loop/app.rb, line 141
def prompt
  puts
  puts "Next: " + current_command_line.join(" ")
  print "#{stack_depth} > "
end
push_argset(args) click to toggle source
# File lib/stack_loop/app.rb, line 76
def push_argset(args)
  read_stack
  @stack.push(args)
  write_stack
end
push_new() click to toggle source
# File lib/stack_loop/app.rb, line 44
def push_new
  unless collect_file.nil?
    push_argset File::read(collect_file).split("\n")
  end
end
read_stack() click to toggle source
# File lib/stack_loop/app.rb, line 50
def read_stack
  stack_string = File.read(stack_file)
  @stack = stack_string.split("\n\n").map do |item|
    item.split("\n")
  end.reject do |item|
    item.empty?
  end
rescue Errno::ENOENT
  @stack = []
end
run() click to toggle source
# File lib/stack_loop/app.rb, line 104
def run
  validate_options!

  puts "Starting with: " + current_command_line.join(" ")
  puts
  run_stack_loop

  abbreviations = Abbrev.abbrev(command_names)

  catch :quit do
    loop do
      prompt
      command_line = $stdin.gets.chomp.split(/\s+/)
      command_name = command_line.shift
      if command_name.nil?
        command_name = "run"
      end

      command_name = abbreviations[command_name]
      command = command_hash.fetch(command_name, command_hash["help"])
      command.run(command_line)
    end
  end
end
run_stack() click to toggle source
# File lib/stack_loop/app.rb, line 165
def run_stack
  return false if @stack.empty?

  success = run_top

  if success.nil?
    raise "#{[command, *args].inspect} couldn't be run"
  end
  pop_argset if success

  return success
end
run_stack_loop() click to toggle source
# File lib/stack_loop/app.rb, line 155
def run_stack_loop
  while run_stack
    puts
    puts "Success running next..."
    puts
  end

  run_top if @stack.empty?
end
run_top() click to toggle source
# File lib/stack_loop/app.rb, line 178
def run_top
  puts current_command_line.join(" ")
  system(*current_command_line)
end
stack_depth() click to toggle source
# File lib/stack_loop/app.rb, line 88
def stack_depth
  read_stack
  @stack.length
end
validate_options!() click to toggle source
# File lib/stack_loop/app.rb, line 39
def validate_options!
  raise "Need a command!" if command.nil?
  raise "Need a stack file!" if stack_file.nil?
end
write_stack() click to toggle source
# File lib/stack_loop/app.rb, line 61
def write_stack
  File::write(stack_file, @stack.map do |argset|
    argset.join("\n")
  end.join("\n\n") + "\n")
end