From f8a57c905d7e5aec751509e9546225407abf01f5 Mon Sep 17 00:00:00 2001 From: Moabeat Date: Sun, 28 Jul 2024 10:59:42 +0200 Subject: [PATCH] Add list of error messages to InfoLine.zig --- res/config.ini | 7 ++++ src/bigclock.zig | 2 +- src/config/Config.zig | 6 ++-- src/main.zig | 62 +++++++++++++++++++-------------- src/tui/TerminalBuffer.zig | 10 ++++-- src/tui/components/InfoLine.zig | 49 +++++++++++++++++++++----- 6 files changed, 95 insertions(+), 41 deletions(-) diff --git a/res/config.ini b/res/config.ini index b0f0fb4..da149a6 100644 --- a/res/config.ini +++ b/res/config.ini @@ -49,6 +49,13 @@ bg = 0 # Foreground color id fg = 8 +# Background color errors +error_bg = 0 + +# Foreground color errors +# Default is red and bold: TB_RED | TB_BOLD +error_fg = 258 + # CMatrix animation foreground color id cmatrix_fg = 3 diff --git a/src/bigclock.zig b/src/bigclock.zig index 00f7939..9054a75 100644 --- a/src/bigclock.zig +++ b/src/bigclock.zig @@ -99,7 +99,7 @@ const E = [_]u21{ }; // zig fmt: on -pub fn clockCell(animate: bool, char: u8, fg: u8, bg: u8) [SIZE]termbox.tb_cell { +pub fn clockCell(animate: bool, char: u8, fg: u16, bg: u16) [SIZE]termbox.tb_cell { var cells: [SIZE]termbox.tb_cell = undefined; var tv: std.c.timeval = undefined; diff --git a/src/config/Config.zig b/src/config/Config.zig index ca8290a..0274ce5 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -7,7 +7,7 @@ const ViMode = enums.ViMode; animation: Animation = .none, asterisk: u8 = '*', -bg: u8 = 0, +bg: u16 = 0, bigclock: bool = false, blank_box: bool = true, border_fg: u8 = 8, @@ -16,7 +16,9 @@ clear_password: bool = false, clock: ?[:0]const u8 = null, console_dev: [:0]const u8 = "/dev/console", default_input: Input = .login, -fg: u8 = 8, +error_bg: u16 = 0, +error_fg: u16 = 258, +fg: u16 = 8, cmatrix_fg: u8 = 3, hide_borders: bool = false, hide_key_hints: bool = false, diff --git a/src/main.zig b/src/main.zig index 37e338f..b692581 100644 --- a/src/main.zig +++ b/src/main.zig @@ -62,7 +62,8 @@ pub fn main() !void { var config: Config = undefined; var lang: Lang = undefined; var save: Save = undefined; - var info_line = InfoLine{}; + var info_line = InfoLine.init(allocator); + defer info_line.deinit(); if (res.args.help != 0) { try clap.help(stderr, clap.Help, ¶ms, .{}); @@ -100,7 +101,10 @@ pub fn main() !void { const config_path = try std.fmt.allocPrint(allocator, "{s}{s}config.ini", .{ s, trailing_slash }); defer allocator.free(config_path); - config = config_ini.readFileToStructWithMap(config_path, mapped_config_fields) catch Config{}; + config = config_ini.readFileToStructWithMap(config_path, mapped_config_fields) catch _config: { + try info_line.addError("Unable to parse config file"); + break :_config Config{}; + }; const lang_path = try std.fmt.allocPrint(allocator, "{s}{s}lang/{s}.ini", .{ s, trailing_slash, config.lang }); defer allocator.free(lang_path); @@ -115,7 +119,10 @@ pub fn main() !void { save = save_ini.readFileToStruct(save_path) catch migrator.tryMigrateSaveFile(&user_buf, config.save_file); } } else { - config = config_ini.readFileToStructWithMap(build_options.data_directory ++ "/config.ini", mapped_config_fields) catch Config{}; + config = config_ini.readFileToStructWithMap(build_options.data_directory ++ "/config.ini", mapped_config_fields) catch _config: { + try info_line.addError("Unable to parse config file"); + break :_config Config{}; + }; const lang_path = try std.fmt.allocPrint(allocator, "{s}/lang/{s}.ini", .{ build_options.data_directory, config.lang }); defer allocator.free(lang_path); @@ -128,20 +135,23 @@ pub fn main() !void { } } - if (!build_options.enable_x11_support) try info_line.setText(lang.no_x11_support); + info_line.error_bg = config.error_bg; + info_line.error_fg = config.error_fg; + + if (!build_options.enable_x11_support) try info_line.addError(lang.no_x11_support); interop.setNumlock(config.numlock) catch {}; if (config.initial_info_text) |text| { - try info_line.setText(text); + info_line.setText(text); } else get_host_name: { // Initialize information line with host name var name_buf: [std.posix.HOST_NAME_MAX]u8 = undefined; const hostname = std.posix.gethostname(&name_buf) catch { - try info_line.setText(lang.err_hostname); + info_line.setText(lang.err_hostname); break :get_host_name; }; - try info_line.setText(hostname); + info_line.setText(hostname); } // Initialize termbox @@ -178,13 +188,13 @@ pub fn main() !void { defer desktop.deinit(); desktop.addEnvironment(.{ .Name = lang.shell }, "", .shell) catch { - try info_line.setText(lang.err_alloc); + try info_line.addError(lang.err_alloc); }; if (build_options.enable_x11_support) { if (config.xinitrc) |xinitrc| { desktop.addEnvironment(.{ .Name = lang.xinitrc, .Exec = xinitrc }, "", .xinitrc) catch { - try info_line.setText(lang.err_alloc); + try info_line.addError(lang.err_alloc); }; } } @@ -227,10 +237,10 @@ pub fn main() !void { switch (active_input) { .session => desktop.handle(null, insert_mode), .login => login.handle(null, insert_mode) catch { - try info_line.setText(lang.err_alloc); + try info_line.addError(lang.err_alloc); }, .password => password.handle(null, insert_mode) catch { - try info_line.setText(lang.err_alloc); + try info_line.addError(lang.err_alloc); }, } } @@ -274,7 +284,7 @@ pub fn main() !void { // Switch to selected TTY if possible open_console_dev: { const fd = std.posix.open(config.console_dev, .{ .ACCMODE = .WRONLY }, 0) catch { - try info_line.setText(lang.err_console_dev); + try info_line.addError(lang.err_console_dev); break :open_console_dev; }; defer std.posix.close(fd); @@ -310,10 +320,10 @@ pub fn main() !void { switch (config.animation) { .none => {}, .doom => doom.realloc() catch { - try info_line.setText(lang.err_alloc); + try info_line.addError(lang.err_alloc); }, .matrix => matrix.realloc() catch { - try info_line.setText(lang.err_alloc); + try info_line.addError(lang.err_alloc); }, } @@ -362,10 +372,10 @@ pub fn main() !void { switch (active_input) { .session => desktop.handle(null, insert_mode), .login => login.handle(null, insert_mode) catch { - try info_line.setText(lang.err_alloc); + try info_line.addError(lang.err_alloc); }, .password => password.handle(null, insert_mode) catch { - try info_line.setText(lang.err_alloc); + try info_line.addError(lang.err_alloc); }, } @@ -386,7 +396,7 @@ pub fn main() !void { buffer.drawLabel(lang.login, label_x, label_y + 4); buffer.drawLabel(lang.password, label_x, label_y + 6); - info_line.draw(buffer); + try info_line.draw(buffer); if (!config.hide_key_hints) { var length: u64 = 0; @@ -439,7 +449,7 @@ pub fn main() !void { draw_lock_state: { const lock_state = interop.getLockState(config.console_dev) catch { - try info_line.setText(lang.err_console_dev); + try info_line.addError(lang.err_console_dev); break :draw_lock_state; }; @@ -515,7 +525,7 @@ pub fn main() !void { } } else if (pressed_key == brightness_down_key and unistd.access(&config.brightnessctl[0], unistd.X_OK) == 0) brightness_change: { const brightness_str = std.fmt.allocPrint(allocator, "{s}%-", .{config.brightness_change}) catch { - try info_line.setText(lang.err_brightness_change); + try info_line.addError(lang.err_brightness_change); break :brightness_change; }; defer allocator.free(brightness_str); @@ -523,7 +533,7 @@ pub fn main() !void { _ = brightness.spawnAndWait() catch .{}; } else if (pressed_key == brightness_up_key and unistd.access(&config.brightnessctl[0], unistd.X_OK) == 0) brightness_change: { const brightness_str = std.fmt.allocPrint(allocator, "+{s}%", .{config.brightness_change}) catch { - try info_line.setText(lang.err_brightness_change); + try info_line.addError(lang.err_brightness_change); break :brightness_change; }; defer allocator.free(brightness_str); @@ -593,9 +603,9 @@ pub fn main() !void { const password_text = try allocator.dupeZ(u8, password.text.items); defer allocator.free(password_text); - try info_line.setText(lang.authenticating); + info_line.setText(lang.authenticating); InfoLine.clearRendered(allocator, buffer) catch {}; - info_line.draw(buffer); + try info_line.draw(buffer); _ = termbox.tb_present(); session_pid = try std.posix.fork(); @@ -616,11 +626,11 @@ pub fn main() !void { if (auth_err) |err| { auth_fails += 1; active_input = .password; - try info_line.setText(getAuthErrorMsg(err, lang)); + info_line.setText(getAuthErrorMsg(err, lang)); if (config.clear_password or err != error.PamAuthError) password.clear(); } else { password.clear(); - try info_line.setText(lang.logout); + info_line.setText(lang.logout); } try std.posix.tcsetattr(std.posix.STDIN_FILENO, .FLUSH, tb_termios); @@ -665,10 +675,10 @@ pub fn main() !void { switch (active_input) { .session => desktop.handle(&event, insert_mode), .login => login.handle(&event, insert_mode) catch { - try info_line.setText(lang.err_alloc); + try info_line.addError(lang.err_alloc); }, .password => password.handle(&event, insert_mode) catch { - try info_line.setText(lang.err_alloc); + try info_line.addError(lang.err_alloc); }, } update = true; diff --git a/src/tui/TerminalBuffer.zig b/src/tui/TerminalBuffer.zig index 9692365..ebc8ca7 100644 --- a/src/tui/TerminalBuffer.zig +++ b/src/tui/TerminalBuffer.zig @@ -14,8 +14,8 @@ random: Random, width: u64, height: u64, buffer: [*]termbox.tb_cell, -fg: u8, -bg: u8, +fg: u16, +bg: u16, border_fg: u8, box_chars: struct { left_up: u32, @@ -158,13 +158,17 @@ pub fn calculateComponentCoordinates(self: TerminalBuffer) struct { } pub fn drawLabel(self: TerminalBuffer, text: []const u8, x: u64, y: u64) void { + drawColorLabel(text, x, y, self.fg, self.bg); +} + +pub fn drawColorLabel(text: []const u8, x: u64, y: u64, fg: u16, bg: u16) void { const yc: c_int = @intCast(y); const utf8view = std.unicode.Utf8View.init(text) catch return; var utf8 = utf8view.iterator(); var i = x; while (utf8.nextCodepoint()) |codepoint| : (i += 1) { - _ = termbox.tb_set_cell(@intCast(i), yc, codepoint, self.fg, self.bg); + _ = termbox.tb_set_cell(@intCast(i), yc, codepoint, fg, bg); } } diff --git a/src/tui/components/InfoLine.zig b/src/tui/components/InfoLine.zig index 82d33ee..06bf665 100644 --- a/src/tui/components/InfoLine.zig +++ b/src/tui/components/InfoLine.zig @@ -2,23 +2,54 @@ const std = @import("std"); const utils = @import("../utils.zig"); const TerminalBuffer = @import("../TerminalBuffer.zig"); +const ArrayList = std.ArrayList; + const InfoLine = @This(); -text: []const u8 = "", -width: u8 = 0, +error_list: ArrayList([]const u8), +error_bg: u16, +error_fg: u16, +text: []const u8, -pub fn setText(self: *InfoLine, text: []const u8) !void { - self.width = if (text.len > 0) try utils.strWidth(text) else 0; +pub fn init(allocator: std.mem.Allocator) InfoLine { + return .{ + .error_list = ArrayList([]const u8).init(allocator), + .error_bg = 0, + .error_fg = 258, + .text = "", + }; +} + +pub fn deinit(self: InfoLine) void { + self.error_list.deinit(); +} + +pub fn setText(self: *InfoLine, text: []const u8) void { self.text = text; } -pub fn draw(self: InfoLine, buffer: TerminalBuffer) void { - if (self.width > 0 and buffer.box_width > self.width) { - const label_y = buffer.box_y + buffer.margin_box_v; - const x = buffer.box_x + ((buffer.box_width - self.width) / 2); +pub fn addError(self: *InfoLine, error_message: []const u8) !void { + try self.error_list.append(error_message); +} - buffer.drawLabel(self.text, x, label_y); +pub fn draw(self: InfoLine, buffer: TerminalBuffer) !void { + var text: []const u8 = self.text; + var bg: u16 = buffer.bg; + var fg: u16 = buffer.fg; + + if (self.error_list.items.len > 0) { + text = self.error_list.getLast(); + bg = self.error_bg; + fg = self.error_fg; } + + const width: u8 = if (text.len > 0) try utils.strWidth(text) else 0; + + if (width > 0 and buffer.box_width > width) { + const label_y = buffer.box_y + buffer.margin_box_v; + const x = buffer.box_x + ((buffer.box_width - width) / 2); + TerminalBuffer.drawColorLabel(text, x, label_y, fg, bg); + } } pub fn clearRendered(allocator: std.mem.Allocator, buffer: TerminalBuffer) !void {