Files
math_shell/src/main.zig
2025-03-30 09:10:18 -04:00

51 lines
1.5 KiB
Zig

const std = @import("std");
const Linenoise = @import("linenoise").Linenoise;
fn clear() !void {
const stdout = std.io.getStdOut().writer();
try stdout.print("\u{001b}[H\u{001b}[J", .{});
}
fn getPrompt() ![]const u8 {
const script = @embedFile("./get_prompt.bash");
const result = try std.process.Child.run(.{
.allocator = std.heap.page_allocator,
.argv = &[_][]const u8{ "bash", "-i", "-c", script },
});
return result.stdout;
}
pub fn main() !void {
try clear();
const allocator = std.heap.page_allocator;
var ln = Linenoise.init(allocator);
defer ln.deinit();
const p = try getPrompt();
while (try ln.linenoise(p)) |input| {
defer allocator.free(input);
std.debug.print("input: {s}\n", .{input});
try ln.history.add(input);
}
}
test "simple test" {
var list = std.ArrayList(i32).init(std.testing.allocator);
defer list.deinit(); // Try commenting this out and see if zig detects the memory leak!
try list.append(42);
try std.testing.expectEqual(@as(i32, 42), list.pop());
}
test "fuzz example" {
const Context = struct {
fn testOne(context: @This(), input: []const u8) anyerror!void {
_ = context;
// Try passing `--fuzz` to `zig build test` and see if it manages to fail this test case!
try std.testing.expect(!std.mem.eql(u8, "canyoufindme", input));
}
};
try std.testing.fuzz(Context{}, Context.testOne, .{});
}