mirror of
https://github.com/apognu/tuigreet.git
synced 2024-11-26 07:28:57 +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,40 +11,50 @@ 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 {
|
||||||
greeter.working = true;
|
Mode::Username => {
|
||||||
greeter.message = None;
|
greeter.working = true;
|
||||||
|
greeter.message = None;
|
||||||
match greeter.mode {
|
greeter.request = Some(Request::CreateSession { username: greeter.username.clone() });
|
||||||
Mode::Username => {
|
greeter.answer = String::new();
|
||||||
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() });
|
|
||||||
}
|
|
||||||
|
|
||||||
Mode::Password => {
|
|
||||||
greeter.request = Some(Request::PostAuthMessageResponse {
|
|
||||||
response: Some(greeter.answer.clone()),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
greeter.answer = String::new();
|
Mode::Password => {
|
||||||
}
|
greeter.working = true;
|
||||||
|
greeter.message = None;
|
||||||
|
|
||||||
|
greeter.request = Some(Request::PostAuthMessageResponse {
|
||||||
|
response: Some(greeter.answer.clone()),
|
||||||
|
});
|
||||||
|
|
||||||
|
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),
|
||||||
|
|
||||||
@ -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,51 +60,65 @@ 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());
|
||||||
|
|
||||||
f.render_widget(username_label, chunks[USERNAME_INDEX]);
|
match greeter.mode {
|
||||||
f.render_widget(
|
Mode::Command => {
|
||||||
username_value,
|
let command_label_text = [prompt_value(COMMAND)];
|
||||||
Rect::new(1 + chunks[USERNAME_INDEX].x + USERNAME.len() as u16, chunks[USERNAME_INDEX].y, get_input_width(greeter, USERNAME), 1),
|
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());
|
||||||
let answer_text = if greeter.working {
|
|
||||||
[Text::raw(WORKING)]
|
|
||||||
} else {
|
|
||||||
[Text::styled(&greeter.prompt, Style::default().modifier(Modifier::BOLD))]
|
|
||||||
};
|
|
||||||
let answer_label = Paragraph::new(answer_text.iter());
|
|
||||||
|
|
||||||
if let Mode::Password = greeter.mode {
|
|
||||||
f.render_widget(answer_label, chunks[ANSWER_INDEX]);
|
|
||||||
|
|
||||||
if !greeter.secret {
|
|
||||||
let answer_value_text = [Text::raw(&greeter.answer)];
|
|
||||||
let answer_value = Paragraph::new(answer_value_text.iter());
|
|
||||||
|
|
||||||
|
f.render_widget(command_label, chunks[USERNAME_INDEX]);
|
||||||
f.render_widget(
|
f.render_widget(
|
||||||
answer_value,
|
command_value,
|
||||||
Rect::new(
|
Rect::new(1 + chunks[USERNAME_INDEX].x + COMMAND.len() as u16, chunks[USERNAME_INDEX].y, get_input_width(greeter, COMMAND), 1),
|
||||||
chunks[ANSWER_INDEX].x + greeter.prompt.len() as u16,
|
|
||||||
chunks[ANSWER_INDEX].y,
|
|
||||||
get_input_width(greeter, &greeter.prompt),
|
|
||||||
1,
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(ref message) = message {
|
Mode::Username | Mode::Password => {
|
||||||
let message_text = [Text::raw(message)];
|
f.render_widget(username_label, chunks[USERNAME_INDEX]);
|
||||||
let message = Paragraph::new(message_text.iter());
|
f.render_widget(
|
||||||
|
username_value,
|
||||||
|
Rect::new(1 + chunks[USERNAME_INDEX].x + USERNAME.len() as u16, chunks[USERNAME_INDEX].y, get_input_width(greeter, USERNAME), 1),
|
||||||
|
);
|
||||||
|
|
||||||
match greeter.mode {
|
let answer_text = if greeter.working { [Text::raw(WORKING)] } else { [prompt_value(&greeter.prompt)] };
|
||||||
Mode::Username => f.render_widget(message, chunks[ANSWER_INDEX]),
|
let answer_label = Paragraph::new(answer_text.iter());
|
||||||
Mode::Password => f.render_widget(message, chunks[MESSAGE_INDEX]),
|
|
||||||
|
if greeter.mode == Mode::Password || greeter.previous_mode == Mode::Password {
|
||||||
|
f.render_widget(answer_label, chunks[ANSWER_INDEX]);
|
||||||
|
|
||||||
|
if !greeter.secret {
|
||||||
|
let answer_value_text = [Text::raw(&greeter.answer)];
|
||||||
|
let answer_value = Paragraph::new(answer_value_text.iter());
|
||||||
|
|
||||||
|
f.render_widget(
|
||||||
|
answer_value,
|
||||||
|
Rect::new(
|
||||||
|
chunks[ANSWER_INDEX].x + greeter.prompt.len() as u16,
|
||||||
|
chunks[ANSWER_INDEX].y,
|
||||||
|
get_input_width(greeter, &greeter.prompt),
|
||||||
|
1,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(ref message) = message {
|
||||||
|
let message_text = [Text::raw(message)];
|
||||||
|
let message = Paragraph::new(message_text.iter());
|
||||||
|
|
||||||
|
match greeter.mode {
|
||||||
|
Mode::Username => f.render_widget(message, chunks[ANSWER_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