Make asterisk optional (hides password if so)

Signed-off-by: AnErrupTion <anerruption@disroot.org>
This commit is contained in:
AnErrupTion
2024-07-31 10:34:27 +02:00
parent b592a11fb0
commit a64d7efc69
4 changed files with 39 additions and 21 deletions

View File

@@ -5,12 +5,14 @@
animation = none animation = none
# Format string for clock in top right corner (see strftime specification). Example: %c # Format string for clock in top right corner (see strftime specification). Example: %c
# If null, the clock won't be shown
clock = null clock = null
# Enable/disable big clock # Enable/disable big clock
bigclock = false bigclock = false
# The character used to mask the password # The character used to mask the password
# If null, the password will be hidden
asterisk = * asterisk = *
# Erase password input on failure # Erase password input on failure
@@ -63,9 +65,11 @@ cmatrix_fg = 3
border_fg = 8 border_fg = 8
# Title to show at the top of the main box # Title to show at the top of the main box
# If set to null, none will be shown
box_title = null 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 initial_info_text = null
# Blank main box background # Blank main box background
@@ -134,7 +138,8 @@ tty = 2
# Console path # Console path
console_dev = /dev/console 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 path = /sbin:/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin
# Event timeout in milliseconds # Event timeout in milliseconds
@@ -161,7 +166,8 @@ wayland_cmd = /etc/ly/wsetup.sh
# Wayland desktop environments # Wayland desktop environments
waylandsessions = /usr/share/wayland-sessions waylandsessions = /usr/share/wayland-sessions
# xinitrc (hidden if null) # xinitrc
# If null, the xinitrc session will be hidden
xinitrc = ~/.xinitrc xinitrc = ~/.xinitrc
# Xorg server command # Xorg server command

View File

@@ -6,7 +6,7 @@ const Input = enums.Input;
const ViMode = enums.ViMode; const ViMode = enums.ViMode;
animation: Animation = .none, animation: Animation = .none,
asterisk: u8 = '*', asterisk: ?u8 = '*',
bg: u16 = 0, bg: u16 = 0,
bigclock: bool = false, bigclock: bool = false,
blank_box: bool = true, blank_box: bool = true,

View File

@@ -253,10 +253,10 @@ pub fn main() !void {
try desktop.crawl(config.waylandsessions, .wayland); try desktop.crawl(config.waylandsessions, .wayland);
if (build_options.enable_x11_support) try desktop.crawl(config.xsessions, .x11); 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(); 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(); defer password.deinit();
var active_input = config.default_input; var active_input = config.default_input;
@@ -508,7 +508,7 @@ pub fn main() !void {
desktop.draw(); desktop.draw();
login.draw(); login.draw();
password.drawMasked(config.asterisk); password.draw();
} else { } else {
std.time.sleep(std.time.ns_per_ms * 10); std.time.sleep(std.time.ns_per_ms * 10);
update = buffer.cascade(); update = buffer.cascade();

View File

@@ -19,8 +19,10 @@ visible_start: usize,
visible_length: usize, visible_length: usize,
x: usize, x: usize,
y: 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); const text = try DynamicString.initCapacity(allocator, max_length);
return .{ return .{
@@ -33,6 +35,8 @@ pub fn init(allocator: Allocator, buffer: *TerminalBuffer, max_length: usize) !T
.visible_length = 0, .visible_length = 0,
.x = 0, .x = 0,
.y = 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)); _ = termbox.tb_set_cursor(@intCast(self.x + (self.cursor - self.visible_start)), @intCast(self.y));
} }
pub fn draw(self: Text) void { 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); const length = @min(self.text.items.len, self.visible_length);
if (length == 0) return; if (length == 0) return;
@@ -96,13 +115,6 @@ pub fn draw(self: Text) void {
self.buffer.drawLabel(visible_slice, self.x, self.y); 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 { pub fn clear(self: *Text) void {
self.text.clearRetainingCapacity(); self.text.clearRetainingCapacity();
self.end = 0; self.end = 0;