olcmenuoption.rb
znmud-0.0.1/lib/olc/olcmenuoption.rb · 2.5 KB
From the archive znmud-minimal-0.0.1.tar.gz
Download the archive « Back to the archive contents
# File: olcmenuoption.rb
# Author: Craig Smith
#
# This source code copyright (C) 2009 Craig Smith
# All rights reserved.
#
# Released under the terms of the GNU Public License
# See COPYING file for additional information.
#
require 'utility/log'
# Options for the OlcMenu
class OlcMenuOption
logger "DEBUG"
attr_accessor :pick, :name, :target, :obj, :func, :regex, :match_case, :toggle, :get_string, :multi_line, :numbered_array, :numbered_hash, :named, :active_hash
def initialize
self.pick = nil
self.name = "Undefined Option:"
self.target = nil
self.toggle = false
self.obj = nil
self.func = nil
self.regex = nil
self.named = false
self.match_case = false
self.get_string = false
self.multi_line = false
self.numbered_array = false
self.numbered_hash = false
self.active_hash = {}
end
# Determines if m matches pick
# [+m+] message from user input
def is_match?(m)
return false if m.size < 1
match = false
if numbered_array or numbered_hash
if m.to_i > 0
idx = m.to_i
if idx < 0 or idx > target.size
return false
else
return true
end
end
return false
end
return false if not pick
pick.gsub!(/[# ]/,'')
if regex
if match_case
match = true if m=~/#{regex}/
else
match = true if m=~/#{regex}/i
end
else
if match_case
match = true if m == pick
else
match = true if m=~/^#{pick}/i
end
end
match
end
# Print menu option
# [+opt1+] Optional Argument
# [+opt2+] Optional Argument
def to_s(opt1=nil,opt2=nil)
msg = ""
if numbered_array
cnt = 1
# When a toggled numbered_array opt1=defaults opt2=current
if toggle and opt1 and opt2
self.target = opt1.concat(opt2)
self.target.uniq!
end
target.each do |item|
mark = ""
oname = ""
if toggle
mark="*" if opt2.include? item
end
if named
oid = item.to_i
if oid > 0
oname = " (#{get_object(oid).shortname})"
end
end
msg << "[color Yellow]#{cnt}[/color]) [color Green]#{mark}#{item}[/color] #{oname}\n"
cnt += 1
end
return msg
elsif numbered_hash
cnt = 1
target.each do |key|
msg << "[color Yellow]#{cnt}[/color]) [color Green]#{key}[/color]"
msg << " (#{active_hash[key]})" if active_hash.has_key? key
msg << "\n"
cnt += 1
end
return msg
end
if target
msg = "[color Yellow]#{pick}[/color]) #{name}: [color Green]#{target.inspect}[/color]\n"
else
msg = "[color Yellow]#{pick}[/color]) #{name}\n"
end
msg
end
def get_object(oid)
Engine.instance.db.get(oid)
end
end