Better handle info line messages internally

Signed-off-by: AnErrupTion <anerruption@disroot.org>
This commit is contained in:
AnErrupTion 2024-07-28 23:05:12 +02:00
parent 04a0ad3b33
commit 56c210372d
No known key found for this signature in database
GPG Key ID: 3E85EB44F610AD7F
2 changed files with 50 additions and 70 deletions

View File

@ -102,8 +102,8 @@ pub fn main() !void {
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: {
// literal error message, due to language file not yet available // We're using a literal error message here since the language file hasn't yet been loaded
try info_line.addError("unable to parse config file"); try info_line.addMessage("unable to parse config file", @intCast(interop.termbox.TB_DEFAULT), @intCast(interop.termbox.TB_RED | interop.termbox.TB_BOLD));
break :_config Config{}; break :_config Config{};
}; };
@ -122,7 +122,7 @@ pub fn main() !void {
} 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: {
// literal error message, due to language file not yet available // literal error message, due to language file not yet available
try info_line.addError("unable to parse config file"); try info_line.addMessage("unable to parse config file", @intCast(interop.termbox.TB_DEFAULT), @intCast(interop.termbox.TB_RED | interop.termbox.TB_BOLD));
break :_config Config{}; break :_config Config{};
}; };
@ -137,23 +137,20 @@ pub fn main() !void {
} }
} }
info_line.error_bg = config.error_bg; if (!build_options.enable_x11_support) try info_line.addMessage(lang.no_x11_support, config.bg, config.fg);
info_line.error_fg = config.error_fg;
if (!build_options.enable_x11_support) try info_line.setText(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); try info_line.addMessage(text, config.bg, config.fg);
} 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); try info_line.addMessage(lang.err_hostname, config.error_bg, config.error_fg);
break :get_host_name; break :get_host_name;
}; };
try info_line.setText(hostname); try info_line.addMessage(hostname, config.bg, config.fg);
} }
// Initialize termbox // Initialize termbox
@ -190,13 +187,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.addError(lang.err_alloc); try info_line.addMessage(lang.err_alloc, config.error_bg, config.error_fg);
}; };
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.addError(lang.err_alloc); try info_line.addMessage(lang.err_alloc, config.error_bg, config.error_fg);
}; };
} }
} }
@ -239,10 +236,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.addError(lang.err_alloc); try info_line.addMessage(lang.err_alloc, config.error_bg, config.error_fg);
}, },
.password => password.handle(null, insert_mode) catch { .password => password.handle(null, insert_mode) catch {
try info_line.addError(lang.err_alloc); try info_line.addMessage(lang.err_alloc, config.error_bg, config.error_fg);
}, },
} }
} }
@ -286,7 +283,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.addError(lang.err_console_dev); try info_line.addMessage(lang.err_console_dev, config.error_bg, config.error_fg);
break :open_console_dev; break :open_console_dev;
}; };
defer std.posix.close(fd); defer std.posix.close(fd);
@ -322,10 +319,10 @@ pub fn main() !void {
switch (config.animation) { switch (config.animation) {
.none => {}, .none => {},
.doom => doom.realloc() catch { .doom => doom.realloc() catch {
try info_line.addError(lang.err_alloc); try info_line.addMessage(lang.err_alloc, config.error_bg, config.error_fg);
}, },
.matrix => matrix.realloc() catch { .matrix => matrix.realloc() catch {
try info_line.addError(lang.err_alloc); try info_line.addMessage(lang.err_alloc, config.error_bg, config.error_fg);
}, },
} }
@ -374,10 +371,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.addError(lang.err_alloc); try info_line.addMessage(lang.err_alloc, config.error_bg, config.error_fg);
}, },
.password => password.handle(null, insert_mode) catch { .password => password.handle(null, insert_mode) catch {
try info_line.addError(lang.err_alloc); try info_line.addMessage(lang.err_alloc, config.error_bg, config.error_fg);
}, },
} }
@ -451,7 +448,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.addError(lang.err_console_dev); try info_line.addMessage(lang.err_console_dev, config.error_bg, config.error_fg);
break :draw_lock_state; break :draw_lock_state;
}; };
@ -527,7 +524,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.addError(lang.err_brightness_change); try info_line.addMessage(lang.err_brightness_change, config.error_bg, config.error_fg);
break :brightness_change; break :brightness_change;
}; };
defer allocator.free(brightness_str); defer allocator.free(brightness_str);
@ -535,7 +532,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.addError(lang.err_brightness_change); try info_line.addMessage(lang.err_brightness_change, config.error_bg, config.error_fg);
break :brightness_change; break :brightness_change;
}; };
defer allocator.free(brightness_str); defer allocator.free(brightness_str);
@ -605,7 +602,7 @@ 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); try info_line.addMessage(lang.authenticating, config.bg, config.fg);
InfoLine.clearRendered(allocator, buffer) catch {}; InfoLine.clearRendered(allocator, buffer) catch {};
try info_line.draw(buffer); try info_line.draw(buffer);
_ = termbox.tb_present(); _ = termbox.tb_present();
@ -628,11 +625,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.addError(getAuthErrorMsg(err, lang)); try info_line.addMessage(getAuthErrorMsg(err, lang), config.error_bg, config.error_fg);
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); try info_line.addMessage(lang.logout, config.bg, config.fg);
} }
try std.posix.tcsetattr(std.posix.STDIN_FILENO, .FLUSH, tb_termios); try std.posix.tcsetattr(std.posix.STDIN_FILENO, .FLUSH, tb_termios);
@ -677,10 +674,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.addError(lang.err_alloc); try info_line.addMessage(lang.err_alloc, config.error_bg, config.error_fg);
}, },
.password => password.handle(&event, insert_mode) catch { .password => password.handle(&event, insert_mode) catch {
try info_line.addError(lang.err_alloc); try info_line.addMessage(lang.err_alloc, config.error_bg, config.error_fg);
}, },
} }
update = true; update = true;

