Add list of error messages to InfoLine.zig

This commit is contained in:
Moabeat 2024-07-28 10:59:42 +02:00
parent 93554d9ba3
commit f8a57c905d
No known key found for this signature in database
GPG Key ID: 315E0CCBC16F3D53
6 changed files with 95 additions and 41 deletions

View File

@ -49,6 +49,13 @@ bg = 0
# Foreground color id # Foreground color id
fg = 8 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 animation foreground color id
cmatrix_fg = 3 cmatrix_fg = 3

View File

@ -99,7 +99,7 @@ const E = [_]u21{
}; };
// zig fmt: on // 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 cells: [SIZE]termbox.tb_cell = undefined;
var tv: std.c.timeval = undefined; var tv: std.c.timeval = undefined;

View File

@ -7,7 +7,7 @@ const ViMode = enums.ViMode;
animation: Animation = .none, animation: Animation = .none,
asterisk: u8 = '*', asterisk: u8 = '*',
bg: u8 = 0, bg: u16 = 0,
bigclock: bool = false, bigclock: bool = false,
blank_box: bool = true, blank_box: bool = true,
border_fg: u8 = 8, border_fg: u8 = 8,
@ -16,7 +16,9 @@ clear_password: bool = false,
clock: ?[:0]const u8 = null, clock: ?[:0]const u8 = null,
console_dev: [:0]const u8 = "/dev/console", console_dev: [:0]const u8 = "/dev/console",
default_input: Input = .login, default_input: Input = .login,
fg: u8 = 8, error_bg: u16 = 0,
error_fg: u16 = 258,
fg: u16 = 8,
cmatrix_fg: u8 = 3, cmatrix_fg: u8 = 3,
hide_borders: bool = false, hide_borders: bool = false,
hide_key_hints: bool = false, hide_key_hints: bool = false,

View File

