c++17 header only library implementing a terminal for imgui applications
c++17 header only library implementing a terminal for imgui applications
ImTerm is a header only "terminal" library for ImGui, intended to help you build a debug console for your application. ImTerm does not implement commands on its own, but tries to make it easier to have a nice feeling terminal where you can type in your custom commands, and aims to be utf8 friendly. It also comes with an optional spdlog integration if you happen to use it.
ImTerm features revolves mainly around completion:
tab pressIf you want to type in !: or !! if your command argument, you'll have to escape one of the exclamation marks with \
Typing command_name Hello, World! will call command_name with two arguments, Hello, and World!.
To merge the two arguments, simply add surrounding quotes: command_name "Hello, World!".
ImTerm only has ImGui as mandatory dependency, and has spdlog as an optional depency. If you use spdlog, then you'll get an easier integration, otherwise that's not a problem, and you won't need to add spdlog to your project.
Since ImTerm is a header only library, appart from its ImGui dependency (that you probably already use if you came there)
you'll just need to get ImTerm headers, #include , and you should be done!
ImTerm requires C++17 or above.
Adding commands to an ImTerm::terminal is done via the types command_type_cref, command_type and argument_type
The command_type type (which I should rather call ImTerm::command_t>) is a structured used to represent a command, and contains the following attributes:
std::string_view)std::string_view) command_type_cref is the type std::reference_wrapper (ie: a const reference that you can store in a vector).
The command callback function can do whatever it wants and takes as a parameter an argument_t>, containing:
TerminalHelper::value_type, can be void)std::vectorThe completion callback function takes the same type of argument and should return an std::vector containing a list of
possible contextual completion (you may return an empty vector if you don't want to autocomplete user's inputs).
To use ImTerm::terminal's class, you must supply it a TerminalHelper.
The TerminalHelper class is responsible for command management. Because It is expected that your commands will need some kind of argument
to interact with the rest of your application, your TerminalHelper must define the type TerminalHelper::value_type.
That value will be passed to your commands as part of their arguments.
It must implement the following methods:
std::vector find_command_by_prefix(std::string_view prefix), which shall return the list of commands starting by prefix (you might want to use ImTerm::misc::prefix_search)std::vector find_command_by_prefix(const char* beg, const char* end), which shall return the list of commands starting by the string formed by [beg, end) (that might just disappear from the needed methods soon)std::vector list_commands(), which shall return the list of all commandsstd::optional format(std::string msg, message::type msg_type), which shall format a string to your liking, or return an empty optional so that it won't be logged (ImTerm::terminal will invoke this methods when it wants to emit feedback to the user).Of course, it's a bit of a bummer to have to implement all those methods, so if you want you can also simply inherit from ImTerm::basic_terminal_helper
(defined in imterm/terminal_helpers.hpp), which does all that for you. Afterward, you just have to add your commands using basic_terminal_helper::add_command_(const command_type&)
Here is a basic example of what a TerminalHelper can look like:
class terminal_helper_example : ImTerm::basic_terminal_helper {
public:
static std::vector no_completion(std::string_view) { return {}; }
// clears the logging screen. argument_type is aliased in ImTerm::basic_terminal_helper
static void clear(argument_type& arg) {
arg.term.clear();
}
// prints the text passed as parameter to the logging screen. argument_type is aliased in ImTerm::basic_terminal_helper
static void echo(argument_type& arg) {
if (arg.command_line.size() & term)``.
This method will be invoked right after the instantiation of ImTerm::terminal if it exists, and the passed reference will be valid throughout the whole lifetime of
the terminal.
# Author
Lucas Lazare, a computer engineering student.
# License
This project is under the MIT license.No open issues yet, or sync has not completed.