View File

@ -2,69 +2,52 @@ 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 ErrorMessage = struct { width: u8, text: []const u8 };
const InfoLine = @This(); const InfoLine = @This();
error_list: ArrayList(ErrorMessage), const Message = struct {
error_bg: u16, width: u8,
error_fg: u16, text: []const u8,
text: []const u8, bg: u16,
width: u8, fg: u16,
};
const MessageList = std.ArrayList(Message);
messages: MessageList,
pub fn init(allocator: std.mem.Allocator) InfoLine { pub fn init(allocator: std.mem.Allocator) InfoLine {
return .{ return .{
.error_list = ArrayList(ErrorMessage).init(allocator), .messages = MessageList.init(allocator),
.error_bg = 0,
.error_fg = 258,
.text = "",
.width = 0,
}; };
} }
pub fn deinit(self: InfoLine) void { pub fn deinit(self: InfoLine) void {
self.error_list.deinit(); self.messages.deinit();
} }
pub fn setText(self: *InfoLine, text: []const u8) !void { pub fn addMessage(self: *InfoLine, text: []const u8, bg: u16, fg: u16) !void {
self.width = if (text.len > 0) try utils.strWidth(text) else 0; if (text.len == 0) return;
self.text = text;
}
pub fn addError(self: *InfoLine, error_message: []const u8) !void { try self.messages.append(.{
if (error_message.len > 0) { .width = try utils.strWidth(text),
const entry = .{ .text = text,
.width = try utils.strWidth(error_message), .bg = bg,
.text = error_message, .fg = fg,
}; });
try self.error_list.append(entry);
}
} }
pub fn draw(self: InfoLine, buffer: TerminalBuffer) !void { pub fn draw(self: InfoLine, buffer: TerminalBuffer) !void {
var text: []const u8 = self.text; if (self.messages.items.len == 0) return;
var bg: u16 = buffer.bg;
var fg: u16 = buffer.fg;
var width: u8 = self.width;
if (self.error_list.items.len > 0) { const entry = self.messages.getLast();
const entry = self.error_list.getLast(); if (entry.width == 0 or buffer.box_width <= entry.width) return;
text = entry.text;
bg = self.error_bg;
fg = self.error_fg;
width = entry.width;
}
if (width > 0 and buffer.box_width > width) { const label_y = buffer.box_y + buffer.margin_box_v;
const label_y = buffer.box_y + buffer.margin_box_v; const x = buffer.box_x + ((buffer.box_width - entry.width) / 2);
const x = buffer.box_x + ((buffer.box_width - width) / 2); TerminalBuffer.drawColorLabel(entry.text, x, label_y, entry.fg, entry.bg);
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 {
// draw over the area // Draw over the area
const y = buffer.box_y + buffer.margin_box_v; const y = buffer.box_y + buffer.margin_box_v;
const spaces = try allocator.alloc(u8, buffer.box_width); const spaces = try allocator.alloc(u8, buffer.box_width);
defer allocator.free(spaces); defer allocator.free(spaces);