From 35e5445060d7e463fe1de14fa1f55e3648fe6a65 Mon Sep 17 00:00:00 2001 From: Antoine POPINEAU Date: Wed, 1 Jul 2020 16:06:03 +0200 Subject: [PATCH] Fixed some instances where padding was hardcoded. Added container and prompt row padding as configurable options. --- README.md | 6 +++++- src/config.rs | 22 ++++++++++++++++++++++ src/ui/prompt.rs | 29 +++++++++++++++-------------- 3 files changed, 42 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index ef131c3..8ded65b 100644 --- a/README.md +++ b/README.md @@ -15,13 +15,17 @@ Options: -g, --greeting GREETING show custom text above login prompt -t, --time display the current date and time + --container-padding PADDING + padding inside the main prompt container (default: 1) + --prompt-padding PADDING + padding between prompt rows (default: 1) ``` ## Usage The default configuration tends to be as minimal as possible, visually speaking, only showing the authentication prompts and some minor information in the status bar. You may print your system's `/etc/issue` at the top of the prompt with `--issue` and the current date and time with `--time`. You may include a custom one-line greeting message instead of `/etc/issue` with `--greeting`. -The initial prompt container will be 80 column wide. You may change this with `--width` in case you need more space (for example, to account for large PAM challenge messages). +The initial prompt container will be 80 column wide. You may change this with `--width` in case you need more space (for example, to account for large PAM challenge messages). Please refer to usage information (`--help`) for more customizaton options. You may change the command that will be executed after opening a session by huttint `F2` and amending the command. diff --git a/src/config.rs b/src/config.rs index 9b2cd12..eb5d3e8 100644 --- a/src/config.rs +++ b/src/config.rs @@ -100,6 +100,26 @@ impl Greeter { 80 } + pub fn container_padding(&self) -> u16 { + if let Some(value) = self.option("container-padding") { + if let Ok(padding) = value.parse::() { + return padding + 1; + } + } + + 2 + } + + pub fn prompt_padding(&self) -> u16 { + if let Some(value) = self.option("prompt-padding") { + if let Ok(padding) = value.parse::() { + return padding; + } + } + + 1 + } + pub fn parse_options(&mut self) { let mut opts = Options::new(); @@ -109,6 +129,8 @@ impl Greeter { opts.optflag("i", "issue", "show the host's issue file"); opts.optopt("g", "greeting", "show custom text above login prompt", "GREETING"); opts.optflag("t", "time", "display the current date and time"); + opts.optopt("", "container-padding", "padding inside the main prompt container (default: 1)", "PADDING"); + opts.optopt("", "prompt-padding", "padding between prompt rows (default: 1)", "PADDING"); self.config = match opts.parse(&env::args().collect::>()) { Ok(matches) => Some(matches), diff --git a/src/ui/prompt.rs b/src/ui/prompt.rs index 5064b84..67a1ede 100644 --- a/src/ui/prompt.rs +++ b/src/ui/prompt.rs @@ -11,8 +11,6 @@ use tui::{ use super::prompt_value; use crate::{info::get_hostname, Greeter, Mode}; -const PADDING: u16 = 2; - const GREETING_INDEX: usize = 0; const USERNAME_INDEX: usize = 1; const ANSWER_INDEX: usize = 2; @@ -28,11 +26,13 @@ pub fn draw(greeter: &mut Greeter, f: &mut Frame { let offset = get_cursor_offset(greeter, greeter.username.len()); - Ok((2 + cursor.x + USERNAME.len() as u16 + offset as u16, 1 + cursor.y)) + Ok((2 + cursor.x + USERNAME.len() as u16 + offset as u16, USERNAME_INDEX as u16 + cursor.y)) } Mode::Password => { let offset = get_cursor_offset(greeter, greeter.answer.len()); if greeter.secret { - Ok((1 + cursor.x + greeter.prompt.len() as u16, 3 + cursor.y)) + Ok((1 + cursor.x + greeter.prompt.len() as u16, ANSWER_INDEX as u16 + prompt_padding + cursor.y)) } else { - Ok((1 + cursor.x + greeter.prompt.len() as u16 + offset as u16, 3 + cursor.y)) + Ok((1 + cursor.x + greeter.prompt.len() as u16 + offset as u16, ANSWER_INDEX as u16 + prompt_padding + cursor.y)) } } Mode::Command => { let offset = get_cursor_offset(greeter, greeter.new_command.len()); - Ok((2 + cursor.x + COMMAND.len() as u16 + offset as u16, 1 + cursor.y)) + Ok((2 + cursor.x + COMMAND.len() as u16 + offset as u16, USERNAME_INDEX as u16 + cursor.y)) } } } fn get_height(greeter: &Greeter) -> u16 { + let container_padding = greeter.container_padding(); + let prompt_padding = greeter.prompt_padding(); let (_, message_height) = get_message_height(greeter, 2, 0); let (_, greeting_height) = get_greeting_height(greeter, 1, 0); let initial = match greeter.mode { - Mode::Username | Mode::Command => 5, - Mode::Password => 7, + Mode::Username | Mode::Command => (2 * container_padding) + 1, + Mode::Password => (2 * container_padding) + 2 + (2 * prompt_padding), }; match greeter.mode {