joachimschmidt557 / linenoize

A port of linenoise to zig
MIT License
55 stars 9 forks source link

Not working with `std.io.getStdOut().writer()` #28

Closed glyh closed 1 month ago

glyh commented 4 months ago

The following won't work:

pub fn main() !void {
    const allocator = std.heap.page_allocator;

    var ln = Linenoise.init(allocator);
    defer ln.deinit();

    const stdout_file = std.io.getStdOut().writer();
    var bw = std.io.bufferedWriter(stdout_file);
    const stdout = bw.writer();

    while (try ln.linenoise("user> ")) |input| {
        defer allocator.free(input);

        try stdout.print("{s}", .{input});
        try bw.flush();

        try ln.history.add(input);
    }
}
joachimschmidt557 commented 1 month ago

This line

        try stdout.print("{s}", .{input});

needs to have an additional newline, like this

        try stdout.print("{s}\n", .{input});

This is because linenoize will clear the line the cursor is currently for displaying the prompt and input.