Added FTP/FTPs options

This commit is contained in:
ChristianVisintin
2020-12-01 15:14:33 +01:00
parent bc17c9bbc1
commit 3944367fb3

View File

@@ -44,6 +44,12 @@ use tui::{
}; };
use unicode_width::UnicodeWidthStr; use unicode_width::UnicodeWidthStr;
enum InputProtocol {
Sftp,
Ftp,
Ftps,
}
/// ### InputField /// ### InputField
/// ///
/// InputField describes the current input field to edit /// InputField describes the current input field to edit
@@ -75,11 +81,13 @@ pub struct AuthActivity {
pub protocol: FileTransferProtocol, pub protocol: FileTransferProtocol,
pub username: String, pub username: String,
pub password: String, pub password: String,
pub secure: bool, // Special option used by some protocols (such as FTP)
pub submit: bool, // becomes true after user has submitted fields pub submit: bool, // becomes true after user has submitted fields
pub quit: bool, // Becomes true if user has pressed esc pub quit: bool, // Becomes true if user has pressed esc
context: Option<Context>, context: Option<Context>,
selected_field: InputField, selected_field: InputField,
input_mode: InputMode, input_mode: InputMode,
input_protocol: InputProtocol,
popup_message: Option<String>, popup_message: Option<String>,
password_placeholder: String, password_placeholder: String,
redraw: bool, // Should ui actually be redrawned? redraw: bool, // Should ui actually be redrawned?
@@ -96,6 +104,8 @@ impl AuthActivity {
protocol: FileTransferProtocol::Sftp, protocol: FileTransferProtocol::Sftp,
username: String::new(), username: String::new(),
password: String::new(), password: String::new(),
input_protocol: InputProtocol::Sftp,
secure: false,
submit: false, submit: false,
quit: false, quit: false,
context: None, context: None,
@@ -107,6 +117,27 @@ impl AuthActivity {
} }
} }
/// ### change_opt_based_on_protocol
///
/// Change current options based on the selected protocol
fn change_opt_based_on_protocol(&mut self) {
// Change options based on current protocol
match self.input_protocol {
InputProtocol::Sftp => {
self.secure = false;
self.protocol = FileTransferProtocol::Sftp;
},
InputProtocol::Ftp => {
self.secure = false;
self.protocol = FileTransferProtocol::Ftp;
},
InputProtocol::Ftps => {
self.secure = true;
self.protocol = FileTransferProtocol::Ftp;
}
}
}
/// ### set_input_mode /// ### set_input_mode
/// ///
/// Update input mode based on current parameters /// Update input mode based on current parameters
@@ -228,19 +259,23 @@ impl AuthActivity {
KeyCode::Left => { KeyCode::Left => {
// If current field is Protocol handle event... (move element left) // If current field is Protocol handle event... (move element left)
if self.selected_field == InputField::Protocol { if self.selected_field == InputField::Protocol {
self.protocol = match self.protocol { self.input_protocol = match self.input_protocol {
FileTransferProtocol::Sftp => FileTransferProtocol::Sftp, InputProtocol::Sftp => InputProtocol::Ftps, // End of list (wrap)
FileTransferProtocol::Ftp => FileTransferProtocol::Sftp, // End of list InputProtocol::Ftp => InputProtocol::Sftp,
} InputProtocol::Ftps => InputProtocol::Ftp,
};
self.change_opt_based_on_protocol();
} }
} }
KeyCode::Right => { KeyCode::Right => {
// If current field is Protocol handle event... ( move element right ) // If current field is Protocol handle event... ( move element right )
if self.selected_field == InputField::Protocol { if self.selected_field == InputField::Protocol {
self.protocol = match self.protocol { self.input_protocol = match self.input_protocol {
FileTransferProtocol::Sftp => FileTransferProtocol::Ftp, InputProtocol::Sftp => InputProtocol::Ftp,
FileTransferProtocol::Ftp => FileTransferProtocol::Ftp, // End of list InputProtocol::Ftp => InputProtocol::Ftps,
} InputProtocol::Ftps => InputProtocol::Sftp, // End of list (wrap)
};
self.change_opt_based_on_protocol();
} }
} }
_ => { /* Nothing to do */ } _ => { /* Nothing to do */ }
@@ -274,7 +309,7 @@ impl AuthActivity {
fn draw_remote_address(&self) -> Paragraph { fn draw_remote_address(&self) -> Paragraph {
Paragraph::new(self.address.as_ref()) Paragraph::new(self.address.as_ref())
.style(match self.selected_field { .style(match self.selected_field {
InputField::Address => Style::default().fg(Color::Cyan), InputField::Address => Style::default().fg(Color::Yellow),
_ => Style::default(), _ => Style::default(),
}) })
.block( .block(
@@ -290,7 +325,7 @@ impl AuthActivity {
fn draw_remote_port(&self) -> Paragraph { fn draw_remote_port(&self) -> Paragraph {
Paragraph::new(self.port.as_ref()) Paragraph::new(self.port.as_ref())
.style(match self.selected_field { .style(match self.selected_field {
InputField::Port => Style::default().fg(Color::Yellow), InputField::Port => Style::default().fg(Color::Cyan),
_ => Style::default(), _ => Style::default(),
}) })
.block(Block::default().borders(Borders::ALL).title("Remote port")) .block(Block::default().borders(Borders::ALL).title("Remote port"))
@@ -300,10 +335,11 @@ impl AuthActivity {
/// ///
/// Draw protocol select /// Draw protocol select
fn draw_protocol_select(&self) -> Tabs { fn draw_protocol_select(&self) -> Tabs {
let protocols: Vec<Spans> = vec![Spans::from("SFTP"), Spans::from("FTP")]; let protocols: Vec<Spans> = vec![Spans::from("SFTP"), Spans::from("FTP"), Spans::from("FTPS")];
let index: usize = match self.protocol { let index: usize = match self.input_protocol {
FileTransferProtocol::Sftp => 0, InputProtocol::Sftp => 0,
FileTransferProtocol::Ftp => 1, InputProtocol::Ftp => 1,
InputProtocol::Ftps => 2,
}; };
Tabs::new(protocols) Tabs::new(protocols)
.block(Block::default().borders(Borders::ALL).title("Protocol")) .block(Block::default().borders(Borders::ALL).title("Protocol"))
@@ -315,7 +351,8 @@ impl AuthActivity {
.highlight_style( .highlight_style(
Style::default() Style::default()
.add_modifier(Modifier::BOLD) .add_modifier(Modifier::BOLD)
.fg(Color::Green), .bg(Color::Green)
.fg(Color::Black)
) )
} }