mirror of
https://github.com/cyberarm/i-mic-fps.git
synced 2025-12-15 15:42:35 +00:00
Added DebugCommand, added support for 'subcommands'
This commit is contained in:
@@ -1,5 +1,20 @@
|
||||
class IMICFPS
|
||||
class Commands
|
||||
module Style
|
||||
def self.error(string)
|
||||
"<c=ff5555>#{string}</c>"
|
||||
end
|
||||
def self.warn(string)
|
||||
"<c=ff5500>#{string}</c>"
|
||||
end
|
||||
def self.notice(string)
|
||||
"<c=55ff55>#{string}</c>"
|
||||
end
|
||||
def self.highlight(string, color = "5555ff")
|
||||
"<c=#{color}>#{string}</c>"
|
||||
end
|
||||
end
|
||||
|
||||
class Command
|
||||
def self.inherited(subclass)
|
||||
@list ||= []
|
||||
@@ -21,10 +36,39 @@ class IMICFPS
|
||||
if found_command
|
||||
found_command.handle(arguments, console)
|
||||
else
|
||||
console.stdin("Command <c=ff5555>#{command}</c> not found.")
|
||||
console.stdin("Command #{Style.error(command)} not found.")
|
||||
end
|
||||
end
|
||||
|
||||
def self.find(command)
|
||||
@commands.detect { |cmd| cmd.command == command.to_sym }
|
||||
end
|
||||
|
||||
def self.list_commands
|
||||
@commands
|
||||
end
|
||||
|
||||
def initialize
|
||||
@store = {}
|
||||
@subcommands = []
|
||||
|
||||
setup
|
||||
end
|
||||
|
||||
def setup; end
|
||||
|
||||
def subcommand(command, type)
|
||||
@subcommands << SubCommand.new(self, command, type)
|
||||
end
|
||||
|
||||
def get(key)
|
||||
@store.dig(key)
|
||||
end
|
||||
|
||||
def set(key, value)
|
||||
@store[key] = value
|
||||
end
|
||||
|
||||
def group
|
||||
raise NotImplementedError
|
||||
end
|
||||
@@ -37,9 +81,95 @@ class IMICFPS
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
def handle_subcommand(arguments, console)
|
||||
if arguments.size == 0
|
||||
console.stdin(usage)
|
||||
return
|
||||
end
|
||||
subcommand = arguments.delete_at(0)
|
||||
|
||||
found_command = @subcommands.detect { |cmd| cmd.command == subcommand.to_sym }
|
||||
if found_command
|
||||
found_command.handle(arguments, console)
|
||||
else
|
||||
console.stdin("Unknown subcommand #{Style.error(subcommand)} for #{Style.highlight(command)}")
|
||||
end
|
||||
end
|
||||
|
||||
def usage
|
||||
raise NotImplementedError
|
||||
end
|
||||
end
|
||||
|
||||
class SubCommand
|
||||
def initialize(parent, command, type)
|
||||
@parent = parent
|
||||
@command = command
|
||||
@type = type
|
||||
end
|
||||
|
||||
def command
|
||||
@command
|
||||
end
|
||||
|
||||
def handle(arguments, console)
|
||||
case @type
|
||||
when :boolean
|
||||
case arguments.last
|
||||
when "", nil
|
||||
var = @parent.get(command.to_sym) ? @parent.get(command.to_sym) : false
|
||||
console.stdin("#{command}: #{Style.highlight(var)}")
|
||||
when "on"
|
||||
var = @parent.set(command.to_sym, true)
|
||||
console.stdin("#{command} => #{Style.highlight(var)}")
|
||||
when "off"
|
||||
var = @parent.set(command.to_sym, false)
|
||||
console.stdin("#{command} => #{Style.highlight(var)}")
|
||||
end
|
||||
when :string
|
||||
case arguments.last
|
||||
when "", nil
|
||||
var = @parent.get(command.to_sym) ? @parent.get(command.to_sym) : "\"\""
|
||||
console.stdin("#{command}: #{Style.highlight(var)}")
|
||||
else
|
||||
var = @parent.set(command.to_sym, arguments.last)
|
||||
console.stdin("#{command} => #{Style.highlight(var)}")
|
||||
end
|
||||
when :integer
|
||||
case arguments.last
|
||||
when "", nil
|
||||
var = @parent.get(command.to_sym) ? @parent.get(command.to_sym) : "nil"
|
||||
console.stdin("#{command}: #{Style.highlight(var)}")
|
||||
else
|
||||
var = @parent.set(command.to_sym, arguments.last.to_i)
|
||||
console.stdin("#{command} => #{Style.highlight(var)}")
|
||||
end
|
||||
when :decimal
|
||||
case arguments.last
|
||||
when "", nil
|
||||
var = @parent.get(command.to_sym) ? @parent.get(command.to_sym) : "nil"
|
||||
console.stdin("#{command}: #{Style.highlight(var)}")
|
||||
else
|
||||
var = @parent.set(command.to_sym, arguments.last.to_f)
|
||||
console.stdin("#{command} => #{Style.highlight(var)}")
|
||||
end
|
||||
else
|
||||
raise RuntimeError
|
||||
end
|
||||
end
|
||||
|
||||
def usage
|
||||
case @type
|
||||
when :boolean
|
||||
"#{Style.highlight(command)} #{Style.notice("[on|off]")}"
|
||||
when :string
|
||||
"#{Style.highlight(command)} #{Style.notice("[on|off]")}"
|
||||
when :integer
|
||||
"#{Style.highlight(command)} #{Style.notice("[on|off]")}"
|
||||
when :decimal
|
||||
"#{Style.highlight(command)} #{Style.notice("[on|off]")}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
31
lib/ui/commands/debug_command.rb
Normal file
31
lib/ui/commands/debug_command.rb
Normal file
@@ -0,0 +1,31 @@
|
||||
class IMICFPS
|
||||
class Commands
|
||||
class DebugCommand < Command
|
||||
def group
|
||||
:global
|
||||
end
|
||||
|
||||
def command
|
||||
:debug
|
||||
end
|
||||
|
||||
def setup
|
||||
subcommand(:boundingboxes, :boolean)
|
||||
subcommand(:wireframe, :boolean)
|
||||
subcommand(:fps, :boolean)
|
||||
subcommand(:stats, :boolean)
|
||||
subcommand(:motd, :string)
|
||||
subcommand(:mode, :integer)
|
||||
subcommand(:gravity, :decimal)
|
||||
end
|
||||
|
||||
def handle(arguments, console)
|
||||
handle_subcommand(arguments, console)
|
||||
end
|
||||
|
||||
def usage
|
||||
string = "debug\n #{@subcommands.map { |sub| sub.usage }.join("\n ")}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,9 +1,6 @@
|
||||
class IMICFPS
|
||||
class Commands
|
||||
class HelpCommand < Command
|
||||
def initialize
|
||||
end
|
||||
|
||||
def group
|
||||
:global
|
||||
end
|
||||
@@ -13,11 +10,17 @@ class IMICFPS
|
||||
end
|
||||
|
||||
def handle(arguments, console)
|
||||
console.stdin(usage)
|
||||
console.stdin(usage(arguments.first))
|
||||
end
|
||||
|
||||
def usage
|
||||
"HELP\ncommand [arguments]\ncommand subcommand [argument]"
|
||||
def usage(command = nil)
|
||||
if command
|
||||
if cmd = Command.find(command)
|
||||
cmd.usage
|
||||
end
|
||||
else
|
||||
"Available commands:\n#{Command.list_commands.map { |cmd| "#{Style.highlight(cmd.command)}" }.join(', ')}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user