class Agen::Runner

Constants

CONFIG_FILE
DEFAULT_HISTFILE
DEFAULT_NUMBER
DEFAULT_RCFILE

Attributes

auto[R]
builder[R]
config_file[R]
histfile[R]
rcfile[R]

Public Class Methods

new( histfile: DEFAULT_HISTFILE, rcfile: DEFAULT_RCFILE, number: DEFAULT_NUMBER, auto: false, config_file: CONFIG_FILE ) click to toggle source
# File lib/agen/runner.rb, line 12
def initialize(
  histfile: DEFAULT_HISTFILE,
  rcfile: DEFAULT_RCFILE,
  number: DEFAULT_NUMBER,
  auto: false,
  config_file: CONFIG_FILE
)
  @histfile = histfile
  @rcfile = rcfile
  @number = number
  @auto = auto
  @config_file = config_file
end

Public Instance Methods

run() click to toggle source
# File lib/agen/runner.rb, line 26
def run
  commands = Finder.new(histfile).commands(limit: @number)
  @builder = Builder.new(commands, rcfile)
  aliases = builder.aliases

  # TODO: This could be its own class too
  File.open(rcfile, "a") do |file|
    puts "Writing new aliases to #{rcfile}:\n\n"
    aliases.each do |al|
      if auto
        write_auto(file, al[:full_alias])
      else
        write_interactive(file, al)
      end
    end
  end
end

Private Instance Methods

ignore_alias(aliaz) click to toggle source
# File lib/agen/runner.rb, line 72
def ignore_alias(aliaz)
  command = aliaz[:command]
  File.open(config_file, "a") { |f| f.puts(command) }
  puts "Ignoring command '#{command}' forever"
end
modify_alias(file, aliaz) click to toggle source
# File lib/agen/runner.rb, line 78
def modify_alias(file, aliaz)
  print "Enter new alias [replacing #{aliaz[:alias]}]: "
  replacement = gets.chomp
  if replacement == ""
    modify_alias(file, aliaz)
    return
  end

  file.puts(builder.construct_alias(replacement, aliaz[:command]))
  puts "Alias written"
end
write_auto(file, full_alias) click to toggle source
# File lib/agen/runner.rb, line 48
def write_auto(file, full_alias)
  puts full_alias
  file.puts(full_alias)
end
write_interactive(file, aliaz) click to toggle source
# File lib/agen/runner.rb, line 53
def write_interactive(file, aliaz)
  puts "Proposed alias: #{aliaz[:full_alias]}"
  print "Accept? [n to reject, m to modify alias, i to ignore forever, any other key to accept]: "
  response = gets.chomp
  case response
  when "n"
    puts "Alias skipped"
  when "m"
    modify_alias(file, aliaz)
  when "i"
    ignore_alias(aliaz)
  else
    file.puts(aliaz[:full_alias])
    puts "Alias written"
  end

  puts
end