Skip to content

Commit

Permalink
Fix CLI for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
bgk- committed Jul 24, 2023
1 parent c3267fe commit affd3ea
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn build(b: *std.Build) void {
const optimize = b.standardOptimizeOption(.{});

const lib = b.addStaticLibrary(.{
.name = "topiary",
.name = "topi",
.root_source_file = .{ .path = "src/vm.zig" },
.target = target,
.optimize = optimize,
Expand Down
30 changes: 20 additions & 10 deletions src/cli.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ pub fn main() !void {
var vm_alloc = arena.allocator();

var vm = try Vm.init(vm_alloc, CliRunner);
vm.debug = true;
vm.interpretSource(contents) catch {
try vm.err.write(contents, std.io.getStdErr().writer());
};
Expand All @@ -46,21 +45,32 @@ const CliRunner = struct {
}
const stdin = std.io.getStdIn().reader();
std.debug.print("{s}", .{dialogue.content});
var buf: [1]u8 = undefined;
if (stdin.readUntilDelimiterOrEof(buf[0..], '\n') catch &buf) |_| {
var buf: [2]u8 = undefined;
if (stdin.readUntilDelimiterOrEof(&buf, '\n') catch &buf) |_| {
vm.selectContinue();
}
}

pub fn on_choices(vm: *Vm, choices: []Choice) void {
for (choices, 0..) |choice, i| {
std.debug.print("[{d}] {s}\n", .{ i, choice.content });
}
var buf: [10]u8 = undefined;
const stdin = std.io.getStdIn().reader();
if (stdin.readUntilDelimiterOrEof(buf[0..], '\n') catch &buf) |user_input| {
var index = std.fmt.parseInt(usize, user_input, 10) catch 0;
vm.selectChoice(index) catch |err| std.debug.print("Error: {}", .{err});
var index: ?usize = null;
while (index == null) {
for (choices, 0..) |choice, i| {
std.debug.print("[{d}] {s}\n", .{ i, choice.content });
}
var buf: [10]u8 = undefined;
if (stdin.readUntilDelimiterOrEof(&buf, '\n') catch &buf) |user_input| {
var input = std.mem.trim(u8, user_input, "\r\n");
index = std.fmt.parseInt(usize, input, 10) catch |err| blk: {
std.debug.print("Invliad value: {}.\n", .{err});
break :blk null;
};
if (index != null and index.? >= choices.len) {
index = null;
std.debug.print("Invalid value.\n", .{});
}
}
}
vm.selectChoice(index.?) catch |err| std.debug.print("Error: {}", .{err});
}
};

0 comments on commit affd3ea

Please sign in to comment.