Browse Source

Add a CLI to the application class

master
C. J. Howard 3 years ago
parent
commit
963823e452
9 changed files with 137 additions and 47 deletions
  1. +1
    -1
      CMakeLists.txt
  2. +9
    -0
      src/application.cpp
  3. +10
    -1
      src/application.hpp
  4. +40
    -0
      src/debug/cli.cpp
  5. +3
    -24
      src/debug/cli.hpp
  6. +37
    -0
      src/debug/console-commands.cpp
  7. +36
    -0
      src/debug/console-commands.hpp
  8. +0
    -15
      src/debug/logger.cpp
  9. +1
    -6
      src/debug/logger.hpp

+ 1
- 1
CMakeLists.txt View File

@ -15,7 +15,6 @@ elseif(MSVC)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} /Ox") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} /Ox")
endif() endif()
# Find dependency packages # Find dependency packages
find_package(vmq REQUIRED CONFIG) find_package(vmq REQUIRED CONFIG)
find_package(dr_wav REQUIRED CONFIG) find_package(dr_wav REQUIRED CONFIG)
@ -26,6 +25,7 @@ find_package(OpenGL REQUIRED)
find_package(SDL2 REQUIRED COMPONENTS SDL2::SDL2-static SDL2::SDL2main CONFIG) find_package(SDL2 REQUIRED COMPONENTS SDL2::SDL2-static SDL2::SDL2main CONFIG)
find_package(OpenAL REQUIRED CONFIG) find_package(OpenAL REQUIRED CONFIG)
# Determine dependencies # Determine dependencies
set(STATIC_LIBS set(STATIC_LIBS
vmq vmq

+ 9
- 0
src/application.cpp View File

@ -39,6 +39,7 @@
// Debug // Debug
#include "debug/ansi-codes.hpp" #include "debug/ansi-codes.hpp"
#include "debug/console-commands.hpp"
// Resources // Resources
#include "resources/resource-manager.hpp" #include "resources/resource-manager.hpp"
@ -800,6 +801,14 @@ application::application(int argc, char** argv):
// Set overworld as active scene // Set overworld as active scene
active_scene = &overworld_scene; active_scene = &overworld_scene;
// Setup debug CLI
cli.register_command("echo", cc::echo);
cli.register_command("exit", std::function<std::string()>(std::bind(&cc::exit, this)));
std::string cmd = "echo abc 123";
logger.log(cmd + "\n");
logger.log(cli.interpret(cmd) + "\n");
} }
application::~application() application::~application()

+ 10
- 1
src/application.hpp View File

@ -33,6 +33,7 @@
// Debug // Debug
#include "debug/logger.hpp" #include "debug/logger.hpp"
#include "debug/performance-sampler.hpp" #include "debug/performance-sampler.hpp"
#include "debug/cli.hpp"
// Input // Input
#include "input/control.hpp" #include "input/control.hpp"
@ -145,6 +146,8 @@ public:
void close(int status); void close(int status);
logger* get_logger(); logger* get_logger();
cli* get_cli();
resource_manager* get_resource_manager(); resource_manager* get_resource_manager();
fsm::machine* get_state_machine(); fsm::machine* get_state_machine();
const fsm::state& get_loading_state() const; const fsm::state& get_loading_state() const;
@ -184,7 +187,8 @@ private:
// Debugging // Debugging
std::ofstream log_filestream; std::ofstream log_filestream;
logger logger;
::logger logger;
::cli cli;
// Paths // Paths
std::string data_path; std::string data_path;
@ -327,6 +331,11 @@ inline logger* application::get_logger()
return &logger; return &logger;
} }
inline cli* application::get_cli()
{
return &cli;
}
inline resource_manager* application::get_resource_manager() inline resource_manager* application::get_resource_manager()
{ {
return resource_manager; return resource_manager;

+ 40
- 0
src/debug/cli.cpp View File

@ -0,0 +1,40 @@
/*
* Copyright (C) 2020 Christopher J. Howard
*
* This file is part of Antkeeper source code.
*
* Antkeeper source code is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Antkeeper source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
*/
#include "cli.hpp"
std::string cli::interpret(const std::string& line) const
{
std::istringstream stream(line);
std::string command_name;
stream >> command_name;
if (auto it = commands.find(command_name); it != commands.end())
{
return it->second(line.substr(command_name.length() + 1));
}
return std::string();
}
void cli::unregister_command(const std::string& name)
{
if (auto it = commands.find(name); it != commands.end())
commands.erase(it);
}

+ 3
- 24
src/debug/cli.hpp View File

@ -32,9 +32,6 @@
class cli class cli
{ {
public: public:
/// String-wrapped function object
typedef std::function<std::string(const std::string&)> command_type;
/** /**
* Interprets a command line as a function invocation. * Interprets a command line as a function invocation.
* *
@ -66,6 +63,9 @@ public:
void unregister_command(const std::string& name); void unregister_command(const std::string& name);
private: private:
/// String-wrapped function object
typedef std::function<std::string(const std::string&)> command_type;
/** /**
* Parses a single argument from a string stream. * Parses a single argument from a string stream.
* *
@ -86,20 +86,6 @@ private:
std::map<std::string, command_type> commands; std::map<std::string, command_type> commands;
}; };
std::string cli::interpret(const std::string& line) const
{
std::istringstream stream(line);
std::string command_name;
stream >> command_name;
if (auto it = commands.find(command_name); it != commands.end())
{
return it->second(line.substr(command_name.length() + 1));
}
return std::string();
}
template <class T, class... Args> template <class T, class... Args>
void cli::register_command(const std::string& name, const std::function<T(Args...)>& function) void cli::register_command(const std::string& name, const std::function<T(Args...)>& function)
{ {
@ -112,12 +98,6 @@ void cli::register_command(const std::string& name, T (*function)(Args...))
commands[name] = wrap(std::function(function)); commands[name] = wrap(std::function(function));
} }
void cli::unregister_command(const std::string& name)
{
if (auto it = commands.find(name); it != commands.end())
commands.erase(it);
}
template <class T> template <class T>
T cli::parse(std::istringstream& stream) T cli::parse(std::istringstream& stream)
{ {
@ -150,4 +130,3 @@ typename cli::command_type cli::wrap(const std::function& function)
} }
#endif // ANTKEEPER_CLI_HPP #endif // ANTKEEPER_CLI_HPP

+ 37
- 0
src/debug/console-commands.cpp View File

@ -0,0 +1,37 @@
/*
* Copyright (C) 2020 Christopher J. Howard
*
* This file is part of Antkeeper source code.
*
* Antkeeper source code is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Antkeeper source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
*/
#include "console-commands.hpp"
#include "application.hpp"
namespace cc
{
std::string echo(std::string text)
{
return text;
}
std::string exit(application* app)
{
app->close(EXIT_SUCCESS);
return std::string();
}
} // namespace cc

+ 36
- 0
src/debug/console-commands.hpp View File

@ -0,0 +1,36 @@
/*
* Copyright (C) 2020 Christopher J. Howard
*
* This file is part of Antkeeper source code.
*
* Antkeeper source code is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Antkeeper source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ANTKEEPER_CONSOLE_COMMANDS_HPP
#define ANTKEEPER_CONSOLE_COMMANDS_HPP
#include <string>
class application;
namespace cc
{
std::string echo(std::string text);
std::string exit(application* app);
} // namespace cc
#endif // ANTKEEPER_CONSOLE_COMMANDS_HPP

+ 0
- 15
src/debug/logger.cpp View File

@ -54,11 +54,6 @@ void logger::log(const std::string& text)
for (std::size_t i = 0; i < tasks.size(); ++i) for (std::size_t i = 0; i < tasks.size(); ++i)
(*os) << indent; (*os) << indent;
for (const std::string& prefix: prefix_stack)
{
(*os) << prefix;
}
(*os) << (log_prefix + text + log_postfix); (*os) << (log_prefix + text + log_postfix);
@ -121,16 +116,6 @@ void logger::set_success_postfix(const std::string& postfix)
success_postfix = postfix; success_postfix = postfix;
} }
void logger::push_prefix(const std::string& prefix)
{
prefix_stack.push_back(prefix);
}
void logger::pop_prefix()
{
prefix_stack.pop_back();
}
void logger::push_task(const std::string& description) void logger::push_task(const std::string& description)
{ {
std::string message = description + "...\n"; std::string message = description + "...\n";

+ 1
- 6
src/debug/logger.hpp View File

@ -54,9 +54,6 @@ public:
void set_error_postfix(const std::string& postfix); void set_error_postfix(const std::string& postfix);
void set_success_prefix(const std::string& prefix); void set_success_prefix(const std::string& prefix);
void set_success_postfix(const std::string& postfix); void set_success_postfix(const std::string& postfix);
void push_prefix(const std::string& prefix);
void pop_prefix();
/** /**
* Pushes a task onto the stack and outputs it to the log. * Pushes a task onto the stack and outputs it to the log.
@ -95,9 +92,7 @@ private:
std::string error_prefix; std::string error_prefix;
std::string error_postfix; std::string error_postfix;
std::string success_prefix; std::string success_prefix;
std::string success_postfix;
std::list<std::string> prefix_stack;
std::string success_postfix;
std::stack<std::string> tasks; std::stack<std::string> tasks;
std::string indent; std::string indent;
bool timestamp_enabled; bool timestamp_enabled;

Loading…
Cancel
Save