A C++ commandline for use in servers and chat software. Provides very simple asynchronous input/output.

Related tags

Graphics commandline
Overview

commandline

Linux Build Windows Build

CodeFactor

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:

main.cpp demo gif

How to build

Run cmake . . Then:

  • On Unix, this will generate a unix Makefile, which you can then run with make.
  • 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

  1. Construct a Commandline instance. A prompt can optionally be passed in this constructor, or via set_prompt.
Commandline com;
  1. 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;
    }
  }
}
  1. To write to the commandline, use Commandline::write.
com.write("hello, world!");
Issues
  • define VS_STARTUP_PROJECT in cmake

    define VS_STARTUP_PROJECT in cmake

    With this property set, you can Ctrl+F5 to run the test app It will launch test instead of attempt to launch ALL_BUILD as VS does by default screenshot

    opened by Pospelove 3
  • User-defined callback for logging

    User-defined callback for logging

    Logging has been replaced with the user-defined callback on_write. on_write will only be called if defined. Cleaned up some #includes. Example added to main.cpp.

    enhancement 
    opened by Hammersamatom 2
  • Multiple fixes and improvements

    Multiple fixes and improvements

    Cursor position is now passed into on_autocomplete DEL key support Shift+Tab support Fix on_autocomplete deadlock Add autocomplete cancels when pressing backspace

    opened by 20dka 1
  • Rewrite CMakeLists

    Rewrite CMakeLists

    The current CMakeLists has a few bad decisions, the wrong version requirement, and a few other issues.

    Rewrite it to make it conform to a newer standard, while going down in required version number (to support older codebases, see #10).

    opened by lionkor 1
  • Move to C++11

    Move to C++11

    The codebase does not require modern C++ (14, 17), as far as I can see. I'd like to see us move all code to C++11, and enforce this in the CMakeLists.

    As a result, commandline can be integrated into more C++ projects.

    opened by lionkor 1
  • History is broken on windows

    History is broken on windows

    When pressing Up-Arrow on the keyboard, the last entered command should appear. Pressing this repeatedly will go through all entered commands in the current session in reverse-chronological order. Down-Arrow then should do the opposite, ending up at the current prompt.

    It doesn't do this properly on (all?) windows machines (CMD).

    bug 
    opened by lionkor 1
  • Reduce cpu usage when stdin is not an tty

    Reduce cpu usage when stdin is not an tty

    On Linux stdin could be /dev/null or a pipe for example when using docker or systemd services. This leads to high cpu usage by constant reading null bytes. To fix this you dont need to spawn the input thread if isatty returns false. See https://man7.org/linux/man-pages/man3/isatty.3.html

    opened by Worty 1
  • Make delete key work

    Make delete key work

    Currently, the delete key does not work. This wasn't implemented originally, because cursor movement didn't exist.

    • Find out what the escape sequence is for Delete on Windows+Linux
    • Implement removal of a character when Delete is pressed
    enhancement 
    opened by lionkor 0
  • Make tab autocomplete cancellable, disableable

    Make tab autocomplete cancellable, disableable

    • Autocomplete should be cancellable (one of the suggestions should be the original, maybe?)
    • Autocomplete should be disableable entirely (maybe by leaving on_autocomplete nullptr?)
    enhancement 
    opened by lionkor 0
  • Cursor left-right movement

    Cursor left-right movement

    This is partially supported already, just needs to be implemented. The idea is moving the cursor via left & right arrow keys. See m_cursor_pos for this.

    enhancement good first issue 
    opened by lionkor 0
  • Set cursor position from on_autocomplete callback

    Set cursor position from on_autocomplete callback

    on_autocomplete currently passes in the current cursor position, in order to allow autocomplete within a word/sentence at a specific point. This is nice, but not as useful as it could be, because the cursor will end up at the end of the autocompletion, which is the end of the entire input.

    We need a way to set the cursor's position generally, like c.set_cursor_position(), but it also has to be understood by the caller of on_autocomplete whether or not this has been called. If it was called, dont put the cursor at the end, and if it wasn't, do.

    enhancement help wanted good first issue 
    opened by lionkor 0
  • Fatal error when building for x64-osx

    Fatal error when building for x64-osx

    When building x64-osx (vcpkg):

    /Users/vagrant/Data/buildtrees/commandline/src/a4454e07a9-613e01ad85.clean/commandline.cpp:11:10: fatal error: 'conio.h' file not found
    #include <conio.h>
             ^~~~~~~~~
    1 error generated.
    

    Originally posted by @JackBoosY in https://github.com/microsoft/vcpkg/issues/23471#issuecomment-1063590831

    build error 
    opened by lionkor 1
  • Use WinAPI functions on Windows

    Use WinAPI functions on Windows

    Currently we're using ANSI escape codes on both Unix/Linux/MacOS and Windows, which causes issues in Windows (of course).

    They decided that Powershell and the Windows Terminal (!= CMD) should not behave properly when presented with ANSI escape codes, even with that feature explicitly enabled.

    So, instead of getting really angry with windows' decisions, we are just going to use their WinAPI features to do what we do with ANSI escape sequences currently.

    Snarkiness aside, the goal is to replace all ANSI escape codes (\0x1b prefixed stuff in all calls to printf) with the equivalent Windows API function call.

    enhancement 
    opened by lionkor 0
Releases(v2.0.0)
  • v2.0.0(Mar 23, 2022)

    Thanks

    Thanks to @20dka for making these changes and continuously putting so much effort in to improve this project! Also thanks to Reddit's r/cpp who provided plenty of feedback, excitement, and motivation to make this all happen!

    What's Changed

    • Add DEL key (Delete) functionality
    • Add Shift+Tab to reverse-cycle through autocompletion suggestions
    • Add escape key (windows) or backspace (windows and Linux) to cancel the autocompletion suggestion
    • Changed on_autocomplete signature (incompatible with 1.x, hence major version increase) to add cursor position at the time of TAB key press
    • Fix printing of control characters if accessing empty history (via up- and down-arrow keys)
    • Fix deadlock when attempting to .write() in on_autocomplete

    Full Changelog: https://github.com/lionkor/commandline/compare/v1.2.0...v2.0.0

    Source code(tar.gz)
    Source code(zip)
  • v1.2.0(Mar 15, 2022)

    What's Changed

    • Add tab autocomplete (fix #7) by @20dka in https://github.com/lionkor/commandline/pull/12

    Full Changelog: https://github.com/lionkor/commandline/compare/v1.1.1...v1.2.0

    Source code(tar.gz)
    Source code(zip)
  • v1.1.1(Mar 14, 2022)

    All changes: https://github.com/lionkor/commandline/compare/v1.1.0...v1.1.1

    • replace sleep()s in the library threads with condition variables, reducing CPU usage
    • clarify why sleep() is used in the example
    Source code(tar.gz)
    Source code(zip)
  • v1.1.0(Mar 12, 2022)

    All Changes: https://github.com/lionkor/commandline/compare/v1.0.0...v1.1.0

    • Got rid of C++17 requirement, now it's C++11
    • Rewrote CMakeLists - it was written a long time ago and needed a rework. Is functionally the same, requires CMake >= 3.1, and uses FindThreads in order to find pthread on *nix.
    Source code(tar.gz)
    Source code(zip)
  • v1.0.0(Mar 8, 2022)

    Features:

    • safely usable from any number of threads without the need for mutexes (all handled internally)
    • async i/o using C++ Threads
    • history (up- and down arrow keys)
    • cursor movement (left, right, home, end)
    • Windows (cmd) and Linux support
    • Implicitly turns off when stdin/stdout is not a tty, to prevent CPU spin
    • Simultaneous output to file
    • Custom prompts
    • Getting and setting history
    • on_command callback
    • configurable history size

    Anyone is welcome to contribute new features or request features by opening an issue!

    Thanks to all contributors for helping with this release, namely

    • @20dka for adding left- and right cursor movement support, bug fixes, extensive windows testing
    • @Worty for adding isatty integration
    • @tsunami650 for adding first MacOS compilation support

    Also thanks to @BeamMP for allowing me to integrate this library into their game server for their terminal console, testing this library to hell and back with all sorts of different setups, piping stdin and stdout, and generally running it on anything with electricity!

    Source code(tar.gz)
    Source code(zip)
Owner
Lion
C++, C and Delphi programmer with a part-time job writing airport simulation software. Always looking for interesting projects to contribute to.
Lion
Vis: Asynchronous 3D Visualization Tool

English | 简体中文 Vis: Asynchronous 3D Visualization Tool Vis 是一款交互式异步3D可视化工具,旨在让3D视觉和机器人应用开发更简单。 其核心功能包括: 图形绘制 3D模型文件导入 多种交互工具 Gzimo 安装 Linux # 安装必要的依

RVBUST 108 May 13, 2022
A multi-platform library for OpenGL, OpenGL ES, Vulkan, window and input

GLFW Introduction GLFW is an Open Source, multi-platform library for OpenGL, OpenGL ES and Vulkan application development. It provides a simple, platf

GLFW 9k May 12, 2022
Software ray tracer written from scratch in C that can run on CPU or GPU with emphasis on ease of use and trivial setup

A minimalist and platform-agnostic interactive/real-time raytracer. Strong emphasis on simplicity, ease of use and almost no setup to get started with

Arnon Marcus 42 Apr 23, 2022
Ipsys Particle System Yey letS go, very cool particle system generator and fast renderer

ipsys - Ipsys Particle System Yey letS go About Ipsys is a piece of software that focuces on running and displaying cool randomly generated particule

Anima Libera 5 Nov 24, 2021
SMAA is a very efficient GPU-based MLAA implementation (DX9, DX10, DX11 and OpenGL)

SMAA is a very efficient GPU-based MLAA implementation (DX9, DX10, DX11 and OpenGL), capable of handling subpixel features seamlessly, and featuring an improved and advanced pattern detection & handling mechanism.

Jorge Jimenez 802 May 11, 2022
DirectX 11 and 12 library that provides a scalable and GCN-optimized solution for deferred shadow filtering

AMD ShadowFX The ShadowFX library provides a scalable and GCN-optimized solution for deferred shadow filtering. Currently the library supports uniform

GPUOpen Effects 161 Apr 16, 2022
Utility on top of the Flutter Driver API that facilitates measuring the performance of your app in an automated way created by Very Good Ventures 🦄

Very Good Performance Developed with ?? by Very Good Ventures ?? Utility on top of the Flutter Driver API that facilitates measuring the performance o

Very Good Open Source 64 Apr 23, 2022
A very stupid window manager.

vswm - very stupid window manager ================================= Probably the most stupid window manager ever created, written over an ancient rel

Felix Hägglund Wennergren 32 Apr 10, 2022
Legion Low Level Rendering Interface provides a graphics API agnostic rendering interface with minimal CPU overhead and low level access to verbose GPU operations.

Legion-LLRI Legion-LLRI, or “Legion Low Level Rendering Interface” is a rendering API that aims to provide a graphics API agnostic approach to graphic

Rythe Interactive 25 Mar 8, 2022
Ksnip is a Qt-based cross-platform screenshot tool that provides many annotation features for your screenshots.

Ksnip is a Qt-based cross-platform screenshot tool that provides many annotation features for your screenshots.

ksnip 1.3k May 14, 2022
Pathway is an Android library that provides new functionalities around the graphics Path API.

Pathway is an Android library that provides new functionalities around the graphics Path API.

Romain Guy 93 May 8, 2022
SPIRV-Reflect is a lightweight library that provides a C/C++ reflection API for SPIR-V shader bytecode in Vulkan applications.

SPIRV-Reflect SPIRV-Reflect is a lightweight library that provides a C/C++ reflection API for SPIR-V shader bytecode in Vulkan applications. SPIRV-Ref

The Khronos Group 396 May 7, 2022
Pencil2D is an animation/drawing software for Windows, macOS, Linux, and FreeBSD.

Pencil2D is an animation/drawing software for Windows, macOS, Linux, and FreeBSD. It lets you create traditional hand-drawn animation (cartoon) using both bitmap and vector graphics. Pencil2D is free and open source.

Pencil2D 1.1k May 11, 2022
Lightweight OpenCL-Wrapper to greatly simplify OpenCL software development with C++ while keeping functionality and performance

OpenCL-Wrapper OpenCL is the most powerful programming language ever created. Yet the OpenCL C++ bindings are very cumbersome and the code overhead pr

Moritz Lehmann 23 May 12, 2022
PainterEngine is a application/game engine with software renderer,PainterEngine can be transplanted to any platform that supports C

PainterEngine is a application/game engine with software renderer,PainterEngine can be transplanted to any platform that supports C

DBinary 1.3k May 11, 2022
Software RayTracing engine written in C++ without the usage of any libraries.

Software Raytracing Engine This is a software unidirectional raytracing engine, fully written in C++17, with no dependencies or external libraries use

Makar Ivashko 1 May 4, 2022
ZBar Bar Code Reader is an open source software suite for reading bar codes from various sources

ZBar Bar Code Reader is an open source software suite for reading bar codes from various sources

null 2.3k May 9, 2022
Dust3D is a cross-platform open-source 3D modeling software

Dust3D is a cross-platform open-source 3D modeling software. Auto UV unwrapping, auto rigging with PBR Material support, pose and motion authoring all in one.

Jeremy HU 2.4k May 11, 2022