76 lines
1.9 KiB
Zig
76 lines
1.9 KiB
Zig
const std = @import("std");
|
|
const mem = std.mem;
|
|
const stdout = std.io.getStdOut().writer();
|
|
const process = std.process;
|
|
const fmt = std.fmt;
|
|
const fs = std.fs;
|
|
const io = std.io;
|
|
|
|
pub const warn = std.debug.print;
|
|
pub fn print(comptime format: []const u8, args: anytype) void {
|
|
nosuspend stdout.print(format, args) catch return;
|
|
}
|
|
|
|
// const depth = 3;
|
|
|
|
pub const FixedBufferWriter = struct {
|
|
const Self = @This();
|
|
|
|
depth: u32 = 3,
|
|
fbs: io.FixedBufferStream([]u8),
|
|
fmtopts: fmt.FormatOptions = fmt.FormatOptions{},
|
|
|
|
pub fn put(self: *Self, str: []const u8) !void {
|
|
var writer = self.fbs.writer();
|
|
try fmt.formatType(str, "s", self.fmtopts, writer, self.depth);
|
|
}
|
|
|
|
pub fn putf(self: *Self, comptime format: []const u8, args: anytype) !void {
|
|
var writer = self.fbs.writer();
|
|
try fmt.format(writer, format, args);
|
|
}
|
|
|
|
pub fn init(buffer: []u8) Self {
|
|
return Self{
|
|
.fbs = io.fixedBufferStream(buffer),
|
|
};
|
|
}
|
|
|
|
pub fn getWritten(self: *Self) []u8 {
|
|
defer self.fbs.reset();
|
|
return self.fbs.getWritten();
|
|
}
|
|
};
|
|
|
|
|
|
pub const CLI = struct {
|
|
const Self = @This();
|
|
|
|
arena: std.heap.ArenaAllocator,
|
|
allocator: std.mem.Allocator,
|
|
args: []const [:0]u8,
|
|
|
|
pub fn init() !Self {
|
|
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
|
const allocator = arena.allocator();
|
|
var args = try process.argsAlloc(allocator);
|
|
|
|
return Self {
|
|
.arena = arena,
|
|
.allocator = allocator,
|
|
.args = args,
|
|
};
|
|
}
|
|
|
|
pub fn deinit(self: *Self) void {
|
|
self.arena.deinit();
|
|
// process.argsFree(self.allocator, self.args);
|
|
}
|
|
|
|
pub fn nextArg(self: Self, idx: *usize) ?[]const u8 {
|
|
if (idx.* >= self.args.len) return null;
|
|
defer idx.* += 1;
|
|
return self.args[idx.*];
|
|
}
|
|
};
|