diff --git a/lib/ui/command.rb b/lib/ui/command.rb
index e07de8c..3a8ec8e 100644
--- a/lib/ui/command.rb
+++ b/lib/ui/command.rb
@@ -1,5 +1,20 @@
class IMICFPS
class Commands
+ module Style
+ def self.error(string)
+ "#{string}"
+ end
+ def self.warn(string)
+ "#{string}"
+ end
+ def self.notice(string)
+ "#{string}"
+ end
+ def self.highlight(string, color = "5555ff")
+ "#{string}"
+ 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 #{command} 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
\ No newline at end of file
diff --git a/lib/ui/commands/debug_command.rb b/lib/ui/commands/debug_command.rb
new file mode 100644
index 0000000..6c1873c
--- /dev/null
+++ b/lib/ui/commands/debug_command.rb
@@ -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
\ No newline at end of file
diff --git a/lib/ui/commands/help_command.rb b/lib/ui/commands/help_command.rb
index 6179f2b..0650bc0 100644
--- a/lib/ui/commands/help_command.rb
+++ b/lib/ui/commands/help_command.rb
@@ -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