mirror of
https://github.com/veeso/termscp.git
synced 2025-12-07 09:36:00 -08:00
enable/disable raw mode
This commit is contained in:
@@ -31,6 +31,10 @@ use crate::host::Localhost;
|
||||
use crate::ui::activities::{auth_activity::AuthActivity, auth_activity::ScpProtocol, Activity};
|
||||
use crate::ui::context::Context;
|
||||
|
||||
// Namespaces
|
||||
use std::thread::sleep;
|
||||
use std::time::Duration;
|
||||
|
||||
/// ### NextActivity
|
||||
///
|
||||
/// NextActivity identified the next identity to run once the current has ended
|
||||
@@ -56,13 +60,18 @@ struct FileTransferParams {
|
||||
pub struct ActivityManager {
|
||||
context: Context,
|
||||
ftparams: Option<FileTransferParams>,
|
||||
interval: Duration,
|
||||
}
|
||||
|
||||
impl ActivityManager {
|
||||
/// ### new
|
||||
///
|
||||
/// Initializes a new Activity Manager
|
||||
pub fn new(client: Box<dyn FileTransfer>, local_dir: &PathBuf) -> Result<ActivityManager, ()> {
|
||||
pub fn new(
|
||||
client: Box<dyn FileTransfer>,
|
||||
local_dir: &PathBuf,
|
||||
interval: Duration,
|
||||
) -> Result<ActivityManager, ()> {
|
||||
// Prepare Context
|
||||
let host: Localhost = match Localhost::new(local_dir.clone()) {
|
||||
Ok(h) => h,
|
||||
@@ -72,6 +81,7 @@ impl ActivityManager {
|
||||
Ok(ActivityManager {
|
||||
context: ctx,
|
||||
ftparams: None,
|
||||
interval: interval,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -100,7 +110,52 @@ impl ActivityManager {
|
||||
///
|
||||
/// Loop for activity manager. You need to provide the activity to start with
|
||||
/// Returns the exitcode
|
||||
pub fn run(&mut self, launch_activity: NextActivity) -> i32 {
|
||||
0
|
||||
pub fn run(&mut self, launch_activity: NextActivity) {
|
||||
let mut current_activity: Option<NextActivity> = Some(launch_activity);
|
||||
loop {
|
||||
current_activity = match current_activity {
|
||||
Some(activity) => match activity {
|
||||
NextActivity::Authentication => self.run_authentication(),
|
||||
NextActivity::FileTransfer => self.run_authentication(), // FIXME: change
|
||||
},
|
||||
None => break, // Exit
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Loops
|
||||
|
||||
/// ### run_authentication
|
||||
///
|
||||
/// Loop for Authentication activity.
|
||||
/// Returns when activity terminates.
|
||||
/// Returns the next activity to run
|
||||
fn run_authentication(&mut self) -> Option<NextActivity> {
|
||||
// Prepare activity
|
||||
let mut activity: AuthActivity = AuthActivity::new();
|
||||
// Prepare result
|
||||
let mut result: Option<NextActivity> = None;
|
||||
// Create activity
|
||||
activity.on_create(&mut self.context);
|
||||
loop {
|
||||
// Draw activity
|
||||
activity.on_draw(&mut self.context);
|
||||
// Check if has to be terminated
|
||||
if activity.quit {
|
||||
// Quit activities
|
||||
result = None;
|
||||
break;
|
||||
}
|
||||
if activity.submit {
|
||||
// User submitted, set next activity
|
||||
result = Some(NextActivity::FileTransfer);
|
||||
break;
|
||||
}
|
||||
// Sleep for ticks
|
||||
//sleep(self.interval);
|
||||
}
|
||||
// Destroy activity
|
||||
activity.on_destroy(&mut self.context);
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
28
src/main.rs
28
src/main.rs
@@ -89,28 +89,6 @@ fn main() {
|
||||
);
|
||||
std::process::exit(255);
|
||||
}
|
||||
// Match protocol first
|
||||
if let Some(val) = matches.opt_str("P") {
|
||||
match val.as_str() {
|
||||
"sftp" => {
|
||||
protocol = ScpProtocol::Sftp;
|
||||
// Set port to 22 as default
|
||||
port = 22;
|
||||
// Set default username to current
|
||||
username = Some(whoami::username());
|
||||
}
|
||||
"ftp" => {
|
||||
protocol = ScpProtocol::Ftp;
|
||||
// Set port to 21
|
||||
port = 21;
|
||||
}
|
||||
_ => {
|
||||
eprintln!("Unknown protocol '{}'", val);
|
||||
print_usage(opts);
|
||||
std::process::exit(255);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Match ticks
|
||||
if let Some(val) = matches.opt_str("T") {
|
||||
match val.parse::<usize>() {
|
||||
@@ -156,7 +134,7 @@ fn main() {
|
||||
Err(_) => PathBuf::from("/"),
|
||||
};
|
||||
// Create activity manager
|
||||
let mut manager: ActivityManager = match ActivityManager::new(file_transfer, &wrkdir) {
|
||||
let mut manager: ActivityManager = match ActivityManager::new(file_transfer, &wrkdir, ticks) {
|
||||
Ok(m) => m,
|
||||
Err(_) => {
|
||||
eprintln!("Invalid directory '{}'", wrkdir.display());
|
||||
@@ -185,7 +163,7 @@ fn main() {
|
||||
manager.set_filetransfer_params(address, port, protocol, username, password);
|
||||
}
|
||||
// Run
|
||||
let rc: i32 = manager.run(start_activity);
|
||||
manager.run(start_activity);
|
||||
// Then return
|
||||
std::process::exit(rc);
|
||||
std::process::exit(0);
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ use super::{Activity, Context};
|
||||
// Includes
|
||||
use crossterm::event::Event as InputEvent;
|
||||
use crossterm::event::{KeyCode, KeyEvent};
|
||||
use crossterm::terminal::{enable_raw_mode, disable_raw_mode};
|
||||
use tui::{
|
||||
backend::CrosstermBackend,
|
||||
layout::{Constraint, Direction, Layout},
|
||||
@@ -72,8 +73,8 @@ pub struct AuthActivity {
|
||||
pub protocol: ScpProtocol,
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
pub form_submit: bool, // becomes true after user has submitted fields
|
||||
pub esc_called: bool, // Becomes true if user has pressed esc
|
||||
pub submit: bool, // becomes true after user has submitted fields
|
||||
pub quit: bool, // Becomes true if user has pressed esc
|
||||
selected_field: InputField,
|
||||
}
|
||||
|
||||
@@ -88,8 +89,8 @@ impl AuthActivity {
|
||||
protocol: ScpProtocol::Sftp,
|
||||
username: String::new(),
|
||||
password: String::new(),
|
||||
form_submit: false,
|
||||
esc_called: false,
|
||||
submit: false,
|
||||
quit: false,
|
||||
selected_field: InputField::Address,
|
||||
}
|
||||
}
|
||||
@@ -101,7 +102,8 @@ impl Activity for AuthActivity {
|
||||
/// `on_create` is the function which must be called to initialize the activity.
|
||||
/// `on_create` must initialize all the data structures used by the activity
|
||||
fn on_create(&mut self, context: &mut Context) {
|
||||
// Mhm, nothing to do here I guess...
|
||||
// Put raw mode on enabled
|
||||
let _ = enable_raw_mode();
|
||||
}
|
||||
|
||||
/// ### on_draw
|
||||
@@ -118,7 +120,7 @@ impl Activity for AuthActivity {
|
||||
InputEvent::Key(key) => {
|
||||
match key.code {
|
||||
KeyCode::Esc => {
|
||||
self.esc_called = true;
|
||||
self.quit = true;
|
||||
break;
|
||||
}
|
||||
KeyCode::Enter => {
|
||||
@@ -152,7 +154,7 @@ impl Activity for AuthActivity {
|
||||
break;
|
||||
}
|
||||
// Everything OK, set enter
|
||||
self.form_submit = true;
|
||||
self.submit = true;
|
||||
popup = Some(format!("Connecting to {}:{}...", self.address, self.port));
|
||||
}
|
||||
KeyCode::Backspace => {
|
||||
@@ -233,7 +235,7 @@ impl Activity for AuthActivity {
|
||||
}
|
||||
}
|
||||
// draw interface
|
||||
context.terminal.draw(|f| {
|
||||
let _ = context.terminal.draw(|f| {
|
||||
let chunks = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.margin(2)
|
||||
@@ -273,6 +275,7 @@ impl Activity for AuthActivity {
|
||||
/// `on_destroy` is the function which cleans up runtime variables and data before terminating the activity.
|
||||
/// This function must be called once before terminating the activity.
|
||||
fn on_destroy(&mut self, context: &mut Context) {
|
||||
// Mhm, nothing to do here I guess...
|
||||
// Disable raw mode
|
||||
let _ = disable_raw_mode();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user