Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use bold/underline formatting instead of colors for ANSI terminals #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/ansi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ pub const LightBlue = "94";
pub const LightMagenta = "95";
pub const LightCyan = "96";
pub const White = "97";

pub const ResetText = "0";
pub const Bold = "1";
pub const Underline = "4";
// zig fmt: on

pub fn Reset() []const u8 {
return escape("0m");
return escape(ResetText ++ "m");
}

pub fn Foreground(comptime color: []const u8) []const u8 {
Expand Down
7 changes: 4 additions & 3 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn askString(allocator: std.mem.Allocator, prompt: []const u8, max_size: usi
const in = std.io.getStdIn().reader();
const out = OutputWriter.init(std.io.getStdOut());

try out.writeSeq(.{ Fg.Cyan, "? ", Fg.White, prompt });
try out.writeSeq(.{ Fg.Bold, "? ", Fg.Reset, prompt });

const result = try in.readUntilDelimiterAlloc(allocator, '\n', max_size);
return if (std.mem.endsWith(u8, result, "\r")) result[0..(result.len - 1)] else result;
Expand Down Expand Up @@ -38,14 +38,15 @@ pub fn askDirPath(allocator: std.mem.Allocator, prompt: []const u8, max_size: us
}
}


pub fn askBool(prompt: []const u8) !bool {
const in = std.io.getStdIn().reader();
const out = OutputWriter.init(std.io.getStdOut());

var buffer: [1]u8 = undefined;

while (true) {
try out.writeSeq(.{ Fg.Cyan, "? ", Fg.White, prompt, Fg.DarkGray, " (y/n) > " });
try out.writeSeq(.{ Fg.Bold, "? ", Fg.Reset, prompt, Fg.Bold, " (y/n) > " });

const read = in.read(&buffer) catch continue;
try in.skipUntilDelimiterOrEof('\n');
Expand All @@ -64,7 +65,7 @@ pub fn askSelectOne(prompt: []const u8, comptime options: type) !options {
const in = std.io.getStdIn().reader();
const out = OutputWriter.init(std.io.getStdOut());

try out.writeSeq(.{ Fg.Cyan, "? ", Fg.White, prompt, Fg.DarkGray, " (select one)", "\n\n" });
try out.writeSeq(.{ Fg.Bold, "? ", Fg.Reset, prompt, Fg.Bold, " (select one)", "\n\n" });

comptime var max_size: usize = 0;
inline for (@typeInfo(options).Enum.fields) |option| {
Expand Down
9 changes: 9 additions & 0 deletions src/writer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub const Fg = enum {
Magenta,
Cyan,
LightGray,
Default,
DarkGray,
LightRed,
LightGreen,
Expand All @@ -28,6 +29,10 @@ pub const Fg = enum {
LightMagenta,
LightCyan,
White,
// Those are for formatting rather than color
Reset,
Bold,
Underline
};

/// Ignores color specifications
Expand Down Expand Up @@ -95,13 +100,17 @@ const AnsiWriter = struct {
Fg.Cyan => ansi.Foreground(ansi.Cyan),
Fg.LightGray => ansi.Foreground(ansi.LightGray),
Fg.DarkGray => ansi.Foreground(ansi.DarkGray),
Fg.Default => ansi.Foreground(ansi.Default),
Fg.LightRed => ansi.Foreground(ansi.LightRed),
Fg.LightGreen => ansi.Foreground(ansi.LightGreen),
Fg.LightYellow => ansi.Foreground(ansi.LightYellow),
Fg.LightBlue => ansi.Foreground(ansi.LightBlue),
Fg.LightMagenta => ansi.Foreground(ansi.LightMagenta),
Fg.LightCyan => ansi.Foreground(ansi.LightCyan),
Fg.White => ansi.Foreground(ansi.White),
Fg.Reset => ansi.Foreground(ansi.ResetText),
Fg.Bold => ansi.Foreground(ansi.Bold),
Fg.Underline => ansi.Foreground(ansi.Underline),
};
}
};
Expand Down