Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Text Align option #136

Merged
merged 6 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/greeter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ impl SecretDisplay {
}
}

// This enum models text alignment options
#[derive(SmartDefault, Debug, Clone)]
pub enum GreetAlign {
#[default]
Center,
Left,
Right
}

#[derive(SmartDefault)]
pub struct Greeter {
pub debug: bool,
Expand Down Expand Up @@ -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))),
Expand Down Expand Up @@ -340,6 +349,18 @@ impl Greeter {
1
}

pub fn greet_align(&self) -> GreetAlign {
if let Some(value) = self.option("greet-align") {
match value.as_str() {
"left" => GreetAlign::Left,
"right" => GreetAlign::Right,
_ => GreetAlign::Center
}
} else {
GreetAlign::default()
}
}

// Sets the locale that will be used for this invocation from environment.
fn set_locale(&mut self) {
let locale = DesktopLanguageRequester::requested_languages()
Expand Down Expand Up @@ -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("", "greet-align", "alignment of the greeting text in the main 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]...'");
Expand Down
9 changes: 7 additions & 2 deletions src/ui/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tui::{
use crate::{
info::get_hostname,
ui::{prompt_value, util::*, Frame},
Greeter, Mode, SecretDisplay,
Greeter, Mode, SecretDisplay, GreetAlign
};

dthelegend marked this conversation as resolved.
Show resolved Hide resolved
use super::common::style::Themed;
Expand All @@ -27,6 +27,11 @@ pub fn draw(greeter: &mut Greeter, f: &mut Frame) -> Result<(u16, u16), Box<dyn

let container_padding = greeter.container_padding();
let prompt_padding = greeter.prompt_padding();
let greeting_alignment = match greeter.greet_align() {
GreetAlign::Center => Alignment::Center,
GreetAlign::Left => Alignment::Left,
GreetAlign::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));
Expand Down Expand Up @@ -59,7 +64,7 @@ pub fn draw(greeter: &mut Greeter, f: &mut Frame) -> Result<(u16, u16), Box<dyn

if let Some(greeting) = &greeting {
let greeting_text = greeting.trim_end();
let greeting_label = Paragraph::new(greeting_text).alignment(Alignment::Center).style(theme.of(&[Themed::Greet]));
let greeting_label = Paragraph::new(greeting_text).alignment(greeting_alignment).style(theme.of(&[Themed::Greet]));

f.render_widget(greeting_label, chunks[GREETING_INDEX]);
}
Expand Down