lib/hammer_cli/shell.rb in hammer_cli-0.0.18 vs lib/hammer_cli/shell.rb in hammer_cli-0.1.0
- old
+ new
@@ -5,11 +5,11 @@
class ShellMainCommand < AbstractCommand
class HelpCommand < AbstractCommand
command_name "help"
- desc "Print help for commands"
+ desc _("Print help for commands")
parameter "[COMMAND] ...", "command"
def execute
ShellMainCommand.run('', command_list << '-h')
@@ -17,93 +17,93 @@
end
end
class ExitCommand < AbstractCommand
command_name "exit"
- desc "Exit interactive shell"
+ desc _("Exit interactive shell")
def execute
exit HammerCLI::EX_OK
end
end
- class AuthCommand < AbstractCommand
- command_name "auth"
- desc "Login and logout actions"
-
- class LoginCommand < AbstractCommand
- command_name "login"
- desc "Set credentials"
-
- def execute
- context[:username] = ask_username
- context[:password] = ask_password
- HammerCLI::EX_OK
- end
+ def self.load_commands(main_cls)
+ cmds = main_cls.recognised_subcommands.select do |sub_cmd|
+ !(sub_cmd.subcommand_class <= HammerCLI::ShellCommand)
end
+ self.recognised_subcommands.push(*cmds)
+ end
- class LogoutCommand < AbstractCommand
- command_name "logout"
- desc "Wipe your credentials"
+ autoload_subcommands
+ end
- def execute
- context[:username] = nil
- context[:password] = nil
- if username(false)
- print_message("Credentials deleted, using defaults now.")
- print_message("You are logged in as [ %s ]." % username(false))
- else
- print_message("Credentials deleted.")
- end
- HammerCLI::EX_OK
- end
- end
+ class ShellHistory
- class InfoCommand < AbstractCommand
- command_name "status"
- desc "Information about current user"
+ def initialize(history_file_path)
+ @file_path = history_file_path
+ load
+ end
- def execute
- if username(false)
- print_message("You are logged in as [ %s ]." % username(false))
- else
- print_message("You are currently not logged in.\nUse 'auth login' to set credentials.")
- end
- HammerCLI::EX_OK
- end
+ def push(line)
+ line.strip!
+ return if line.empty? or ingonred_commands.include?(line)
+
+ Readline::HISTORY.push(line)
+ File.open(file_path, "a") do |f|
+ f.puts(line)
end
+ end
- autoload_subcommands
+ def ingonred_commands
+ ["exit"]
end
+ protected
- def self.load_commands(main_cls)
- cmds = main_cls.recognised_subcommands.select do |sub_cmd|
- !(sub_cmd.subcommand_class <= HammerCLI::ShellCommand)
+ def file_path
+ File.expand_path(@file_path)
+ end
+
+ def load
+ if File.exist?(file_path)
+ File.readlines(file_path).each do |line|
+ Readline::HISTORY.push(line.strip)
+ end
end
- self.recognised_subcommands.push(*cmds)
end
- autoload_subcommands
end
+
class ShellCommand < AbstractCommand
+ DEFAULT_HISTORY_FILE = "~/.hammer_history"
+
def execute
ShellMainCommand.load_commands(HammerCLI::MainCommand)
- Readline.completion_append_character = ''
- Readline.completer_word_break_characters = ' '
- Readline.completion_proc = complete_proc
+ if RUBY_VERSION >= "1.9"
+ Readline.completion_append_character = ''
+ Readline.completer_word_break_characters = ' ='
+ Readline.completion_proc = complete_proc
+ else
+ Readline.completion_proc = lambda {}
+ end
stty_save = `stty -g`.chomp
+ history = ShellHistory.new(Settings.get(:ui, :history_file) || DEFAULT_HISTORY_FILE)
+
begin
print_welcome_message
- while line = Readline.readline(prompt, true)
- ShellMainCommand.run('', line.split, context) unless line.start_with? 'shell' or line.strip.empty?
+ while line = Readline.readline(prompt)
+
+ history.push(line)
+
+ line = HammerCLI::CompleterLine.new(line)
+ ShellMainCommand.run('', line, context) unless line.empty?
end
rescue Interrupt => e
puts
system('stty', stty_save) # Restore
exit
@@ -115,12 +115,13 @@
def prompt
'hammer> '
end
def print_welcome_message
- print_message("Welcome to the hammer interactive shell")
- print_message("Type 'help' for usage information")
+ print_message(_("Welcome to the hammer interactive shell"))
+ print_message(_("Type 'help' for usage information"))
+ print_message(_("Command completion is disabled on ruby < 1.9 due to compatibility problems.")) if RUBY_VERSION < "1.9"
end
def common_prefix(results)
results.delete_if{ |r| !r[0].start_with?(results[0][0][0]) }.length == results.length
end
@@ -132,7 +133,7 @@
end
end
end
- HammerCLI::MainCommand.subcommand "shell", "Interactive shell", HammerCLI::ShellCommand
+ HammerCLI::MainCommand.subcommand "shell", _("Interactive shell"), HammerCLI::ShellCommand
end