Giter Club home page Giter Club logo

cpp-linenoise's Introduction

cpp-linenoise

Multi-platform (Unix, Windows) C++ header-only linenoise-based readline library.

This library gathered code from following excellent libraries, clean it up, and put it into a C++ header file for convenience.

The licenses for the libraries are included in linenoise.hpp.

Usage

#include "linenoise.hpp"

...

const auto path = "history.txt";

// Setup completion words every time when a user types
linenoise::SetCompletionCallback([](const char* editBuffer, std::vector<std::string>& completions) {
    if (editBuffer[0] == 'h') {
        completions.push_back("hello");
        completions.push_back("hello there");
    }
});

// Enable the multi-line mode
linenoise::SetMultiLine(true);

// Set max length of the history
linenoise::SetHistoryMaxLen(4);

// Load history
linenoise::LoadHistory(path);

while (true) {
    // Read line
    std::string line;
    auto quit = linenoise::Readline("hello> ", line);

    if (quit) {
        break;
    }

    cout <<  "echo: '" << line << "'" << endl;

    // Add text to history
    linenoise::AddHistory(line.c_str());
}

// Save history
linenoise::SaveHistory(path);

API

namespace linenoise;

std::string Readline(const char* prompt);

void SetMultiLine(bool multiLineMode);

typedef std::function<void (const char* editBuffer, std::vector<std::string>& completions)> CompletionCallback;

void SetCompletionCallback(CompletionCallback fn);

bool SetHistoryMaxLen(size_t len);

bool LoadHistory(const char* path);

bool SaveHistory(const char* path);

bool AddHistory(const char* line);

const std::vector<std::string>& GetHistory();

Tested compilers

  • Visual Studio 2015
  • Clang 3.5
  • GCC 6.3.1

License

BSD license (© 2015 Yuji Hirose)

cpp-linenoise's People

Contributors

antonzub1 avatar cmollekopf avatar egorpugin avatar hhirsch avatar jsoref avatar mmurrian avatar octobanana avatar werat avatar xorhash avatar yhirose avatar zeux avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cpp-linenoise's Issues

TERM=null should be unsupported?

In isUnsupportedTerm I suggest changing it to:

if (term == NULL) return true;

This is mainly because CLion's integrated console does not set TERM and does not work with line noise or cpp-linenoise. Instead of getting the prompt we the following noise, plus the enter key is not picked up - so readline never exits;

screen shot 2017-03-28 at 14 56 07

By making this small change, the CLion console works:

screen shot 2017-03-28 at 14 58 36

Differentiating blank line and Ctrl+C

Is there any way to differentiate between whether user entered blank line or Ctrl+C. I currently don't see way in Realine() API. I think it would be nice to have a call back for Ctrl+C, Ctrl+Z etc.

Cannot work with stdin after linenoise

Hi,

I have following problem.
After I stop linenoise stdin is closed or something like this.
See https://github.com/cppan/cppan/blob/master/src/client/init.cpp#L130-L134
When while() loop is stopped, I need to read from stdin again, but I can't. (Tried this on linux/win32.)
Program can react only on ctrl+c.

Linenoise register at_exit callback to restore standard streams behavior. But I need to restore it immediately after while loop.
Probably it could be done by adding separate function for this.

Briefly:

while (!linenoise::Readline(...)) { ... }
cin >> myvar; // does not work!!

We need:

while (!linenoise::Readline(...)) { ... }
linenoise::RestoreStdinBehavior(...);
cin >> myvar; // works!!

Compiler errors with -Wall

I'm compiling everything with all warnings as errors (it's just good practice). On GCC linenoise has one warning and doesn't compile:

.../include/linenoise.hpp:1826:12: warning: unused variable ‘plen’ [-Wunused-variable]
     size_t plen = l->prompt.length();

Typo on github page

Multi-platfrom (Unix, Windows) C++ header-only linenoise-based readline library.
Multi-platform

multiple definition errors on Windows (at link time)

