High-level wrapper for readline package
Nordlöw via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Sep 7 07:00:36 PDT 2017
On Thursday, 7 September 2017 at 13:44:52 UTC, Adam D. Ruppe
wrote:
> On Thursday, 7 September 2017 at 13:37:16 UTC, Nordlöw wrote:
>> Have anybody cooked together high-level bindings on top of the
>> C-bindings for libreadline here
>
> Have you ever used readline?
>
> The basic C api is already very high level:
>
>
> char* from_user = readline("Prompt> ");
>
>
> I don't think there's a lot to gain by wrapping that...
There's always room for usability improvements when wrapping C
APIs...
Here's what I hacked together:
version = readline;
extern(C) void add_history(const char*); // missing from
gnu.readline
/**
High-level wrapper around GNU libreadline.
*/
const(char)[] readLine(in string prompt,
bool useHistory = true)
{
version(readline)
{
import gnu.readline : readline;
import std.string : toStringz, fromStringz;
const lineStringz = readline(prompt.toStringz);
if (useHistory)
{
add_history(lineStringz);
}
return lineStringz.fromStringz;
}
else
{
write(prompt); stdout.flush;
return readln.strip;
}
}
More information about the Digitalmars-d-learn
mailing list