Fixed some instances where padding was hardcoded. Added container and prompt row padding as configurable options.

This commit is contained in:
Antoine POPINEAU 2020-07-01 16:06:03 +02:00
parent 85ca5cceda
commit 35e5445060
No known key found for this signature in database
GPG Key ID: A78AC64694F84063
3 changed files with 42 additions and 15 deletions

View File

@ -15,13 +15,17 @@ Options:
-g, --greeting GREETING -g, --greeting GREETING
show custom text above login prompt show custom text above login prompt
-t, --time display the current date and time -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 ## 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 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. You may change the command that will be executed after opening a session by huttint `F2` and amending the command.

View File

@ -100,6 +100,26 @@ impl Greeter {
80 80
} }
pub fn container_padding(&self) -> u16 {
if let Some(value) = self.option("container-padding") {
if let Ok(padding) = value.parse::<u16>() {
return padding + 1;
}
}
2
}
pub fn prompt_padding(&self) -> u16 {
if let Some(value) = self.option("prompt-padding") {
if let Ok(padding) = value.parse::<u16>() {
return padding;
}
}
1
}
pub fn parse_options(&mut self) { pub fn parse_options(&mut self) {
let mut opts = Options::new(); let mut opts = Options::new();
@ -109,6 +129,8 @@ impl Greeter {
opts.optflag("i", "issue", "show the host's issue file"); opts.optflag("i", "issue", "show the host's issue file");
opts.optopt("g", "greeting", "show custom text above login prompt", "GREETING"); opts.optopt("g", "greeting", "show custom text above login prompt", "GREETING");
opts.optflag("t", "time", "display the current date and time"); 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::<Vec<String>>()) { self.config = match opts.parse(&env::args().collect::<Vec<String>>()) {
Ok(matches) => Some(matches), Ok(matches) => Some(matches),

View File

@ -11,8 +11,6 @@ use tui::{
use super::prompt_value; use super::prompt_value;
use crate::{info::get_hostname, Greeter, Mode}; use crate::{info::get_hostname, Greeter, Mode};
const PADDING: u16 = 2;
const GREETING_INDEX: usize = 0; const GREETING_INDEX: usize = 0;
const USERNAME_INDEX: usize = 1; const USERNAME_INDEX: usize = 1;
const ANSWER_INDEX: usize = 2; const ANSWER_INDEX: usize = 2;
@ -28,11 +26,13 @@ pub fn draw(greeter: &mut Greeter, f: &mut Frame<TermionBackend<RawTerminal<io::
let width = greeter.width(); let width = greeter.width();
let height = get_height(&greeter); let height = get_height(&greeter);
let container_padding = greeter.container_padding();
let prompt_padding = greeter.prompt_padding();
let x = (size.width - width) / 2; let x = (size.width - width) / 2;
let y = (size.height - height) / 2; let y = (size.height - height) / 2;
let container = Rect::new(x, y, width, height); let container = Rect::new(x, y, width, height);
let frame = Rect::new(x + PADDING, y + PADDING, width - PADDING, height - PADDING); let frame = Rect::new(x + container_padding, y + container_padding, width - container_padding, height - container_padding);
let hostname = format!(" {} {} ", TITLE, get_hostname()); let hostname = format!(" {} {} ", TITLE, get_hostname());
let block = Block::default().title(&hostname).borders(Borders::ALL).border_type(BorderType::Plain); let block = Block::default().title(&hostname).borders(Borders::ALL).border_type(BorderType::Plain);
@ -44,13 +44,12 @@ pub fn draw(greeter: &mut Greeter, f: &mut Frame<TermionBackend<RawTerminal<io::
let constraints = [ let constraints = [
Constraint::Length(greeting_height), // Greeting Constraint::Length(greeting_height), // Greeting
Constraint::Length(2), // Username Constraint::Length(1 + prompt_padding), // Username
Constraint::Length(if let Mode::Username = greeter.mode { message_height } else { 2 }), // Message or answer Constraint::Length(if let Mode::Username = greeter.mode { message_height } else { 1 + prompt_padding }), // Message or answer
Constraint::Length(if let Mode::Password = greeter.mode { message_height } else { 1 }), // Message Constraint::Length(if let Mode::Password = greeter.mode { message_height } else { 1 }), // Message
]; ];
let chunks = Layout::default().direction(Direction::Vertical).constraints(constraints.as_ref()).split(frame); let chunks = Layout::default().direction(Direction::Vertical).constraints(constraints.as_ref()).split(frame);
let cursor = chunks[USERNAME_INDEX]; let cursor = chunks[USERNAME_INDEX];
if let Some(greeting) = &greeting { if let Some(greeting) = &greeting {
@ -126,34 +125,36 @@ pub fn draw(greeter: &mut Greeter, f: &mut Frame<TermionBackend<RawTerminal<io::
Mode::Username => { Mode::Username => {
let offset = get_cursor_offset(greeter, greeter.username.len()); 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 => { Mode::Password => {
let offset = get_cursor_offset(greeter, greeter.answer.len()); let offset = get_cursor_offset(greeter, greeter.answer.len());
if greeter.secret { 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 { } 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 => { Mode::Command => {
let offset = get_cursor_offset(greeter, greeter.new_command.len()); 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 { 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 (_, message_height) = get_message_height(greeter, 2, 0);
let (_, greeting_height) = get_greeting_height(greeter, 1, 0); let (_, greeting_height) = get_greeting_height(greeter, 1, 0);
let initial = match greeter.mode { let initial = match greeter.mode {
Mode::Username | Mode::Command => 5, Mode::Username | Mode::Command => (2 * container_padding) + 1,
Mode::Password => 7, Mode::Password => (2 * container_padding) + 2 + (2 * prompt_padding),
}; };
match greeter.mode { match greeter.mode {