Display error messages differently in info line (#661)

* Add list of error messages to InfoLine.zig

* Change info and error cases according to review

* Add changes from review for width calculation
This commit is contained in:
Moabeat 2024-07-28 13:02:42 +02:00 committed by GitHub
parent 93554d9ba3
commit 19d4b195f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 101 additions and 34 deletions

View File

@ -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

View File

@ -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;

View File

@ -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,

View File

@ -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, &params, .{});
@ -100,7 +101,11 @@ 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: {
// literal error message, due to language file not yet available
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 +120,11 @@ 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: {
// literal error message, due to language file not yet available
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,6 +137,9 @@ pub fn main() !void {
}
}
info_line.error_bg = config.error_bg;
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 {};
@ -178,13 +190,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 +239,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 +286,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 +322,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 +374,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 +398,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 +451,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 +527,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 +535,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);
@ -595,7 +607,7 @@ pub fn main() !void {
try 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,7 +628,7 @@ pub fn main() !void {
if (auth_err) |err| {
auth_fails += 1;
active_input = .password;
try info_line.setText(getAuthErrorMsg(err, lang));
try info_line.addError(getAuthErrorMsg(err, lang));
if (config.clear_password or err != error.PamAuthError) password.clear();
} else {
password.clear();
@ -665,10 +677,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;

View File

@ -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);
}
}

View File

@ -2,25 +2,67 @@ const std = @import("std");
const utils = @import("../utils.zig");
const TerminalBuffer = @import("../TerminalBuffer.zig");
const ArrayList = std.ArrayList;
const ErrorMessage = struct { width: u8, text: []const u8 };
const InfoLine = @This();
text: []const u8 = "",
width: u8 = 0,
error_list: ArrayList(ErrorMessage),
error_bg: u16,
error_fg: u16,
text: []const u8,
width: u8,
pub fn init(allocator: std.mem.Allocator) InfoLine {
return .{
.error_list = ArrayList(ErrorMessage).init(allocator),
.error_bg = 0,
.error_fg = 258,
.text = "",
.width = 0,
};
}
pub fn deinit(self: InfoLine) void {
self.error_list.deinit();
}
pub fn setText(self: *InfoLine, text: []const u8) !void {
self.width = if (text.len > 0) try utils.strWidth(text) else 0;
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);
buffer.drawLabel(self.text, x, label_y);
pub fn addError(self: *InfoLine, error_message: []const u8) !void {
if (error_message.len > 0) {
const entry = .{
.width = try utils.strWidth(error_message),
.text = error_message,
};
try self.error_list.append(entry);
}
}
pub fn draw(self: InfoLine, buffer: TerminalBuffer) !void {
var text: []const u8 = self.text;
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.error_list.getLast();
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 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 {
// draw over the area
const y = buffer.box_y + buffer.margin_box_v;