processor/command/base/cmd.rb in trepanning-0.1.0 vs processor/command/base/cmd.rb in trepanning-0.1.1
- old
+ new
@@ -2,10 +2,11 @@
# Copyright (C) 2010, 2011 Rocky Bernstein <rockyb@rubyforge.net>
# Base class of all commands. Code common to all commands is here.
# Note: don't end classname with Command (capital C) since main
# will think this a command name like QuitCommand
require 'columnize'
+require_relative '../../../app/complete'
class Trepan
class Command
attr_accessor :core, :proc
@@ -57,12 +58,12 @@
def msg(message, opts={})
@proc.msg(message, opts)
end
# Convenience short-hand for @dbgr.intf[-1].msg_nocr
- def msg_nocr(msg)
- @proc.msg_nocr(msg, opts={})
+ def msg_nocr(msg, opts={})
+ @proc.msg_nocr(msg, opts)
end
def my_const(name)
# Set class constant SHORT_HELP to be the first line of HELP
# unless it has been defined in the class already.
@@ -99,7 +100,49 @@
:SHORT_HELP
else :HELP
end
my_const(help_constant_sym)
end
+
+ # Define a method called 'complete' on the singleton class.
+ def self.completion(ary)
+ self.send(:define_method,
+ :complete,
+ Proc.new {|prefix|
+ Trepan::Complete.complete_token(ary, prefix) })
+ end
end
+end
+if __FILE__ == $0
+ class Trepan
+ class CmdProcessor
+ def initialize(dbgr)
+ end
+ def confirm(message, default)
+ p ['confirm: ', message, default]
+ end
+ def errmsg(message, opts)
+ p ['err:', message, opts]
+ end
+ def msg(message, opts)
+ p [message, opts]
+ end
+ def msg_nocr(message, opts)
+ p ['nocr: ', message, opts]
+ end
+ def section(message, opts)
+ p ['section: ', message, opts]
+ end
+ end
+ class Command::Test < Trepan::Command
+ NAME = 'test'
+ CATEGORY = 'testcategory'
+ completion %w(a aa ab ba aac)
+ end
+ end
+ proc = Trepan::CmdProcessor.new(nil)
+ cmd = Trepan::Command::Test.new(proc)
+ %w(confirm errmsg msg msg_nocr section).each do |meth|
+ cmd.send(meth, 'test', nil)
+ end
+ p cmd.complete('aa')
end