From dd9f54acae7c91a7c365b06b36e5c0d50aeccf98 Mon Sep 17 00:00:00 2001 From: ChristianVisintin Date: Sat, 19 Dec 2020 21:04:55 +0100 Subject: [PATCH] Working on text-editor --- CHANGELOG.md | 3 + Cargo.lock | 30 +++++++++ Cargo.toml | 6 +- README.md | 2 - .../filetransfer_activity/session.rs | 62 +++++++++++++++---- 5 files changed, 86 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd82346..e62bdb4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,9 @@ Released on ?? - Linux: `/home/alice/.config/termscp/bookmarks.toml` - Windows: `C:\Users\Alice\AppData\Roaming\termscp\bookmarks.toml` - MacOS: `/Users/Alice/Library/Application Support/termscp/bookmarks.toml` +- **Text Editor** + - Added text editor feature to explorer view + - Added `o` to keybindings to open a text file - Enhancements: - User interface - Collpased borders to make everything more *aesthetic* diff --git a/Cargo.lock b/Cargo.lock index b35d2c7..da1657c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -196,6 +196,15 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" +[[package]] +name = "content_inspector" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7bda66e858c683005a53a9a60c69a4aca7eeaa45d124526e389f7aec8e62f38" +dependencies = [ + "memchr", +] + [[package]] name = "core-foundation" version = "0.9.1" @@ -318,6 +327,16 @@ dependencies = [ "winapi", ] +[[package]] +name = "edit" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "323032447eba6f5aca88b46d6e7815151c16c53e4128569420c09d7840db3bfc" +dependencies = [ + "tempfile", + "which", +] + [[package]] name = "foreign-types" version = "0.3.2" @@ -981,8 +1000,10 @@ version = "0.2.0" dependencies = [ "bytesize", "chrono", + "content_inspector", "crossterm", "dirs", + "edit", "ftp4", "getopts", "hostname", @@ -1187,6 +1208,15 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "which" +version = "3.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d011071ae14a2f6671d0b74080ae0cd8ebf3a6f8c9589a2cd45f23126fe29724" +dependencies = [ + "libc", +] + [[package]] name = "whoami" version = "1.0.0" diff --git a/Cargo.toml b/Cargo.toml index 73a944f..e33b1c0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,8 +17,10 @@ readme = "README.md" [dependencies] bytesize = "1.0.1" chrono = "0.4.19" +content_inspector = "0.2.4" crossterm = "0.18.2" dirs = "3.0.1" +edit = "0.1.2" ftp4 = { version = "^4.0.1", features = ["secure"] } getopts = "0.2.21" hostname = "0.3.1" @@ -29,6 +31,7 @@ regex = "1.4.2" rpassword = "5.0.0" serde = { version = "1.0.118", features = ["derive"] } ssh2 = "0.9.0" +tempfile = "3.1.0" textwrap = "0.13.0" toml = "0.5.7" tui = { version = "0.13.0", features = ["crossterm"], default-features = false } @@ -38,9 +41,6 @@ whoami = "1.0.0" [target.'cfg(any(target_os = "unix", target_os = "macos", target_os = "linux"))'.dependencies] users = "0.11.0" -[dev-dependencies] -tempfile = "3" - #[patch.crates-io] #ftp = { git = "https://github.com/ChristianVisintin/rust-ftp" } diff --git a/README.md b/README.md index f4d4ce4..1ed4a63 100644 --- a/README.md +++ b/README.md @@ -258,8 +258,6 @@ The developer documentation can be found on Rust Docs at { // Report err - self.log( + self.log_and_alert( LogLevel::Error, - format!("Could not change working directory: {}", err).as_str(), - ); - self.input_mode = InputMode::Popup(PopupType::Alert( - Color::Red, format!("Could not change working directory: {}", err), - )); + ); } } } Err(err) => { // Report err - self.log( + self.log_and_alert( LogLevel::Error, - format!("Could not change working directory: {}", err).as_str(), - ); - self.input_mode = InputMode::Popup(PopupType::Alert( - Color::Red, format!("Could not change working directory: {}", err), - )); + ); } } } + + /// ### edit_file + /// + /// Edit a file on localhost + pub(super) fn edit_file(&mut self, path: &Path) { + // Read first 2048 bytes or less from file to check if it is textual + match OpenOptions::new().read(true).open(path) { + Ok(mut f) => { + // Read + let mut buff: [u8; 2048] = [0; 2048]; + match f.read(&mut buff) { + Ok(_) => { + if content_inspector::inspect(&buff).is_binary() { + self.log_and_alert( + LogLevel::Error, + format!("Could not open file in editor: file is binary"), + ); + } + } + Err(err) => { + self.log_and_alert( + LogLevel::Error, + format!("Could not read file: {}", err), + ); + return; + } + } + } + Err(err) => { + self.log_and_alert(LogLevel::Error, format!("Could not read file: {}", err)); + return; + } + } + // Put input mode back to normal + let _ = disable_raw_mode(); + // Open editor + if let Err(err) = edit::edit_file(path) { + self.log_and_alert(LogLevel::Error, format!("Could not open editor: {}", err)); + } + // Re-enable raw mode + let _ = enable_raw_mode(); + } }