dotfiles/scripts/mzteinit/src/util.zig

49 lines
1.2 KiB
Zig
Raw Normal View History

const std = @import("std");
2023-07-18 13:04:33 +02:00
const at = @import("ansi-term");
2023-07-01 19:20:02 +02:00
pub const ansi_clear = "\x1b[2J\x1b[1;1H";
2023-06-02 16:29:17 +02:00
pub const ExitMode = enum { run, immediate, delayed };
2023-07-18 13:04:33 +02:00
pub inline fn updateStyle(writer: anytype, current: *?at.style.Style, new: at.style.Style) !void {
try at.format.updateStyle(writer, new, current.*);
current.* = new;
}
fn formatCommand(
cmd: []const []const u8,
comptime fmt: []const u8,
options: std.fmt.FormatOptions,
writer: anytype,
) !void {
_ = options;
_ = fmt;
var first = true;
for (cmd) |arg| {
defer first = false;
var needs_quote = false;
for (arg) |ch| {
if (!std.ascii.isPrint(ch) or ch == '"' or ch == ' ' or ch == '*' or ch == '$') {
needs_quote = true;
break;
}
}
if (!first)
try writer.writeByte(' ');
if (needs_quote) {
try writer.writeByte('\'');
try writer.print("{}", .{std.fmt.fmtSliceEscapeUpper(arg)});
try writer.writeByte('\'');
} else {
try writer.writeAll(arg);
}
}
}
pub fn fmtCommand(cmd: []const []const u8) std.fmt.Formatter(formatCommand) {
return .{ .data = cmd };
}