1
0
Fork 0

Fix comptime colouring.

This commit is contained in:
namedkitten 2020-07-18 05:11:12 +01:00
parent fa00747fca
commit 8dec84876e
5 changed files with 39 additions and 73 deletions

View file

@ -28,85 +28,54 @@ const TerminalYellowColour = "\u{001b}[33m";
const TerminalGreenColour = "\u{001b}[32m"; const TerminalGreenColour = "\u{001b}[32m";
const TerminalPurpleColour = "\u{001b}[35m"; const TerminalPurpleColour = "\u{001b}[35m";
fn getColourFromColour(clr: []const u8) []const u8 {
if (clr[0] == '#' or clr[0] == '\u{001b}') {
return clr;
} else if (eql(u8, clr, "text")) {
return if (!terminal_version) TextColour else TerminalTextColour;
} else if (eql(u8, clr, "dark")) {
return if (!terminal_version) DarkerTextColour else TerminalDarkerTextColour;
} else if (eql(u8, clr, "darkest")) {
return if (!terminal_version) DarkestTextColour else TerminalDarkestTextColour;
} else if (eql(u8, clr, "accentlight")) {
return if (!terminal_version) AccentLightColour else TerminalAccentLightColour;
} else if (eql(u8, clr, "accentmedium")) {
return if (!terminal_version) AccentMediumColour else TerminalAccentMediumColour;
} else if (eql(u8, clr, "accentdark")) {
return if (!terminal_version) AccentDarkColour else TerminalAccentDarkColour;
} else if (eql(u8, clr, "red")) {
return if (!terminal_version) RedColour else TerminalRedColour;
} else if (eql(u8, clr, "orange")) {
return if (!terminal_version) OrangeColour else TerminalOrangeColour;
} else if (eql(u8, clr, "yellow")) {
return if (!terminal_version) YellowColour else TerminalYellowColour;
} else if (eql(u8, clr, "green")) {
return if (!terminal_version) GreenColour else TerminalGreenColour;
} else if (eql(u8, clr, "purple")) {
return if (!terminal_version) PurpleColour else TerminalPurpleColour;
} else {
unreachable;
}
}
/// This colours a string but at comptime. /// This colours a string but at comptime.
pub fn comptimeColour(comptime clr: []const u8, comptime str: []const u8) []const u8 { pub fn comptimeColour(comptime clr: []const u8, comptime str: []const u8) []const u8 {
if (disable_colour) return str; if (disable_colour) return str;
if (clr[0] == '#' or clr[0] == '\u{001b}') { const proper_colour = comptime getColourFromColour(clr);
if (terminal_version) { if (terminal_version) {
return crl ++ str ++ TerminalResetColour; return proper_colour ++ str ++ TerminalResetColour;
} else {
return "<span color=\"" ++ clr ++ "\">" ++ str ++ "</span>";
}
} else { } else {
comptime var colourText: []const u8 = ""; return "<span color=\"" ++ proper_colour ++ "\">" ++ str ++ "</span>";
if (eql(u8, clr, "text")) {
colourText = if (!terminal_version) TextColour else TerminalTextColour;
} else if (eql(u8, clr, "dark")) {
colourText = if (!terminal_version) DarkerTextColour else TerminalDarkerTextColour;
} else if (eql(u8, clr, "darkest")) {
colourText = if (!terminal_version) DarkestTextColour else TerminalDarkestTextColour;
} else if (eql(u8, clr, "accentlight")) {
colourText = if (!terminal_version) AccentLightColour else TerminalAccentLightColour;
} else if (eql(u8, clr, "accentmedium")) {
colourText = if (!terminal_version) AccentMediumColour else TerminalAccentMediumColour;
} else if (eql(u8, clr, "accentdark")) {
colourText = if (!terminal_version) AccentDarkColour else TerminalAccentDarkColour;
} else if (eql(u8, clr, "red")) {
colourText = if (!terminal_version) RedColour else TerminalRedColour;
} else if (eql(u8, clr, "orange")) {
colourText = if (!terminal_version) OrangeColour else TerminalOrangeColour;
} else if (eql(u8, clr, "yellow")) {
colourText = if (!terminal_version) YellowColour else TerminalYellowColour;
} else if (eql(u8, clr, "green")) {
colourText = if (!terminal_version) GreenColour else TerminalGreenColour;
} else if (eql(u8, clr, "purple")) {
colourText = if (!terminal_version) PurpleColour else TerminalPurpleColour;
}
if (colourText.len == 0) {
unreachable;
}
return comptimeColour(colourText, str);
} }
} }
/// This colours a dynamic string at runtime. /// This colours a dynamic string at runtime.
/// It may make more than one allocation,
/// so put it on a ArenaAllocator so you can free what else it allocates.
pub fn colour(alloc: *std.mem.Allocator, clr: []const u8, str: []const u8) ![]const u8 { pub fn colour(alloc: *std.mem.Allocator, clr: []const u8, str: []const u8) ![]const u8 {
if (disable_colour) return str; if (disable_colour) return str;
const proper_colour = getColourFromColour(clr);
if (clr[0] == '#' or clr[0] == '\u{001b}') { if (terminal_version) {
if (terminal_version) { return try std.fmt.allocPrint(alloc, "{}{}" ++ TerminalResetColour, .{ proper_colour, str });
return try std.fmt.allocPrint(alloc, "{}{}" ++ TerminalResetColour, .{ clr, str });
} else {
return try std.fmt.allocPrint(alloc, "<span color=\"{}\">{}</span>", .{ clr, str });
}
} else { } else {
var colourText: []const u8 = ""; return try std.fmt.allocPrint(alloc, "<span color=\"{}\">{}</span>", .{ proper_colour, str });
if (eql(u8, clr, "text")) {
colourText = if (!terminal_version) TextColour else TerminalTextColour;
} else if (eql(u8, clr, "dark")) {
colourText = if (!terminal_version) DarkerTextColour else TerminalDarkerTextColour;
} else if (eql(u8, clr, "darkest")) {
colourText = if (!terminal_version) DarkestTextColour else TerminalDarkestTextColour;
} else if (eql(u8, clr, "accentlight")) {
colourText = if (!terminal_version) AccentLightColour else TerminalAccentLightColour;
} else if (eql(u8, clr, "accentmedium")) {
colourText = if (!terminal_version) AccentMediumColour else TerminalAccentMediumColour;
} else if (eql(u8, clr, "accentdark")) {
colourText = if (!terminal_version) AccentDarkColour else TerminalAccentDarkColour;
} else if (eql(u8, clr, "red")) {
colourText = if (!terminal_version) RedColour else TerminalRedColour;
} else if (eql(u8, clr, "orange")) {
colourText = if (!terminal_version) OrangeColour else TerminalOrangeColour;
} else if (eql(u8, clr, "yellow")) {
colourText = if (!terminal_version) YellowColour else TerminalYellowColour;
} else if (eql(u8, clr, "green")) {
colourText = if (!terminal_version) GreenColour else TerminalGreenColour;
} else if (eql(u8, clr, "purple")) {
colourText = if (!terminal_version) PurpleColour else TerminalPurpleColour;
}
return colour(alloc, colourText, str);
} }
} }