@ -62,7 +62,8 @@ pub fn main() !void {
var config: Config = undefined; var config: Config = undefined;
var lang: Lang = undefined; var lang: Lang = undefined;
var save: Save = undefined; var save: Save = undefined;
var info_line = InfoLine{}; var info_line = InfoLine.init(allocator);
defer info_line.deinit();
if (res.args.help != 0) { if (res.args.help != 0) {
try clap.help(stderr, clap.Help, &params, .{}); try clap.help(stderr, clap.Help, &params, .{});
@ -100,7 +101,10 @@ pub fn main() !void {
const config_path = try std.fmt.allocPrint(allocator, "{s}{s}config.ini", .{ s, trailing_slash }); const config_path = try std.fmt.allocPrint(allocator, "{s}{s}config.ini", .{ s, trailing_slash });
defer allocator.free(config_path); 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 }); const lang_path = try std.fmt.allocPrint(allocator, "{s}{s}lang/{s}.ini", .{ s, trailing_slash, config.lang });
defer allocator.free(lang_path); 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); save = save_ini.readFileToStruct(save_path) catch migrator.tryMigrateSaveFile(&user_buf, config.save_file);
} }
} else { } 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 }); const lang_path = try std.fmt.allocPrint(allocator, "{s}/lang/{s}.ini", .{ build_options.data_directory, config.lang });
defer allocator.free(lang_path); 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 {}; interop.setNumlock(config.numlock) catch {};
if (config.initial_info_text) |text| { if (config.initial_info_text) |text| {
try info_line.setText(text); info_line.setText(text);
} else get_host_name: { } else get_host_name: {
// Initialize information line with host name // Initialize information line with host name
var name_buf: [std.posix.HOST_NAME_MAX]u8 = undefined; var name_buf: [std.posix.HOST_NAME_MAX]u8 = undefined;
const hostname = std.posix.gethostname(&name_buf) catch { 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; break :get_host_name;
}; };
try info_line.setText(hostname); info_line.setText(hostname);
} }
// Initialize termbox // Initialize termbox
@ -178,13 +188,13 @@ pub fn main() !void {
defer desktop.deinit(); defer desktop.deinit();
desktop.addEnvironment(.{ .Name = lang.shell }, "", .shell) catch { 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 (build_options.enable_x11_support) {
if (config.xinitrc) |xinitrc| { if (config.xinitrc) |xinitrc| {
desktop.addEnvironment(.{ .Name = lang.xinitrc, .Exec = xinitrc }, "", .xinitrc) catch { 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) { switch (active_input) {
.session => desktop.handle(null, insert_mode), .session => desktop.handle(null, insert_mode),
.login => login.handle(null, insert_mode) catch { .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 { .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 // Switch to selected TTY if possible
open_console_dev: { open_console_dev: {
const fd = std.posix.open(config.console_dev, .{ .ACCMODE = .WRONLY }, 0) catch { 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; break :open_console_dev;
}; };
defer std.posix.close(fd); defer std.posix.close(fd);
@ -310,10 +320,10 @@ pub fn main() !void {
switch (config.animation) { switch (config.animation) {
.none => {}, .none => {},
.doom => doom.realloc() catch { .doom => doom.realloc() catch {
try info_line.setText(lang.err_alloc); try info_line.addError(lang.err_alloc);
}, },
.matrix => matrix.realloc() catch { .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) { switch (active_input) {
.session => desktop.handle(null, insert_mode), .session => desktop.handle(null, insert_mode),
.login => login.handle(null, insert_mode) catch { .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 { .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.login, label_x, label_y + 4);
buffer.drawLabel(lang.password, label_x, label_y + 6); buffer.drawLabel(lang.password, label_x, label_y + 6);
info_line.draw(buffer); try info_line.draw(buffer);
if (!config.hide_key_hints) { if (!config.hide_key_hints) {
var length: u64 = 0; var length: u64 = 0;
@ -439,7 +449,7 @@ pub fn main() !void {
draw_lock_state: { draw_lock_state: {
const lock_state = interop.getLockState(config.console_dev) catch { 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; 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: { } 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 { 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; break :brightness_change;
}; };
defer allocator.free(brightness_str); defer allocator.free(brightness_str);
@ -523,7 +533,7 @@ pub fn main() !void {
_ = brightness.spawnAndWait() catch .{}; _ = brightness.spawnAndWait() catch .{};
} else if (pressed_key == brightness_up_key and unistd.access(&config.brightnessctl[0], unistd.X_OK) == 0) brightness_change: { } 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 { 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; break :brightness_change;
}; };
defer allocator.free(brightness_str); defer allocator.free(brightness_str);
@ -593,9 +603,9 @@ pub fn main() !void {
const password_text = try allocator.dupeZ(u8, password.text.items); const password_text = try allocator.dupeZ(u8, password.text.items);
defer allocator.free(password_text); defer allocator.free(password_text);
try info_line.setText(lang.authenticating); info_line.setText(lang.authenticating);
InfoLine.clearRendered(allocator, buffer) catch {}; InfoLine.clearRendered(allocator, buffer) catch {};
info_line.draw(buffer); try info_line.draw(buffer);
_ = termbox.tb_present(); _ = termbox.tb_present();
session_pid = try std.posix.fork(); session_pid = try std.posix.fork();
@ -616,11 +626,11 @@ pub fn main() !void {
if (auth_err) |err| { if (auth_err) |err| {
auth_fails += 1; auth_fails += 1;
active_input = .password; 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(); if (config.clear_password or err != error.PamAuthError) password.clear();
} else { } else {
password.clear(); password.clear();
try info_line.setText(lang.logout); info_line.setText(lang.logout);
} }
try std.posix.tcsetattr(std.posix.STDIN_FILENO, .FLUSH, tb_termios); try std.posix.tcsetattr(std.posix.STDIN_FILENO, .FLUSH, tb_termios);
@ -665,10 +675,10 @@ pub fn main() !void {
switch (active_input) { switch (active_input) {
.session => desktop.handle(&event, insert_mode), .session => desktop.handle(&event, insert_mode),
.login => login.handle(&event, insert_mode) catch { .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 { .password => password.handle(&event, insert_mode) catch {
try info_line.setText(lang.err_alloc); try info_line.addError(lang.err_alloc);
}, },
} }
update = true; update = true;

View File

@ -14,8 +14,8 @@ random: Random,
width: u64, width: u64,
height: u64, height: u64,
buffer: [*]termbox.tb_cell, buffer: [*]termbox.tb_cell,
fg: u8, fg: u16,
bg: u8, bg: u16,
border_fg: u8, border_fg: u8,
box_chars: struct { box_chars: struct {
left_up: u32, 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 { 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 yc: c_int = @intCast(y);
const utf8view = std.unicode.Utf8View.init(text) catch return; const utf8view = std.unicode.Utf8View.init(text) catch return;
var utf8 = utf8view.iterator(); var utf8 = utf8view.iterator();
var i = x; var i = x;
while (utf8.nextCodepoint()) |codepoint| : (i += 1) { 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);
} }
} }

View File

@ -2,23 +2,54 @@ const std = @import("std");
const utils = @import("../utils.zig"); const utils = @import("../utils.zig");
const TerminalBuffer = @import("../TerminalBuffer.zig"); const TerminalBuffer = @import("../TerminalBuffer.zig");
const ArrayList = std.ArrayList;
const InfoLine = @This(); const InfoLine = @This();
text: []const u8 = "", error_list: ArrayList([]const u8),
width: u8 = 0, error_bg: u16,
error_fg: u16,
text: []const u8,
pub fn setText(self: *InfoLine, text: []const u8) !void { pub fn init(allocator: std.mem.Allocator) InfoLine {
self.width = if (text.len > 0) try utils.strWidth(text) else 0; 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; self.text = text;
} }
pub fn draw(self: InfoLine, buffer: TerminalBuffer) void { pub fn addError(self: *InfoLine, error_message: []const u8) !void {
if (self.width > 0 and buffer.box_width > self.width) { try self.error_list.append(error_message);
const label_y = buffer.box_y + buffer.margin_box_v; }
const x = buffer.box_x + ((buffer.box_width - self.width) / 2);
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 { pub fn clearRendered(allocator: std.mem.Allocator, buffer: TerminalBuffer) !void {