From 6239bf0f312168db15daab3600e52eeaaab57d06 Mon Sep 17 00:00:00 2001 From: dthelegend Date: Thu, 2 May 2024 13:07:05 +0100 Subject: [PATCH] Add Text Align option --- src/greeter.rs | 24 +++++++++++++++++++++++- src/ui/prompt.rs | 17 +++++++++-------- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/greeter.rs b/src/greeter.rs index 597cc7e..e72047d 100644 --- a/src/greeter.rs +++ b/src/greeter.rs @@ -90,6 +90,15 @@ impl SecretDisplay { } } +// This enum models text alignment options +#[derive(SmartDefault, Debug, Clone)] +pub enum TextAlign { + #[default] + Center, + Left, + Right +} + #[derive(SmartDefault)] pub struct Greeter { pub debug: bool, @@ -270,7 +279,7 @@ impl Greeter { self.connect().await; } - // Connect to `greetd` and return a strea we can safely write to. + // Connect to `greetd` and return a stream we can safely write to. pub async fn connect(&mut self) { match UnixStream::connect(&self.socket).await { Ok(stream) => self.stream = Some(Arc::new(RwLock::new(stream))), @@ -340,6 +349,18 @@ impl Greeter { 1 } + pub fn text_align(&self) -> TextAlign { + if let Some(value) = self.option("text-align") { + match value.to_uppercase().as_str() { + "LEFT" | "L" => TextAlign::Left, + "RIGHT" | "R" => TextAlign::Right, + _ => TextAlign::Center + } + } else { + TextAlign::default() + } + } + // Sets the locale that will be used for this invocation from environment. fn set_locale(&mut self) { let locale = DesktopLanguageRequester::requested_languages() @@ -384,6 +405,7 @@ impl Greeter { opts.optopt("", "window-padding", "padding inside the terminal area (default: 0)", "PADDING"); opts.optopt("", "container-padding", "padding inside the main prompt container (default: 1)", "PADDING"); opts.optopt("", "prompt-padding", "padding between prompt rows (default: 1)", "PADDING"); + opts.optopt("", "text-align", "alignment of text in the prompt container (default: 'center')", "[left|center|right]"); opts.optopt("", "power-shutdown", "command to run to shut down the system", "'CMD [ARGS]...'"); opts.optopt("", "power-reboot", "command to run to reboot the system", "'CMD [ARGS]...'"); diff --git a/src/ui/prompt.rs b/src/ui/prompt.rs index 53400c5..f7b9080 100644 --- a/src/ui/prompt.rs +++ b/src/ui/prompt.rs @@ -7,11 +7,7 @@ use tui::{ widgets::{Block, BorderType, Borders, Paragraph}, }; -use crate::{ - info::get_hostname, - ui::{prompt_value, util::*, Frame}, - Greeter, Mode, SecretDisplay, -}; +use crate::{info::get_hostname, ui::{prompt_value, util::*, Frame}, Greeter, Mode, SecretDisplay, TextAlign}; use super::common::style::Themed; @@ -27,6 +23,11 @@ pub fn draw(greeter: &mut Greeter, f: &mut Frame) -> Result<(u16, u16), Box Alignment::Center, + TextAlign::Left => Alignment::Left, + TextAlign::Right => Alignment::Right + }; let container = Rect::new(x, y, width, height); let frame = Rect::new(x + container_padding, y + container_padding, width - (2 * container_padding), height - (2 * container_padding)); @@ -59,7 +60,7 @@ pub fn draw(greeter: &mut Greeter, f: &mut Frame) -> Result<(u16, u16), Box Result<(u16, u16), Box Result<(u16, u16), Box