View file

@ -25,7 +25,6 @@ pub const TimeWidget = struct {
pub fn start(self: *TimeWidget) anyerror!void { pub fn start(self: *TimeWidget) anyerror!void {
// TODO: find a god damn decent time library thats better than this bullshit. // TODO: find a god damn decent time library thats better than this bullshit.
while (self.bar.keep_running()) { while (self.bar.keep_running()) {
var arena = std.heap.ArenaAllocator.init(self.allocator); var arena = std.heap.ArenaAllocator.init(self.allocator);
defer arena.deinit(); defer arena.deinit();

View file

@ -62,7 +62,6 @@ pub const WeatherWidget = struct {
var main: []const u8 = ""; var main: []const u8 = "";
var message: []const u8 = ""; var message: []const u8 = "";
// This parser is clunky, it may be worth a rewrite but it seems like it optimizes decently. // This parser is clunky, it may be worth a rewrite but it seems like it optimizes decently.
while (try client.readEvent()) |event| { while (try client.readEvent()) |event| {
switch (event) { switch (event) {
@ -173,7 +172,6 @@ pub const WeatherWidget = struct {
var temp = inf.temp; var temp = inf.temp;
var main = inf.main; var main = inf.main;
// Please note that these are *my* personal temp preferences. // Please note that these are *my* personal temp preferences.
// TODO: it may be worth making a way for the user to change this with a function on init. // TODO: it may be worth making a way for the user to change this with a function on init.
// If you happen to read this and you plan on inviting me round your house one day, // If you happen to read this and you plan on inviting me round your house one day,