commandline
A C++ commandline for use in servers and terminal chat software. Provides very simple asynchronous input/output.
Supports reading and writing at the same time, using VT100 ANSI escape codes. This means that, on windows, you need to enable those for your CMD terminal.
Has a history implementation, so you can enable history with enable_history()
, and then navigate it with Esc[A (Up-Arrow) and Esc[B (Down-Arrow). You can set the history size with set_history_size
, by default this is std::numeric_limits<size_t>::max()
.
A prompt can be passed in the constructor, or via set_prompt
.
Example
#include "commandline.h"
int main() {
Commandline com;
while (true) {
if (com.has_command()) {
auto command = com.get_command();
com.write("got command: " + command);
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
com.write("this is a message written with com.write");
}
}
Result:
How to build
Run cmake .
. Then:
- On Unix, this will generate a unix
Makefile
, which you can then run withmake
. - On Windows, this will generate a VS project which you can open.
It should have then built the library, which you can link against.
You could also put add_subdirectory(commandline)
to your CMakeLists, if you clone the repo in the same folder.
How to use
- Construct a
Commandline
instance. A prompt can optionally be passed in this constructor, or viaset_prompt
.
Commandline com;
- Query for new commands in a loop - this should usually be your main server loop or something similar.
bool is_running = true;
while (is_running) {
// check if something was entered
if (com.has_command()) {
// grab the command from the queue
// note: undefined if has_command wasn't checked beforehand
std::string command = com.get_command();
if (command == "quit") {
is_running = false;
}
}
}
- To write to the commandline, use
Commandline::write
.
com.write("hello, world!");