Added line numbers to prompt, simple GPA setup in main
This commit is contained in:
parent
a2984da093
commit
9b2cdf1f0a
13
src/main.zig
13
src/main.zig
|
@ -3,16 +3,23 @@ const prompt = @import("prompt.zig");
|
|||
|
||||
pub fn main() anyerror!void {
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||
|
||||
defer {
|
||||
var notclear = gpa.deinit();
|
||||
if (notclear) {
|
||||
std.log.err("Detected memory leaks!!!", .{});
|
||||
}
|
||||
}
|
||||
|
||||
try stdout.writeAll("topiku - toki pi kulupu ijo - ver. 0.0.1\n");
|
||||
|
||||
prompt.init();
|
||||
defer prompt.deinit();
|
||||
|
||||
while (true) {
|
||||
|
||||
var input = prompt.readline("topiku> ") orelse break;
|
||||
defer prompt.free(input);
|
||||
|
||||
try stdout.print(">>> {s}\n", .{input});
|
||||
try stdout.print("<{}>: {s}\n", .{ input.num, input.content });
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,25 +8,34 @@ const io = std.io;
|
|||
const __sighandler = ?fn (c_int) callconv(.C) void;
|
||||
extern fn signal(sig: c_int, handler: __sighandler) __sighandler;
|
||||
|
||||
pub fn readline(prompt: []const u8) ?[]u8 {
|
||||
var line_num: usize = 0;
|
||||
|
||||
pub const Line = struct {
|
||||
content: []u8,
|
||||
num: usize,
|
||||
};
|
||||
|
||||
pub fn readline(prompt: []const u8) ?Line {
|
||||
var line = ceditline.readline(@as([*c]const u8, prompt.ptr));
|
||||
if (line == 0) {
|
||||
std.c.free(line);
|
||||
return null;
|
||||
}
|
||||
return std.mem.sliceTo(line, 0);
|
||||
line_num += 1;
|
||||
return Line{ .content = std.mem.sliceTo(line, 0), .num = line_num };
|
||||
}
|
||||
|
||||
pub fn free(slice: []u8) void {
|
||||
std.c.free(slice.ptr);
|
||||
pub fn free(line: Line) void {
|
||||
std.c.free(line.content.ptr);
|
||||
}
|
||||
|
||||
pub fn init() void {
|
||||
ceditline.rl_initialize();
|
||||
_ = signal(std.os.SIG.INT, exit);
|
||||
line_num = 0;
|
||||
}
|
||||
|
||||
fn exit(_ : c_int) callconv(.C) void {
|
||||
fn exit(_: c_int) callconv(.C) void {
|
||||
deinit();
|
||||
std.os.exit(0);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue