repl: use readline

This commit is contained in:
Luna 2019-10-06 10:53:09 -03:00
parent 8c5f42b3db
commit cb689c3ade
2 changed files with 15 additions and 6 deletions

View file

@ -6,6 +6,7 @@ fn setupLinks(step: *builds.LibExeObjStep) void {
step.linkSystemLibrary("lilv-0"); step.linkSystemLibrary("lilv-0");
step.linkSystemLibrary("sndfile"); step.linkSystemLibrary("sndfile");
step.linkSystemLibrary("readline");
step.linkSystemLibrary("GraphicsMagickWand"); step.linkSystemLibrary("GraphicsMagickWand");
step.linkSystemLibrary("GraphicsMagick"); step.linkSystemLibrary("GraphicsMagick");

View file

@ -8,6 +8,14 @@ test "scritcher" {
_ = @import("runner.zig"); _ = @import("runner.zig");
} }
const readline = @cImport({
@cInclude("stdio.h");
@cInclude("stdlib.h");
@cInclude("readline/readline.h");
@cInclude("readline/history.h");
});
fn wrapInCmdList(allocator: *std.mem.Allocator, cmd: langs.Command) !langs.CommandList { fn wrapInCmdList(allocator: *std.mem.Allocator, cmd: langs.Command) !langs.CommandList {
var cmds = langs.CommandList.init(allocator); var cmds = langs.CommandList.init(allocator);
try cmds.append(cmd); try cmds.append(cmd);
@ -69,13 +77,13 @@ pub fn doRepl(allocator: *std.mem.Allocator, args_it: var) !void {
while (true) { while (true) {
lang.reset(); lang.reset();
try stdout.print("> ");
var buffer = try std.Buffer.init(allocator, ""[0..]); var rd_line = readline.readline(c"> ");
var line = std.io.readLine(&buffer) catch |err| { if (rd_line == null) break;
if (err == error.EndOfStream) break; readline.add_history(rd_line);
return err; defer std.heap.c_allocator.destroy(rd_line);
};
var line = rd_line[0..std.mem.len(u8, rd_line)];
if (std.mem.eql(u8, line, "push")) { if (std.mem.eql(u8, line, "push")) {
try cmds.append(current); try cmds.append(current);