mirror of
https://github.com/apognu/tuigreet.git
synced 2024-11-22 12:44:06 +03:00
Refactored some UI utility and command prompt into separate modules.
This commit is contained in:
parent
0d664c755f
commit
b3d6df5655
@ -20,7 +20,7 @@ pub fn handle(greeter: &mut Greeter, events: &Events) -> Result<(), Box<dyn Erro
|
||||
Key::Right => greeter.cursor_offset += 1,
|
||||
|
||||
Key::F(2) => {
|
||||
greeter.new_command = greeter.command.clone().unwrap_or_else(String::new);
|
||||
greeter.new_command = greeter.command.clone().unwrap_or_default();
|
||||
greeter.previous_mode = greeter.mode;
|
||||
greeter.mode = Mode::Command;
|
||||
}
|
||||
|
51
src/ui/command.rs
Normal file
51
src/ui/command.rs
Normal file
@ -0,0 +1,51 @@
|
||||
use std::{error::Error, io};
|
||||
|
||||
use termion::raw::RawTerminal;
|
||||
use tui::{
|
||||
backend::TermionBackend,
|
||||
layout::{Constraint, Direction, Layout, Rect},
|
||||
widgets::{Block, BorderType, Borders, Paragraph, Text},
|
||||
Frame,
|
||||
};
|
||||
|
||||
use super::prompt_value;
|
||||
use crate::{ui::util::*, Greeter};
|
||||
|
||||
const CHANGE_COMMAND: &str = " Change session command ";
|
||||
const COMMAND: &str = "New command:";
|
||||
|
||||
pub fn draw(greeter: &mut Greeter, f: &mut Frame<TermionBackend<RawTerminal<io::Stdout>>>) -> Result<(u16, u16), Box<dyn Error>> {
|
||||
let size = f.size();
|
||||
|
||||
let width = greeter.width();
|
||||
let height = get_height(&greeter);
|
||||
let container_padding = greeter.container_padding();
|
||||
let x = (size.width - width) / 2;
|
||||
let y = (size.height - height) / 2;
|
||||
|
||||
let container = Rect::new(x, y, width, height);
|
||||
let frame = Rect::new(x + container_padding, y + container_padding, width - container_padding, height - container_padding);
|
||||
|
||||
let block = Block::default().title(CHANGE_COMMAND).borders(Borders::ALL).border_type(BorderType::Plain);
|
||||
|
||||
f.render_widget(block, container);
|
||||
|
||||
let constraints = [
|
||||
Constraint::Length(1), // Username
|
||||
];
|
||||
|
||||
let chunks = Layout::default().direction(Direction::Vertical).constraints(constraints.as_ref()).split(frame);
|
||||
let cursor = chunks[0];
|
||||
|
||||
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[0]);
|
||||
f.render_widget(command_value, Rect::new(1 + chunks[0].x + COMMAND.len() as u16, chunks[0].y, get_input_width(greeter, COMMAND), 1));
|
||||
|
||||
let offset = get_cursor_offset(greeter, greeter.new_command.len());
|
||||
|
||||
Ok((2 + cursor.x + COMMAND.len() as u16 + offset as u16, cursor.y + 1))
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
mod command;
|
||||
mod prompt;
|
||||
mod sessions;
|
||||
mod util;
|
||||
|
||||
use std::{
|
||||
error::Error,
|
||||
@ -81,6 +83,7 @@ pub fn draw(terminal: &mut Terminal<TermionBackend<RawTerminal<io::Stdout>>>, gr
|
||||
}
|
||||
|
||||
cursor = match greeter.mode {
|
||||
Mode::Command => self::command::draw(greeter, &mut f).ok(),
|
||||
Mode::Sessions => self::sessions::draw(greeter, &mut f).ok(),
|
||||
_ => self::prompt::draw(greeter, &mut f).ok(),
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ use tui::{
|
||||
Frame,
|
||||
};
|
||||
|
||||
use super::prompt_value;
|
||||
use super::{prompt_value, util::*};
|
||||
use crate::{info::get_hostname, Greeter, Mode};
|
||||
|
||||
const GREETING_INDEX: usize = 0;
|
||||
@ -18,7 +18,6 @@ const MESSAGE_INDEX: usize = 3;
|
||||
|
||||
const TITLE: &str = "Authenticate into";
|
||||
const USERNAME: &str = "Username:";
|
||||
const COMMAND: &str = "New command:";
|
||||
const WORKING: &str = "Please wait...";
|
||||
|
||||
pub fn draw(greeter: &mut Greeter, f: &mut Frame<TermionBackend<RawTerminal<io::Stdout>>>) -> Result<(u16, u16), Box<dyn Error>> {
|
||||
@ -43,10 +42,10 @@ pub fn draw(greeter: &mut Greeter, f: &mut Frame<TermionBackend<RawTerminal<io::
|
||||
let (greeting, greeting_height) = get_greeting_height(greeter, 1, 0);
|
||||
|
||||
let constraints = [
|
||||
Constraint::Length(greeting_height), // Greeting
|
||||
Constraint::Length(1 + prompt_padding), // Username
|
||||
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(greeting_height), // Greeting
|
||||
Constraint::Length(1 + prompt_padding), // Username
|
||||
Constraint::Length(if greeter.mode == Mode::Username { message_height } else { 1 + prompt_padding }), // Message or answer
|
||||
Constraint::Length(if greeter.mode == Mode::Password { message_height } else { 1 }), // Message
|
||||
];
|
||||
|
||||
let chunks = Layout::default().direction(Direction::Vertical).constraints(constraints.as_ref()).split(frame);
|
||||
@ -66,19 +65,6 @@ pub fn draw(greeter: &mut Greeter, f: &mut Frame<TermionBackend<RawTerminal<io::
|
||||
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(
|
||||
@ -120,7 +106,7 @@ pub fn draw(greeter: &mut Greeter, f: &mut Frame<TermionBackend<RawTerminal<io::
|
||||
}
|
||||
}
|
||||
|
||||
Mode::Sessions => {}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
match greeter.mode {
|
||||
@ -140,74 +126,6 @@ pub fn draw(greeter: &mut Greeter, f: &mut Frame<TermionBackend<RawTerminal<io::
|
||||
}
|
||||
}
|
||||
|
||||
Mode::Command => {
|
||||
let offset = get_cursor_offset(greeter, greeter.new_command.len());
|
||||
|
||||
Ok((2 + cursor.x + COMMAND.len() as u16 + offset as u16, USERNAME_INDEX as u16 + cursor.y))
|
||||
}
|
||||
|
||||
Mode::Sessions => Ok((1, 1)),
|
||||
_ => Ok((1, 1)),
|
||||
}
|
||||
}
|
||||
|
||||
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 => (2 * container_padding) + 1,
|
||||
Mode::Password => (2 * container_padding) + prompt_padding + 2,
|
||||
Mode::Sessions => 0,
|
||||
};
|
||||
|
||||
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) {
|
||||
if let Some(greeting) = &greeter.greeting {
|
||||
let width = greeter.width();
|
||||
let wrapped = textwrap::fill(greeting, width as usize - 4);
|
||||
let height = wrapped.trim_end().matches('\n').count();
|
||||
|
||||
(Some(wrapped), height as u16 + 1 + padding)
|
||||
} else {
|
||||
(None, fallback)
|
||||
}
|
||||
}
|
||||
|
||||
fn get_input_width(greeter: &Greeter, label: &str) -> u16 {
|
||||
greeter.width() - label.len() as u16 - 4 - 1
|
||||
}
|
||||
|
||||
fn get_message_height(greeter: &Greeter, padding: u16, fallback: u16) -> (Option<String>, u16) {
|
||||
if let Some(message) = &greeter.message {
|
||||
let width = greeter.width();
|
||||
let wrapped = textwrap::fill(message.trim_end(), width as usize - 4);
|
||||
let height = wrapped.trim_end().matches('\n').count();
|
||||
|
||||
(Some(wrapped), height as u16 + padding)
|
||||
} else {
|
||||
(None, fallback)
|
||||
}
|
||||
}
|
||||
|
||||
fn get_cursor_offset(greeter: &mut Greeter, length: usize) -> i16 {
|
||||
let mut offset = length as i16 + greeter.cursor_offset;
|
||||
|
||||
if offset < 0 {
|
||||
offset = 0;
|
||||
greeter.cursor_offset = -(length as i16);
|
||||
}
|
||||
|
||||
if offset > length as i16 {
|
||||
offset = length as i16;
|
||||
greeter.cursor_offset = 0;
|
||||
}
|
||||
|
||||
offset
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ use tui::{
|
||||
Frame,
|
||||
};
|
||||
|
||||
use super::util::*;
|
||||
use crate::Greeter;
|
||||
|
||||
const CHANGE_SESSION: &str = "Change session";
|
||||
@ -17,7 +18,7 @@ pub fn draw(greeter: &mut Greeter, f: &mut Frame<TermionBackend<RawTerminal<io::
|
||||
let size = f.size();
|
||||
|
||||
let width = greeter.width();
|
||||
let height: u16 = greeter.sessions.len() as u16 + 4;
|
||||
let height: u16 = get_height(greeter) + greeter.sessions.len() as u16;
|
||||
let x = (size.width - width) / 2;
|
||||
let y = (size.height - height) / 2;
|
||||
|
||||
|
63
src/ui/util.rs
Normal file
63
src/ui/util.rs
Normal file
@ -0,0 +1,63 @@
|
||||
use crate::{Greeter, Mode};
|
||||
|
||||
pub 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 => (2 * container_padding) + 1,
|
||||
Mode::Password => (2 * container_padding) + prompt_padding + 2,
|
||||
Mode::Sessions => (2 * container_padding),
|
||||
};
|
||||
|
||||
match greeter.mode {
|
||||
Mode::Command | Mode::Sessions => initial,
|
||||
_ => initial + greeting_height + message_height,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_input_width(greeter: &Greeter, label: &str) -> u16 {
|
||||
greeter.width() - label.len() as u16 - 4 - 1
|
||||
}
|
||||
|
||||
pub fn get_cursor_offset(greeter: &mut Greeter, length: usize) -> i16 {
|
||||
let mut offset = length as i16 + greeter.cursor_offset;
|
||||
|
||||
if offset < 0 {
|
||||
offset = 0;
|
||||
greeter.cursor_offset = -(length as i16);
|
||||
}
|
||||
|
||||
if offset > length as i16 {
|
||||
offset = length as i16;
|
||||
greeter.cursor_offset = 0;
|
||||
}
|
||||
|
||||
offset
|
||||
}
|
||||
|
||||
pub fn get_greeting_height(greeter: &Greeter, padding: u16, fallback: u16) -> (Option<String>, u16) {
|
||||
if let Some(greeting) = &greeter.greeting {
|
||||
let width = greeter.width();
|
||||
let wrapped = textwrap::fill(greeting, width as usize - 4);
|
||||
let height = wrapped.trim_end().matches('\n').count();
|
||||
|
||||
(Some(wrapped), height as u16 + 1 + padding)
|
||||
} else {
|
||||
(None, fallback)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_message_height(greeter: &Greeter, padding: u16, fallback: u16) -> (Option<String>, u16) {
|
||||
if let Some(message) = &greeter.message {
|
||||
let width = greeter.width();
|
||||
let wrapped = textwrap::fill(message.trim_end(), width as usize - 4);
|
||||
let height = wrapped.trim_end().matches('\n').count();
|
||||
|
||||
(Some(wrapped), height as u16 + padding)
|
||||
} else {
|
||||
(None, fallback)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user