From a64d7efc695d28a9efd19f095365b8446b35aef3 Mon Sep 17 00:00:00 2001 From: AnErrupTion Date: Wed, 31 Jul 2024 10:34:27 +0200 Subject: [PATCH] Make asterisk optional (hides password if so) Signed-off-by: AnErrupTion --- res/config.ini | 24 +++++++++++++++--------- src/config/Config.zig | 2 +- src/main.zig | 6 +++--- src/tui/components/Text.zig | 28 ++++++++++++++++++++-------- 4 files changed, 39 insertions(+), 21 deletions(-) diff --git a/res/config.ini b/res/config.ini index da149a6..d2a7eee 100644 --- a/res/config.ini +++ b/res/config.ini @@ -5,12 +5,14 @@ animation = none # Format string for clock in top right corner (see strftime specification). Example: %c +# If null, the clock won't be shown clock = null # Enable/disable big clock bigclock = false # The character used to mask the password +# If null, the password will be hidden asterisk = * # Erase password input on failure @@ -35,12 +37,12 @@ vi_default_mode = normal #define TB_CYAN 0x07 #define TB_WHITE 0x08 # -# Setting both to zero makes `bg` black and `fg` white. To set the actual color palette you are encouraged to use another tool -# such as [mkinitcpio-colors](https://github.com/evanpurkhiser/mkinitcpio-colors). Note that the color palette defined with -# `mkinitcpio-colors` takes 16 colors (0-15), only values 0-8 are valid for `ly` config and these values do not correspond -# exactly. For instance, in defining palettes with `mkinitcpio-colors` the order is black, dark red, dark green, brown, dark -# blue, dark purple, dark cyan, light gray, dark gray, bright red, bright green, yellow, bright blue, bright purple, bright -# cyan, and white, indexed in that order 0 through 15. For example, the color defined for white (indexed at 15 in the mkinitcpio +# Setting both to zero makes `bg` black and `fg` white. To set the actual color palette you are encouraged to use another tool +# such as [mkinitcpio-colors](https://github.com/evanpurkhiser/mkinitcpio-colors). Note that the color palette defined with +# `mkinitcpio-colors` takes 16 colors (0-15), only values 0-8 are valid for `ly` config and these values do not correspond +# exactly. For instance, in defining palettes with `mkinitcpio-colors` the order is black, dark red, dark green, brown, dark +# blue, dark purple, dark cyan, light gray, dark gray, bright red, bright green, yellow, bright blue, bright purple, bright +# cyan, and white, indexed in that order 0 through 15. For example, the color defined for white (indexed at 15 in the mkinitcpio # config) will be used by `ly` for `fg = 8`. # Background color id @@ -63,9 +65,11 @@ cmatrix_fg = 3 border_fg = 8 # Title to show at the top of the main box +# If set to null, none will be shown box_title = null -# Initial text to show on the info line (Defaults to hostname) +# Initial text to show on the info line +# If set to null, the info line defaults to the hostname initial_info_text = null # Blank main box background @@ -134,7 +138,8 @@ tty = 2 # Console path console_dev = /dev/console -# Default path. If null, ly doesn't set a path. +# Default path +# If null, ly doesn't set a path path = /sbin:/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin # Event timeout in milliseconds @@ -161,7 +166,8 @@ wayland_cmd = /etc/ly/wsetup.sh # Wayland desktop environments waylandsessions = /usr/share/wayland-sessions -# xinitrc (hidden if null) +# xinitrc +# If null, the xinitrc session will be hidden xinitrc = ~/.xinitrc # Xorg server command diff --git a/src/config/Config.zig b/src/config/Config.zig index 11006d7..c14db0f 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -6,7 +6,7 @@ const Input = enums.Input; const ViMode = enums.ViMode; animation: Animation = .none, -asterisk: u8 = '*', +asterisk: ?u8 = '*', bg: u16 = 0, bigclock: bool = false, blank_box: bool = true, diff --git a/src/main.zig b/src/main.zig index 1f12bd7..7f49dfc 100644 --- a/src/main.zig +++ b/src/main.zig @@ -253,10 +253,10 @@ pub fn main() !void { try desktop.crawl(config.waylandsessions, .wayland); if (build_options.enable_x11_support) try desktop.crawl(config.xsessions, .x11); - var login = try Text.init(allocator, &buffer, config.max_login_len); + var login = try Text.init(allocator, &buffer, config.max_login_len, false, null); defer login.deinit(); - var password = try Text.init(allocator, &buffer, config.max_password_len); + var password = try Text.init(allocator, &buffer, config.max_password_len, true, config.asterisk); defer password.deinit(); var active_input = config.default_input; @@ -508,7 +508,7 @@ pub fn main() !void { desktop.draw(); login.draw(); - password.drawMasked(config.asterisk); + password.draw(); } else { std.time.sleep(std.time.ns_per_ms * 10); update = buffer.cascade(); diff --git a/src/tui/components/Text.zig b/src/tui/components/Text.zig index e65e4a6..2d76b76 100644 --- a/src/tui/components/Text.zig +++ b/src/tui/components/Text.zig @@ -19,8 +19,10 @@ visible_start: usize, visible_length: usize, x: usize, y: usize, +masked: bool, +maybe_mask: ?u8, -pub fn init(allocator: Allocator, buffer: *TerminalBuffer, max_length: usize) !Text { +pub fn init(allocator: Allocator, buffer: *TerminalBuffer, max_length: usize, masked: bool, maybe_mask: ?u8) !Text { const text = try DynamicString.initCapacity(allocator, max_length); return .{ @@ -33,6 +35,8 @@ pub fn init(allocator: Allocator, buffer: *TerminalBuffer, max_length: usize) !T .visible_length = 0, .x = 0, .y = 0, + .masked = masked, + .maybe_mask = maybe_mask, }; } @@ -78,10 +82,25 @@ pub fn handle(self: *Text, maybe_event: ?*termbox.tb_event, insert_mode: bool) ! } } + if (self.masked and self.maybe_mask == null) { + _ = termbox.tb_set_cursor(@intCast(self.x), @intCast(self.y)); + return; + } + _ = termbox.tb_set_cursor(@intCast(self.x + (self.cursor - self.visible_start)), @intCast(self.y)); } pub fn draw(self: Text) void { + if (self.masked) { + if (self.maybe_mask) |mask| { + const length = @min(self.text.items.len, self.visible_length - 1); + if (length == 0) return; + + self.buffer.drawCharMultiple(mask, self.x, self.y, length); + } + return; + } + const length = @min(self.text.items.len, self.visible_length); if (length == 0) return; @@ -96,13 +115,6 @@ pub fn draw(self: Text) void { self.buffer.drawLabel(visible_slice, self.x, self.y); } -pub fn drawMasked(self: Text, mask: u8) void { - const length = @min(self.text.items.len, self.visible_length - 1); - if (length == 0) return; - - self.buffer.drawCharMultiple(mask, self.x, self.y, length); -} - pub fn clear(self: *Text) void { self.text.clearRetainingCapacity(); self.end = 0;