mirror of
https://github.com/apognu/tuigreet.git
synced 2024-11-22 22:05:04 +03:00
Make changing command more discoverable and UI friendly.
This commit is contained in:
parent
ce159b5347
commit
85ca5cceda
@ -23,7 +23,7 @@ The default configuration tends to be as minimal as possible, visually speaking,
|
|||||||
|
|
||||||
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).
|
||||||
|
|
||||||
You may change the command that will be executed after opening a session by prefixing it with `!` in the username input box, for example `!bash`.
|
You may change the command that will be executed after opening a session by huttint `F2` and amending the command.
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
|
|
||||||
|
@ -27,9 +27,11 @@ impl Display for AuthStatus {
|
|||||||
|
|
||||||
impl Error for AuthStatus {}
|
impl Error for AuthStatus {}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, PartialEq)]
|
||||||
pub enum Mode {
|
pub enum Mode {
|
||||||
Username,
|
Username,
|
||||||
Password,
|
Password,
|
||||||
|
Command,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Mode {
|
impl Default for Mode {
|
||||||
@ -43,11 +45,13 @@ pub struct Greeter {
|
|||||||
pub config: Option<Matches>,
|
pub config: Option<Matches>,
|
||||||
pub stream: Option<UnixStream>,
|
pub stream: Option<UnixStream>,
|
||||||
pub command: Option<String>,
|
pub command: Option<String>,
|
||||||
|
pub previous_mode: Mode,
|
||||||
pub mode: Mode,
|
pub mode: Mode,
|
||||||
pub request: Option<Request>,
|
pub request: Option<Request>,
|
||||||
pub cursor_offset: i16,
|
pub cursor_offset: i16,
|
||||||
pub username: String,
|
pub username: String,
|
||||||
pub answer: String,
|
pub answer: String,
|
||||||
|
pub new_command: String,
|
||||||
pub secret: bool,
|
pub secret: bool,
|
||||||
pub prompt: String,
|
pub prompt: String,
|
||||||
pub greeting: Option<String>,
|
pub greeting: Option<String>,
|
||||||
|
@ -11,41 +11,51 @@ use crate::{
|
|||||||
pub fn handle(greeter: &mut Greeter, events: &Events) -> Result<(), Box<dyn Error>> {
|
pub fn handle(greeter: &mut Greeter, events: &Events) -> Result<(), Box<dyn Error>> {
|
||||||
if let Event::Input(input) = events.next()? {
|
if let Event::Input(input) = events.next()? {
|
||||||
match input {
|
match input {
|
||||||
Key::Esc => crate::exit(greeter, AuthStatus::Cancel)?,
|
Key::Esc => {
|
||||||
|
if let Mode::Command = greeter.mode {
|
||||||
|
greeter.mode = greeter.previous_mode;
|
||||||
|
} else {
|
||||||
|
crate::exit(greeter, AuthStatus::Cancel)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Key::Left => greeter.cursor_offset -= 1,
|
Key::Left => greeter.cursor_offset -= 1,
|
||||||
Key::Right => greeter.cursor_offset += 1,
|
Key::Right => greeter.cursor_offset += 1,
|
||||||
|
|
||||||
|
Key::F(2) => {
|
||||||
|
greeter.new_command = greeter.command.clone().unwrap_or_else(String::new);
|
||||||
|
greeter.previous_mode = greeter.mode;
|
||||||
|
greeter.mode = Mode::Command;
|
||||||
|
}
|
||||||
|
|
||||||
Key::Ctrl('a') => greeter.cursor_offset = -(greeter.username.len() as i16),
|
Key::Ctrl('a') => greeter.cursor_offset = -(greeter.username.len() as i16),
|
||||||
Key::Ctrl('e') => greeter.cursor_offset = 0,
|
Key::Ctrl('e') => greeter.cursor_offset = 0,
|
||||||
|
|
||||||
Key::Char('\n') | Key::Char('\t') => {
|
Key::Char('\n') | Key::Char('\t') => match greeter.mode {
|
||||||
|
Mode::Username => {
|
||||||
greeter.working = true;
|
greeter.working = true;
|
||||||
greeter.message = None;
|
greeter.message = None;
|
||||||
|
|
||||||
match greeter.mode {
|
|
||||||
Mode::Username => {
|
|
||||||
if greeter.username.starts_with('!') {
|
|
||||||
greeter.command = Some(greeter.username.trim_start_matches('!').to_string());
|
|
||||||
greeter.username = String::new();
|
|
||||||
greeter.working = false;
|
|
||||||
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
greeter.request = Some(Request::CreateSession { username: greeter.username.clone() });
|
greeter.request = Some(Request::CreateSession { username: greeter.username.clone() });
|
||||||
|
greeter.answer = String::new();
|
||||||
}
|
}
|
||||||
|
|
||||||
Mode::Password => {
|
Mode::Password => {
|
||||||
|
greeter.working = true;
|
||||||
|
greeter.message = None;
|
||||||
|
|
||||||
greeter.request = Some(Request::PostAuthMessageResponse {
|
greeter.request = Some(Request::PostAuthMessageResponse {
|
||||||
response: Some(greeter.answer.clone()),
|
response: Some(greeter.answer.clone()),
|
||||||
})
|
});
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
greeter.answer = String::new();
|
greeter.answer = String::new();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Mode::Command => {
|
||||||
|
greeter.command = Some(greeter.new_command.clone());
|
||||||
|
greeter.mode = greeter.previous_mode;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
Key::Char(c) => insert_key(greeter, c),
|
Key::Char(c) => insert_key(greeter, c),
|
||||||
|
|
||||||
Key::Backspace | Key::Delete => delete_key(greeter, input),
|
Key::Backspace | Key::Delete => delete_key(greeter, input),
|
||||||
@ -61,6 +71,7 @@ fn insert_key(greeter: &mut Greeter, c: char) {
|
|||||||
let value = match greeter.mode {
|
let value = match greeter.mode {
|
||||||
Mode::Username => &mut greeter.username,
|
Mode::Username => &mut greeter.username,
|
||||||
Mode::Password => &mut greeter.answer,
|
Mode::Password => &mut greeter.answer,
|
||||||
|
Mode::Command => &mut greeter.new_command,
|
||||||
};
|
};
|
||||||
|
|
||||||
let index = value.len() as i16 + greeter.cursor_offset;
|
let index = value.len() as i16 + greeter.cursor_offset;
|
||||||
@ -72,6 +83,7 @@ fn delete_key(greeter: &mut Greeter, key: Key) {
|
|||||||
let value = match greeter.mode {
|
let value = match greeter.mode {
|
||||||
Mode::Username => &mut greeter.username,
|
Mode::Username => &mut greeter.username,
|
||||||
Mode::Password => &mut greeter.answer,
|
Mode::Password => &mut greeter.answer,
|
||||||
|
Mode::Command => &mut greeter.new_command,
|
||||||
};
|
};
|
||||||
|
|
||||||
let index = match key {
|
let index = match key {
|
||||||
|
@ -18,6 +18,7 @@ use tui::{
|
|||||||
use crate::Greeter;
|
use crate::Greeter;
|
||||||
|
|
||||||
const EXIT: &str = "Exit";
|
const EXIT: &str = "Exit";
|
||||||
|
const CHANGE_COMMAND: &str = "Change command";
|
||||||
const COMMAND: &str = "COMMAND";
|
const COMMAND: &str = "COMMAND";
|
||||||
|
|
||||||
pub fn draw(terminal: &mut Terminal<TermionBackend<RawTerminal<io::Stdout>>>, greeter: &mut Greeter) -> Result<(), Box<dyn Error>> {
|
pub fn draw(terminal: &mut Terminal<TermionBackend<RawTerminal<io::Stdout>>>, greeter: &mut Greeter) -> Result<(), Box<dyn Error>> {
|
||||||
@ -50,7 +51,14 @@ pub fn draw(terminal: &mut Terminal<TermionBackend<RawTerminal<io::Stdout>>>, gr
|
|||||||
}
|
}
|
||||||
|
|
||||||
let command = greeter.command.clone().unwrap_or_else(|| "-".to_string());
|
let command = greeter.command.clone().unwrap_or_else(|| "-".to_string());
|
||||||
let status_text = [status_label("ESC"), status_value(format!(" {} ", EXIT)), status_label(COMMAND), status_value(format!(" {} ", command))];
|
let status_text = [
|
||||||
|
status_label("ESC"),
|
||||||
|
status_value(EXIT),
|
||||||
|
status_label("F2"),
|
||||||
|
status_value(CHANGE_COMMAND),
|
||||||
|
status_label(COMMAND),
|
||||||
|
status_value(command),
|
||||||
|
];
|
||||||
let status = Paragraph::new(status_text.iter());
|
let status = Paragraph::new(status_text.iter());
|
||||||
|
|
||||||
f.render_widget(status, chunks[2]);
|
f.render_widget(status, chunks[2]);
|
||||||
@ -82,5 +90,12 @@ fn status_value<'s, S>(text: S) -> Text<'s>
|
|||||||
where
|
where
|
||||||
S: Into<String>,
|
S: Into<String>,
|
||||||
{
|
{
|
||||||
Text::raw(text.into())
|
Text::raw(format!(" {} ", text.into()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn prompt_value<'s, S>(text: S) -> Text<'s>
|
||||||
|
where
|
||||||
|
S: Into<String>,
|
||||||
|
{
|
||||||
|
Text::styled(text.into(), Style::default().modifier(Modifier::BOLD))
|
||||||
}
|
}
|
||||||
|
@ -4,11 +4,11 @@ use termion::raw::RawTerminal;
|
|||||||
use tui::{
|
use tui::{
|
||||||
backend::TermionBackend,
|
backend::TermionBackend,
|
||||||
layout::{Alignment, Constraint, Direction, Layout, Rect},
|
layout::{Alignment, Constraint, Direction, Layout, Rect},
|
||||||
style::{Modifier, Style},
|
|
||||||
widgets::{Block, BorderType, Borders, Paragraph, Text},
|
widgets::{Block, BorderType, Borders, Paragraph, Text},
|
||||||
Frame,
|
Frame,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use super::prompt_value;
|
||||||
use crate::{info::get_hostname, Greeter, Mode};
|
use crate::{info::get_hostname, Greeter, Mode};
|
||||||
|
|
||||||
const PADDING: u16 = 2;
|
const PADDING: u16 = 2;
|
||||||
@ -20,6 +20,7 @@ const MESSAGE_INDEX: usize = 3;
|
|||||||
|
|
||||||
const TITLE: &str = "Authenticate into";
|
const TITLE: &str = "Authenticate into";
|
||||||
const USERNAME: &str = "Username:";
|
const USERNAME: &str = "Username:";
|
||||||
|
const COMMAND: &str = "New command:";
|
||||||
const WORKING: &str = "Please wait...";
|
const WORKING: &str = "Please wait...";
|
||||||
|
|
||||||
pub fn draw(greeter: &mut Greeter, f: &mut Frame<TermionBackend<RawTerminal<io::Stdout>>>) -> Result<(u16, u16), Box<dyn Error>> {
|
pub fn draw(greeter: &mut Greeter, f: &mut Frame<TermionBackend<RawTerminal<io::Stdout>>>) -> Result<(u16, u16), Box<dyn Error>> {
|
||||||
@ -59,26 +60,37 @@ pub fn draw(greeter: &mut Greeter, f: &mut Frame<TermionBackend<RawTerminal<io::
|
|||||||
f.render_widget(greeting_label, chunks[GREETING_INDEX]);
|
f.render_widget(greeting_label, chunks[GREETING_INDEX]);
|
||||||
}
|
}
|
||||||
|
|
||||||
let username_text = [Text::styled(USERNAME, Style::default().modifier(Modifier::BOLD))];
|
let username_text = [prompt_value(USERNAME)];
|
||||||
let username_label = Paragraph::new(username_text.iter());
|
let username_label = Paragraph::new(username_text.iter());
|
||||||
|
|
||||||
let username_value_text = [Text::raw(&greeter.username)];
|
let username_value_text = [Text::raw(&greeter.username)];
|
||||||
let username_value = Paragraph::new(username_value_text.iter());
|
let username_value = Paragraph::new(username_value_text.iter());
|
||||||
|
|
||||||
|
match greeter.mode {
|
||||||
|
Mode::Command => {
|
||||||
|
let command_label_text = [prompt_value(COMMAND)];
|
||||||
|
let command_label = Paragraph::new(command_label_text.iter());
|
||||||
|
let command_value_text = [Text::raw(&greeter.new_command)];
|
||||||
|
let command_value = Paragraph::new(command_value_text.iter());
|
||||||
|
|
||||||
|
f.render_widget(command_label, chunks[USERNAME_INDEX]);
|
||||||
|
f.render_widget(
|
||||||
|
command_value,
|
||||||
|
Rect::new(1 + chunks[USERNAME_INDEX].x + COMMAND.len() as u16, chunks[USERNAME_INDEX].y, get_input_width(greeter, COMMAND), 1),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Mode::Username | Mode::Password => {
|
||||||
f.render_widget(username_label, chunks[USERNAME_INDEX]);
|
f.render_widget(username_label, chunks[USERNAME_INDEX]);
|
||||||
f.render_widget(
|
f.render_widget(
|
||||||
username_value,
|
username_value,
|
||||||
Rect::new(1 + chunks[USERNAME_INDEX].x + USERNAME.len() as u16, chunks[USERNAME_INDEX].y, get_input_width(greeter, USERNAME), 1),
|
Rect::new(1 + chunks[USERNAME_INDEX].x + USERNAME.len() as u16, chunks[USERNAME_INDEX].y, get_input_width(greeter, USERNAME), 1),
|
||||||
);
|
);
|
||||||
|
|
||||||
let answer_text = if greeter.working {
|
let answer_text = if greeter.working { [Text::raw(WORKING)] } else { [prompt_value(&greeter.prompt)] };
|
||||||
[Text::raw(WORKING)]
|
|
||||||
} else {
|
|
||||||
[Text::styled(&greeter.prompt, Style::default().modifier(Modifier::BOLD))]
|
|
||||||
};
|
|
||||||
let answer_label = Paragraph::new(answer_text.iter());
|
let answer_label = Paragraph::new(answer_text.iter());
|
||||||
|
|
||||||
if let Mode::Password = greeter.mode {
|
if greeter.mode == Mode::Password || greeter.previous_mode == Mode::Password {
|
||||||
f.render_widget(answer_label, chunks[ANSWER_INDEX]);
|
f.render_widget(answer_label, chunks[ANSWER_INDEX]);
|
||||||
|
|
||||||
if !greeter.secret {
|
if !greeter.secret {
|
||||||
@ -104,6 +116,9 @@ pub fn draw(greeter: &mut Greeter, f: &mut Frame<TermionBackend<RawTerminal<io::
|
|||||||
match greeter.mode {
|
match greeter.mode {
|
||||||
Mode::Username => f.render_widget(message, chunks[ANSWER_INDEX]),
|
Mode::Username => f.render_widget(message, chunks[ANSWER_INDEX]),
|
||||||
Mode::Password => f.render_widget(message, chunks[MESSAGE_INDEX]),
|
Mode::Password => f.render_widget(message, chunks[MESSAGE_INDEX]),
|
||||||
|
Mode::Command => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -123,6 +138,12 @@ pub fn draw(greeter: &mut Greeter, f: &mut Frame<TermionBackend<RawTerminal<io::
|
|||||||
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, 3 + 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))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,11 +152,14 @@ fn get_height(greeter: &Greeter) -> u16 {
|
|||||||
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 => 5,
|
Mode::Username | Mode::Command => 5,
|
||||||
Mode::Password => 7,
|
Mode::Password => 7,
|
||||||
};
|
};
|
||||||
|
|
||||||
initial + greeting_height + message_height
|
match greeter.mode {
|
||||||
|
Mode::Command => initial + greeting_height,
|
||||||
|
_ => initial + greeting_height + message_height,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_greeting_height(greeter: &Greeter, padding: u16, fallback: u16) -> (Option<String>, u16) {
|
fn get_greeting_height(greeter: &Greeter, padding: u16, fallback: u16) -> (Option<String>, u16) {
|
||||||
|
Loading…
Reference in New Issue
Block a user