I am getting errors like this when include libnoise.hpp in multiple source (cpp) files in my project (https://github.com/machinezone/IXWebSocket)

Example file -> https://github.com/machinezone/IXWebSocket/blob/ac9710d5d6e1c3dba1c681168cca39e358ab73ef/ws/ws_redis_cli.cpp

(?grm@ansi@linenoise@@3UGRM@12@A) already defined in ws_connect.obj [D:\a\IXWebSocket\IXWebSocket\build\ws\ws.vcxproj]
ws_redis_cli.obj : error LNK2005: "struct _COORD linenoise::ansi::SavePos" (?SavePos@ansi@linenoise@@3U_COORD@@A) already defined in ws_connect.obj [D:\a\IXWebSocket\IXWebSocket\build\ws\ws.vcxproj]
ws_redis_cli.obj : error LNK2005: "int linenoise::ansi::nCharInBuffer" (?nCharInBuffer@ansi@linenoise@@3HA) already defined in ws_connect.obj [D:\a\IXWebSocket\IXWebSocket\build\ws\ws.vcxproj]
ws_redis_cli.obj : error LNK2005: "wchar_t * linenoise::ansi::ChBuffer" (?ChBuffer@ansi@linenoise@@3PA_WA) already defined in ws_connect.obj [D:\a\IXWebSocket\IXWebSocket\build\ws\ws.vcxproj]

The way I solved it is by moving the body in an implementation file, but I wonder if there's a better way.

(fix => machinezone/IXWebSocket@6ed8723)

Move to C implementation ?

I think this is a great project but personally the use of C++ is a deal-breaker for me.

What is the dependency of cpp-linenoise on C++ ? Have you considered switching to C ?

Visual Studio 2015 Compile Error

Compiling with VS2015 gives me this error:
Severity Code Description Project File Error C2664 'int linenoise::win32read(int *)': cannot convert argument 1 from 'int **' to 'int *'

Changing line 1802 from
nread = win32read(&c);
to
nread = win32read(c);
solves the issue for me.

I'm not familiar with your code, so not sure if this makes sense.

Give position to autocomplete handler

For now, there is no way for an autocomplete handler to understand where the user cursor is exactly located. It would be nice to have one, as in many cases the user wants to autocomplete something in the middle of the input string.

Clean up C++11 warnings?

cpp-linenoise.hpp builds and runs on C++11 compilers, but -Wall produces many warnings - implicit signed/unsigned conversions, old style casts, deprecated functions (sscanf), goto crossing a variable initialization. The goto issue is actually considered fatal in gcc unless -fpermissive is specified.

Any thoughts about updating the source?

Many warnings

Building on a MS compiler gives many warnings related to conversions between size_t and int, and similar. On a build that forces warnings to be errors, this was a problem. I had to patch the source code to do the casts.

Multithreaded issue

Hiya,

When writing to the terminal on one thread, and reading on another with linenoise, output will be strangely indented.

The problem and patch is perfectly summed up in this thread - antirez/linenoise#128 - I wondered if you would be interested in applying the patch.

Thanks for the library by the way, it's far nicer to use than the C linenoise ❤️

Tab gets appended after auto-completion

Just saw this bug...

Repro steps:

  1. Setup linenoise with few autocomplete.
  2. Press first char, press Tab to do auto complete.
  3. Press space.

Notice that tab gets appended in console. if you try to do backspace, you get weird behavior now.

mingw + msys build failure

Hi there,

I am currently porting this library to hunter (a cmake based package manger) and encountered build errors using mingw and msys.

Build logs can be found here:

Regards
Michael

wrong cursor position with non-ascii input

Whenever the input contains non-ASCII characters that consist of multibyte characters, then the cursor position is wrong.
For example, when entering ö (an Umlaut character), the cursor will go to the right by 2 positions, but it should advance only one character. The reason for this is that the ö consists of two bytes in UTF-8, and entering this characters causes two calls to read() insidie linenoiseEdit() and the linenoise version used does not seem to be Unicode-aware at all.
So for UTF-8 multibyte characters it is broken, at least on all the Linuxes I tried.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.