diff --git a/src/main.zig b/src/main.zig index 41e5504..f41098d 100644 --- a/src/main.zig +++ b/src/main.zig @@ -228,8 +228,12 @@ pub fn main() anyerror!void { } } else { const prompt_str = std.process.getEnvVarOwned(allocator, "ZF_PROMPT") catch "> "; + const no_color = if (std.process.getEnvVarOwned(allocator, "NO_COLOR")) |value| blk: { + break :blk value.len > 0; + } + else |_| false; - var terminal = try ui.Terminal.init(@minimum(candidates.len, config.lines)); + var terminal = try ui.Terminal.init(@minimum(candidates.len, config.lines), no_color); var selected = try ui.run(allocator, &terminal, candidates, config.keep_order, prompt_str); try ui.cleanUp(&terminal); terminal.deinit(); diff --git a/src/ui.zig b/src/ui.zig index c38caed..d38ec6f 100644 --- a/src/ui.zig +++ b/src/ui.zig @@ -26,7 +26,9 @@ pub const Terminal = struct { height: usize = undefined, max_height: usize, - pub fn init(max_height: usize) !Terminal { + no_color: bool, + + pub fn init(max_height: usize, no_color: bool) !Terminal { var tty = try std.fs.openFileAbsolute("/dev/tty", .{ .read = true, .write = true }); // store original terminal settings to restore later @@ -44,6 +46,7 @@ pub const Terminal = struct { .termios = termios, .raw_termios = raw_termios, .max_height = max_height, + .no_color = no_color, }; } @@ -264,7 +267,7 @@ inline fn drawCandidate(terminal: *Terminal, candidate: Candidate, width: usize, const str = candidate.str[0..std.math.min(width, candidate.str.len)]; // no highlights, just draw the string - if (candidate.ranges == null) { + if (candidate.ranges == null or terminal.no_color) { _ = terminal.writer.write(str) catch unreachable; } else { var slicer = Slicer.init(str, candidate.ranges.?);