class TarkinSh

Constants

HIST_LINES
SETTINGS_FILE

Protected Class Methods

start(client, commands) click to toggle source
# File lib/tarkin_sh.rb, line 88
def self.start(client, commands)
  @@client = client
  @@commands = commands
  run
end

Public Instance Methods

complete_cat(args) click to toggle source
# File lib/tarkin_sh.rb, line 51
def complete_cat(args)
  commands.items(full_dir(@cd)).collect {|x| x[:username]}.select {|x| x.start_with? args}
end
complete_cd(args) click to toggle source
# File lib/tarkin_sh.rb, line 38
def complete_cd(args)
  commands.dirs(full_dir(@cd)).collect {|x| x[:name]}.select {|x| x.start_with? args}
end
do_cat(args) click to toggle source
# File lib/tarkin_sh.rb, line 46
def do_cat(args)
  args[:args].each do |item|
    commands.cat(full_dir(item))
  end
end
do_cd(args) click to toggle source
# File lib/tarkin_sh.rb, line 29
def do_cd(args)
  if args[:args].empty? 
    @cd = '/'
  else
    # TODO: warn if change to non-existing directory
    dir = args[:args].first
    @cd = full_dir dir
  end
end
do_find(args) click to toggle source
# File lib/tarkin_sh.rb, line 57
def do_find(args)
  if args[:args].empty?
    commands.ls @cd, false
  else
    commands.find(args[:args])
  end
end
do_help(command = nil) click to toggle source
# File lib/tarkin_sh.rb, line 65
def do_help(command = nil)
  command = command[:args].first
  if command
    command = translate_shortcut(command)
    docs.include?(command) ? print_help(command) : no_help(command)
  else
    documented_commands.each {|cmd| print_help cmd}
    print_undocumented_commands if undocumented_commands?
  end
end
do_ls(args) click to toggle source
# File lib/tarkin_sh.rb, line 13
def do_ls(args)
  if args[:args].empty?
    commands.ls @cd, args[:options].include?('l')
  else
    args[:args].each do |dir|
      commands.ls full_dir(dir), args[:options].include?('l')
    end
  end
end
do_pwd() click to toggle source
# File lib/tarkin_sh.rb, line 24
def do_pwd
  write @cd
end
do_shell(line) click to toggle source

Executes a shell, perhaps should only be defined by subclasses.

# File lib/tarkin_sh.rb, line 79
def do_shell(line)
  line = line[:original]
  shell = ENV['SHELL']
  write shell
  write "**#{line}**"
  line.empty? ?  system(shell) : write(%x(#{line}).strip)
end

Protected Instance Methods

command_missing(command, args) click to toggle source
# File lib/tarkin_sh.rb, line 108
def command_missing(command, args)
  write "tarkin: command not found: #{command}"
end
handle_client_exception(exception) click to toggle source
# File lib/tarkin_sh.rb, line 112
def handle_client_exception(exception)
  write "tarkin: #{exception.message}"
end
postloop() click to toggle source
# File lib/tarkin_sh.rb, line 116
def postloop
  settings = { cd: @cd, history: Readline::HISTORY.to_a.last(HIST_LINES) }
  File.open(SETTINGS_FILE, 'w') { |f| f.puts settings.to_yaml }
end
prompt_command() click to toggle source
# File lib/tarkin_sh.rb, line 104
def prompt_command
  "#{client.settings[:tarkin_url]}#{@cd}> "
end
setup() click to toggle source
# File lib/tarkin_sh.rb, line 94
def setup
  if File.exists? SETTINGS_FILE
    settings = YAML::load_file SETTINGS_FILE
    @cd = settings[:cd]
    settings[:history].each { |h| Readline::HISTORY.push h }
  else
    @cd = '/'
  end
end
tokenize_args(args) click to toggle source
# File lib/tarkin_sh.rb, line 121
def tokenize_args(args)
  a = if args then args.split else [] end
  { args: a.select {|x| !x.start_with? '-'},
    options: a.select {|x| x.start_with? '-'}.map {|x| x.sub(/^-/, '')}.join.split('').uniq,
    original: args}
end

Private Instance Methods

client() click to toggle source
# File lib/tarkin_sh.rb, line 133
def client
  @@client
end
commands() click to toggle source
# File lib/tarkin_sh.rb, line 129
def commands
  @@commands
end
full_dir(dir) click to toggle source
# File lib/tarkin_sh.rb, line 137
def full_dir(dir)
  if dir.start_with? '/'
    File.absolute_path(dir)
  else
    File.absolute_path(dir, @cd)
  end
end