mirror of
https://github.com/veeso/termscp.git
synced 2025-12-07 09:36:00 -08:00
InputMode as Option<Popup> in AuthActivity
This commit is contained in:
@@ -27,7 +27,7 @@
|
||||
extern crate dirs;
|
||||
|
||||
// Locals
|
||||
use super::{AuthActivity, Color, DialogYesNoOption, InputMode, PopupType};
|
||||
use super::{AuthActivity, Color, DialogYesNoOption, Popup};
|
||||
use crate::system::bookmarks_client::BookmarksClient;
|
||||
use crate::system::environment;
|
||||
|
||||
@@ -89,7 +89,7 @@ impl AuthActivity {
|
||||
let port: u16 = match self.port.parse::<usize>() {
|
||||
Ok(val) => {
|
||||
if val > 65535 {
|
||||
self.input_mode = InputMode::Popup(PopupType::Alert(
|
||||
self.popup = Some(Popup::Alert(
|
||||
Color::Red,
|
||||
String::from("Specified port must be in range 0-65535"),
|
||||
));
|
||||
@@ -98,7 +98,7 @@ impl AuthActivity {
|
||||
val as u16
|
||||
}
|
||||
Err(_) => {
|
||||
self.input_mode = InputMode::Popup(PopupType::Alert(
|
||||
self.popup = Some(Popup::Alert(
|
||||
Color::Red,
|
||||
String::from("Specified port is not a number"),
|
||||
));
|
||||
@@ -174,7 +174,7 @@ impl AuthActivity {
|
||||
let port: u16 = match self.port.parse::<usize>() {
|
||||
Ok(val) => {
|
||||
if val > 65535 {
|
||||
self.input_mode = InputMode::Popup(PopupType::Alert(
|
||||
self.popup = Some(Popup::Alert(
|
||||
Color::Red,
|
||||
String::from("Specified port must be in range 0-65535"),
|
||||
));
|
||||
@@ -183,7 +183,7 @@ impl AuthActivity {
|
||||
val as u16
|
||||
}
|
||||
Err(_) => {
|
||||
self.input_mode = InputMode::Popup(PopupType::Alert(
|
||||
self.popup = Some(Popup::Alert(
|
||||
Color::Red,
|
||||
String::from("Specified port is not a number"),
|
||||
));
|
||||
@@ -208,7 +208,7 @@ impl AuthActivity {
|
||||
fn write_bookmarks(&mut self) {
|
||||
if let Some(bookmarks_cli) = self.bookmarks_client.as_ref() {
|
||||
if let Err(err) = bookmarks_cli.write_bookmarks() {
|
||||
self.input_mode = InputMode::Popup(PopupType::Alert(
|
||||
self.popup = Some(Popup::Alert(
|
||||
Color::Red,
|
||||
format!("Could not write bookmarks: {}", err),
|
||||
));
|
||||
@@ -225,12 +225,13 @@ impl AuthActivity {
|
||||
Ok(path) => {
|
||||
// If some configure client, otherwise do nothing; don't bother users telling them that bookmarks are not supported on their system.
|
||||
if let Some(path) = path {
|
||||
let (bookmarks_file, key_file): (PathBuf, PathBuf) = environment::get_bookmarks_paths(path.as_path());
|
||||
let (bookmarks_file, key_file): (PathBuf, PathBuf) =
|
||||
environment::get_bookmarks_paths(path.as_path());
|
||||
// Initialize client
|
||||
match BookmarksClient::new(bookmarks_file.as_path(), key_file.as_path(), 16) {
|
||||
Ok(cli) => self.bookmarks_client = Some(cli),
|
||||
Err(err) => {
|
||||
self.input_mode = InputMode::Popup(PopupType::Alert(
|
||||
self.popup = Some(Popup::Alert(
|
||||
Color::Red,
|
||||
format!(
|
||||
"Could not initialize bookmarks (at \"{}\", \"{}\"): {}",
|
||||
@@ -244,7 +245,7 @@ impl AuthActivity {
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
self.input_mode = InputMode::Popup(PopupType::Alert(
|
||||
self.popup = Some(Popup::Alert(
|
||||
Color::Red,
|
||||
format!("Could not initialize configuration directory: {}", err),
|
||||
))
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
|
||||
use super::{
|
||||
AuthActivity, DialogCallback, DialogYesNoOption, FileTransferProtocol, InputEvent, InputField,
|
||||
InputForm, InputMode, PopupType,
|
||||
InputForm, Popup,
|
||||
};
|
||||
|
||||
use crossterm::event::{KeyCode, KeyModifiers};
|
||||
@@ -36,13 +36,13 @@ impl AuthActivity {
|
||||
///
|
||||
/// Handle input event, based on current input mode
|
||||
pub(super) fn handle_input_event(&mut self, ev: &InputEvent) {
|
||||
let popup: Option<PopupType> = match &self.input_mode {
|
||||
InputMode::Popup(ptype) => Some(ptype.clone()),
|
||||
let popup: Option<Popup> = match &self.popup {
|
||||
Some(ptype) => Some(ptype.clone()),
|
||||
_ => None,
|
||||
};
|
||||
match self.input_mode {
|
||||
InputMode::Form => self.handle_input_event_mode_form(ev),
|
||||
InputMode::Popup(_) => {
|
||||
match &self.popup {
|
||||
None => self.handle_input_event_mode_form(ev),
|
||||
Some(_) => {
|
||||
if let Some(ptype) = popup {
|
||||
self.handle_input_event_mode_popup(ev, ptype)
|
||||
}
|
||||
@@ -69,7 +69,7 @@ impl AuthActivity {
|
||||
match key.code {
|
||||
KeyCode::Esc => {
|
||||
// Show quit dialog
|
||||
self.input_mode = InputMode::Popup(PopupType::YesNo(
|
||||
self.popup = Some(Popup::YesNo(
|
||||
String::from("Are you sure you want to quit termscp?"),
|
||||
AuthActivity::callback_quit,
|
||||
AuthActivity::callback_nothing_to_do,
|
||||
@@ -81,10 +81,8 @@ impl AuthActivity {
|
||||
// Check form
|
||||
// Check address
|
||||
if self.address.is_empty() {
|
||||
self.input_mode = InputMode::Popup(PopupType::Alert(
|
||||
Color::Red,
|
||||
String::from("Invalid address"),
|
||||
));
|
||||
self.popup =
|
||||
Some(Popup::Alert(Color::Red, String::from("Invalid address")));
|
||||
return;
|
||||
}
|
||||
// Check port
|
||||
@@ -92,7 +90,7 @@ impl AuthActivity {
|
||||
match self.port.parse::<usize>() {
|
||||
Ok(val) => {
|
||||
if val > 65535 {
|
||||
self.input_mode = InputMode::Popup(PopupType::Alert(
|
||||
self.popup = Some(Popup::Alert(
|
||||
Color::Red,
|
||||
String::from("Specified port must be in range 0-65535"),
|
||||
));
|
||||
@@ -100,7 +98,7 @@ impl AuthActivity {
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
self.input_mode = InputMode::Popup(PopupType::Alert(
|
||||
self.popup = Some(Popup::Alert(
|
||||
Color::Red,
|
||||
String::from("Specified port is not a number"),
|
||||
));
|
||||
@@ -157,7 +155,7 @@ impl AuthActivity {
|
||||
match ch {
|
||||
'H' | 'h' => {
|
||||
// Show help
|
||||
self.input_mode = InputMode::Popup(PopupType::Help);
|
||||
self.popup = Some(Popup::Help);
|
||||
}
|
||||
'C' | 'c' => {
|
||||
// Show setup
|
||||
@@ -167,7 +165,7 @@ impl AuthActivity {
|
||||
// Default choice option to no
|
||||
self.choice_opt = DialogYesNoOption::No;
|
||||
// Save bookmark as...
|
||||
self.input_mode = InputMode::Popup(PopupType::SaveBookmark);
|
||||
self.popup = Some(Popup::SaveBookmark);
|
||||
}
|
||||
_ => { /* Nothing to do */ }
|
||||
}
|
||||
@@ -225,7 +223,7 @@ impl AuthActivity {
|
||||
match key.code {
|
||||
KeyCode::Esc => {
|
||||
// Show quit dialog
|
||||
self.input_mode = InputMode::Popup(PopupType::YesNo(
|
||||
self.popup = Some(Popup::YesNo(
|
||||
String::from("Are you sure you want to quit termscp?"),
|
||||
AuthActivity::callback_quit,
|
||||
AuthActivity::callback_nothing_to_do,
|
||||
@@ -257,7 +255,7 @@ impl AuthActivity {
|
||||
}
|
||||
KeyCode::Delete => {
|
||||
// Ask if user wants to delete bookmark
|
||||
self.input_mode = InputMode::Popup(PopupType::YesNo(
|
||||
self.popup = Some(Popup::YesNo(
|
||||
String::from("Are you sure you want to delete the selected bookmark?"),
|
||||
AuthActivity::callback_del_bookmark,
|
||||
AuthActivity::callback_nothing_to_do,
|
||||
@@ -278,7 +276,7 @@ impl AuthActivity {
|
||||
}
|
||||
'E' | 'e' => {
|
||||
// Ask if user wants to delete bookmark; NOTE: same as <DEL>
|
||||
self.input_mode = InputMode::Popup(PopupType::YesNo(
|
||||
self.popup = Some(Popup::YesNo(
|
||||
String::from("Are you sure you want to delete the selected bookmark?"),
|
||||
AuthActivity::callback_del_bookmark,
|
||||
AuthActivity::callback_nothing_to_do,
|
||||
@@ -286,13 +284,13 @@ impl AuthActivity {
|
||||
}
|
||||
'H' | 'h' => {
|
||||
// Show help
|
||||
self.input_mode = InputMode::Popup(PopupType::Help);
|
||||
self.popup = Some(Popup::Help);
|
||||
}
|
||||
'S' | 's' => {
|
||||
// Default choice option to no
|
||||
self.choice_opt = DialogYesNoOption::No;
|
||||
// Save bookmark as...
|
||||
self.input_mode = InputMode::Popup(PopupType::SaveBookmark);
|
||||
self.popup = Some(Popup::SaveBookmark);
|
||||
}
|
||||
_ => { /* Nothing to do */ }
|
||||
},
|
||||
@@ -309,7 +307,7 @@ impl AuthActivity {
|
||||
match key.code {
|
||||
KeyCode::Esc => {
|
||||
// Show quit dialog
|
||||
self.input_mode = InputMode::Popup(PopupType::YesNo(
|
||||
self.popup = Some(Popup::YesNo(
|
||||
String::from("Are you sure you want to quit termscp?"),
|
||||
AuthActivity::callback_quit,
|
||||
AuthActivity::callback_nothing_to_do,
|
||||
@@ -341,7 +339,7 @@ impl AuthActivity {
|
||||
}
|
||||
KeyCode::Delete => {
|
||||
// Ask if user wants to delete bookmark
|
||||
self.input_mode = InputMode::Popup(PopupType::YesNo(
|
||||
self.popup = Some(Popup::YesNo(
|
||||
String::from("Are you sure you want to delete the selected host?"),
|
||||
AuthActivity::callback_del_bookmark,
|
||||
AuthActivity::callback_nothing_to_do,
|
||||
@@ -362,7 +360,7 @@ impl AuthActivity {
|
||||
}
|
||||
'E' | 'e' => {
|
||||
// Ask if user wants to delete bookmark; NOTE: same as <DEL>
|
||||
self.input_mode = InputMode::Popup(PopupType::YesNo(
|
||||
self.popup = Some(Popup::YesNo(
|
||||
String::from("Are you sure you want to delete the selected host?"),
|
||||
AuthActivity::callback_del_bookmark,
|
||||
AuthActivity::callback_nothing_to_do,
|
||||
@@ -370,13 +368,13 @@ impl AuthActivity {
|
||||
}
|
||||
'H' | 'h' => {
|
||||
// Show help
|
||||
self.input_mode = InputMode::Popup(PopupType::Help);
|
||||
self.popup = Some(Popup::Help);
|
||||
}
|
||||
'S' | 's' => {
|
||||
// Default choice option to no
|
||||
self.choice_opt = DialogYesNoOption::No;
|
||||
// Save bookmark as...
|
||||
self.input_mode = InputMode::Popup(PopupType::SaveBookmark);
|
||||
self.popup = Some(Popup::SaveBookmark);
|
||||
}
|
||||
_ => { /* Nothing to do */ }
|
||||
},
|
||||
@@ -388,12 +386,12 @@ impl AuthActivity {
|
||||
/// ### handle_input_event_mode_text
|
||||
///
|
||||
/// Handler for input event when in popup mode
|
||||
fn handle_input_event_mode_popup(&mut self, ev: &InputEvent, ptype: PopupType) {
|
||||
fn handle_input_event_mode_popup(&mut self, ev: &InputEvent, ptype: Popup) {
|
||||
match ptype {
|
||||
PopupType::Alert(_, _) => self.handle_input_event_mode_popup_alert(ev),
|
||||
PopupType::Help => self.handle_input_event_mode_popup_help(ev),
|
||||
PopupType::SaveBookmark => self.handle_input_event_mode_popup_save_bookmark(ev),
|
||||
PopupType::YesNo(_, yes_cb, no_cb) => {
|
||||
Popup::Alert(_, _) => self.handle_input_event_mode_popup_alert(ev),
|
||||
Popup::Help => self.handle_input_event_mode_popup_help(ev),
|
||||
Popup::SaveBookmark => self.handle_input_event_mode_popup_save_bookmark(ev),
|
||||
Popup::YesNo(_, yes_cb, no_cb) => {
|
||||
self.handle_input_event_mode_popup_yesno(ev, yes_cb, no_cb)
|
||||
}
|
||||
}
|
||||
@@ -406,7 +404,7 @@ impl AuthActivity {
|
||||
// Only enter should be allowed here
|
||||
if let InputEvent::Key(key) = ev {
|
||||
if matches!(key.code, KeyCode::Esc | KeyCode::Enter) {
|
||||
self.input_mode = InputMode::Form; // Hide popup
|
||||
self.popup = None; // Hide popup
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -419,7 +417,7 @@ impl AuthActivity {
|
||||
if let InputEvent::Key(key) = ev {
|
||||
if matches!(key.code, KeyCode::Esc | KeyCode::Enter) {
|
||||
// Set input mode back to form
|
||||
self.input_mode = InputMode::Form;
|
||||
self.popup = None;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -436,7 +434,7 @@ impl AuthActivity {
|
||||
// Clear current input text
|
||||
self.input_txt.clear();
|
||||
// Set mode back to form
|
||||
self.input_mode = InputMode::Form;
|
||||
self.popup = None;
|
||||
// Reset choice option to yes
|
||||
self.choice_opt = DialogYesNoOption::Yes;
|
||||
}
|
||||
@@ -446,7 +444,7 @@ impl AuthActivity {
|
||||
// Clear current input text
|
||||
self.input_txt.clear();
|
||||
// Set mode back to form BEFORE CALLBACKS!!! Callback can then overwrite this, clever uh?
|
||||
self.input_mode = InputMode::Form;
|
||||
self.popup = None;
|
||||
// Call cb
|
||||
self.callback_save_bookmark(input_text);
|
||||
// Reset choice option to yes
|
||||
@@ -477,7 +475,7 @@ impl AuthActivity {
|
||||
match key.code {
|
||||
KeyCode::Enter => {
|
||||
// @! Set input mode to Form BEFORE CALLBACKS!!! Callback can then overwrite this, clever uh?
|
||||
self.input_mode = InputMode::Form;
|
||||
self.popup = None;
|
||||
// Check if user selected yes or not
|
||||
match self.choice_opt {
|
||||
DialogYesNoOption::No => no_cb(self),
|
||||
|
||||
@@ -25,8 +25,7 @@
|
||||
|
||||
// Locals
|
||||
use super::{
|
||||
AuthActivity, Context, DialogYesNoOption, FileTransferProtocol, InputField, InputForm,
|
||||
InputMode, PopupType,
|
||||
AuthActivity, Context, DialogYesNoOption, FileTransferProtocol, InputField, InputForm, Popup,
|
||||
};
|
||||
use crate::utils::fmt::align_text_center;
|
||||
// Ext
|
||||
@@ -123,23 +122,23 @@ impl AuthActivity {
|
||||
f.render_stateful_widget(tab, bookmark_chunks[1], &mut recents_state);
|
||||
}
|
||||
// Draw popup
|
||||
if let InputMode::Popup(popup) = &self.input_mode {
|
||||
if let Some(popup) = &self.popup {
|
||||
// Calculate popup size
|
||||
let (width, height): (u16, u16) = match popup {
|
||||
PopupType::Alert(_, _) => (50, 10),
|
||||
PopupType::Help => (50, 70),
|
||||
PopupType::SaveBookmark => (20, 20),
|
||||
PopupType::YesNo(_, _, _) => (30, 10),
|
||||
Popup::Alert(_, _) => (50, 10),
|
||||
Popup::Help => (50, 70),
|
||||
Popup::SaveBookmark => (20, 20),
|
||||
Popup::YesNo(_, _, _) => (30, 10),
|
||||
};
|
||||
let popup_area: Rect = self.draw_popup_area(f.size(), width, height);
|
||||
f.render_widget(Clear, popup_area); //this clears out the background
|
||||
match popup {
|
||||
PopupType::Alert(color, txt) => f.render_widget(
|
||||
Popup::Alert(color, txt) => f.render_widget(
|
||||
self.draw_popup_alert(*color, txt.clone(), popup_area.width),
|
||||
popup_area,
|
||||
),
|
||||
PopupType::Help => f.render_widget(self.draw_popup_help(), popup_area),
|
||||
PopupType::SaveBookmark => {
|
||||
Popup::Help => f.render_widget(self.draw_popup_help(), popup_area),
|
||||
Popup::SaveBookmark => {
|
||||
let popup_chunks = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints(
|
||||
@@ -160,7 +159,7 @@ impl AuthActivity {
|
||||
popup_chunks[0].y + 1,
|
||||
)
|
||||
}
|
||||
PopupType::YesNo(txt, _, _) => {
|
||||
Popup::YesNo(txt, _, _) => {
|
||||
f.render_widget(self.draw_popup_yesno(txt.clone()), popup_area)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,26 +71,17 @@ enum DialogYesNoOption {
|
||||
No,
|
||||
}
|
||||
|
||||
/// ### PopupType
|
||||
/// ### Popup
|
||||
///
|
||||
/// PopupType describes the type of the popup displayed
|
||||
/// Popup describes the type of the popup displayed
|
||||
#[derive(Clone)]
|
||||
enum PopupType {
|
||||
enum Popup {
|
||||
Alert(Color, String), // Show a message displaying text with the provided color
|
||||
Help, // Help page
|
||||
SaveBookmark,
|
||||
YesNo(String, DialogCallback, DialogCallback), // Yes, no callback
|
||||
}
|
||||
|
||||
/// ### InputMode
|
||||
///
|
||||
/// InputMode describes the current input mode
|
||||
/// Each input mode handle the input events in a different way
|
||||
enum InputMode {
|
||||
Form,
|
||||
Popup(PopupType),
|
||||
}
|
||||
|
||||
#[derive(std::cmp::PartialEq)]
|
||||
/// ### InputForm
|
||||
///
|
||||
@@ -117,7 +108,7 @@ pub struct AuthActivity {
|
||||
bookmarks_client: Option<BookmarksClient>,
|
||||
config_client: Option<ConfigClient>,
|
||||
selected_field: InputField, // Selected field in AuthCredentials Form
|
||||
input_mode: InputMode,
|
||||
popup: Option<Popup>,
|
||||
input_form: InputForm,
|
||||
password_placeholder: String,
|
||||
redraw: bool, // Should ui actually be redrawned?
|
||||
@@ -151,7 +142,7 @@ impl AuthActivity {
|
||||
bookmarks_client: None,
|
||||
config_client: None,
|
||||
selected_field: InputField::Address,
|
||||
input_mode: InputMode::Form,
|
||||
popup: None,
|
||||
input_form: InputForm::AuthCredentials,
|
||||
password_placeholder: String::new(),
|
||||
redraw: true, // True at startup
|
||||
@@ -181,7 +172,7 @@ impl AuthActivity {
|
||||
self.config_client = Some(cli);
|
||||
}
|
||||
Err(err) => {
|
||||
self.input_mode = InputMode::Popup(PopupType::Alert(
|
||||
self.popup = Some(Popup::Alert(
|
||||
Color::Red,
|
||||
format!("Could not initialize user configuration: {}", err),
|
||||
))
|
||||
@@ -190,7 +181,7 @@ impl AuthActivity {
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
self.input_mode = InputMode::Popup(PopupType::Alert(
|
||||
self.popup = Some(Popup::Alert(
|
||||
Color::Red,
|
||||
format!("Could not initialize configuration directory: {}", err),
|
||||
))
|
||||
@@ -212,7 +203,7 @@ impl Activity for AuthActivity {
|
||||
self.context.as_mut().unwrap().clear_screen();
|
||||
// Put raw mode on enabled
|
||||
let _ = enable_raw_mode();
|
||||
self.input_mode = InputMode::Form;
|
||||
self.popup = None;
|
||||
// Init bookmarks client
|
||||
if self.bookmarks_client.is_none() {
|
||||
self.init_bookmarks_client();
|
||||
|
||||
Reference in New Issue
Block a user