Repeat HN: Commandline, a C++ library for async terminal I/O

78
Repeat HN: Commandline, a C++ library for async terminal I/O

Linux Build
Windows Build
CodeFactor

A base-platfrom C++ commandline for spend in servers and terminal chat instrument. Provides very easy asynchronous enter/output.

Callback-primarily primarily based completely i/o supported, as successfully as historical past, custom prompts, and extra (investigate cross-take a look at aspects)

Instance

#embody "commandline.h"

int major() {
    Commandline com;
    while (appropriate) {
        if (com.has_command()) {
            auto whisper=com.get_command();
            com.write("got whisper: " + whisper);
        }
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
        com.write("right here's a message written with com.write");
    }
}

Consequence:

main.cpp demo gif

Aspects

  • Asynchronous I/O:
    The user can model into stdin while the program spams output onto stdout, and not utilizing a visual issues. A whisper or message is committed with the Enter/Return key.

  • Thread-safety:
    All output is buffered internally and stable with mutexes, so write() will even simply additionally be known as by many threads at the same time with out issues. Efficiency-wise this makes shrimp affect, in our attempting out, as in comparison to extraordinary printf() or std::cout logging (it be much quicker than the latter in overall scenarios).

  • History:
    History of all instructions entered is saved, if the historical past used to be enabled with Commandline::enable_history(). The historical past will even simply additionally be navigated respect anticipated, with the up- and down-arrow keys, as successfully as cleared by the program, saved and restored, and extra.

  • Cursor motion:
    Despite the fact that an internal buffer is used (and the same outdated buffered enter is disabled), the cursor will even simply additionally be moved as extraordinary with the left- and shapely-arrow keys, and moved to the entrance and abet with the HOME and END keys.

  • has_command() and get_command(), or on_command callback:
    All these are supported as programs to come by a line that used to be entered by the user, allowing many programs to combine the library into unusual applications.

  • Output to file:
    Helps specifying a file to write down all output into, as a logging mechanism!

  • Imperfect-platform:
    Works on any POSIX system with a terminal that supports ANSI (the total ones yow will locate, potentially), as successfully as WinAPI console applications and Microsoft CMD.

create

Scamper cmake . .
Then:

  • On Unix, this might perhaps occasionally generate a unix Makefile, which you are going to even then dawdle with come by.
  • On Residence windows, this might perhaps occasionally generate a VS venture which you are going to even delivery.

It will need to salvage then constructed the library, which you are going to even hyperlink in opposition to.

You are going to even additionally put add_subdirectory(commandline) to your CMakeLists, even as you happen to clone the repo in the same folder.

NOTE: To create the instance from above (included in major.cpp), space BUILD_EXAMPLES to ON for your CMake configuration.

Why no longer X?

Why no longer GNU getline?

As a consequence of it be GPL licensed, which makes it fascinating to combine into extra permissive licensed or closed-source / industrial projects. commandline goals to be usable by anybody, so long as MIT license phrases are followed.

Why no longer beef up autocomplete?

commandline will come by (no longer fundamental) autocomplete beef up in the long dawdle, it be being labored on.

spend

  1. Contain a Commandline instance. A immediate can optionally be passed in this constructor, or by the usage of set_prompt.
  1. Question for glossy instructions in a loop – this must restful in overall be your major server loop or one thing the same.
bool is_running=appropriate;
while (is_running) {
  // test if one thing used to be entered
  if (com.has_command()) {
    // steal the whisper from the queue
    // characterize: undefined if has_command wasn't checked beforehand
    std::string whisper=com.get_command();
    if (whisper=="quit") {
      is_running=false;
    }
  }
}
  1. To jot down to the commandline, spend Commandline::write.
com.write("hello, world!");

How does it work?

To beef up reading and writing at the same time, it be the usage of VT100 ANSI destroy out codes. This suggests that, on windows, it be fundamental to allow those on your CMD terminal.

Earlier than a glossy line is output into stdout, the sizzling enter line is cleared, the output line is written, and the enter line is rewritten in the road beneath, positioning the cursor where it used to be, as if nothing came about. This finally ends up in the looks that the enter line is mounted beneath the output, which is a extremely pure model.

In most terminals and in Microsoft’s CMD.exe (as successfully as standalone WinAPI console applications), this occurs suddenly and no visual system faults appear, which makes it very fun and tranquil to make spend of.

Susceptible In

Commandline is used in https://github.com/BeamMP/BeamMP-Server!

Read More

Charlie Layers
WRITTEN BY

Charlie Layers

Fill your life with experiences so you always have a great story to tell