Working on text-editor

This commit is contained in:
ChristianVisintin 2020-12-19 21:04:55 +01:00
parent 6bc2bcb89e
commit dd9f54acae
5 changed files with 86 additions and 17 deletions

View file

@ -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*

30
Cargo.lock generated
View file

@ -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"

View file

@ -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" }

View file

@ -258,8 +258,6 @@ The developer documentation can be found on Rust Docs at <https://docs.rs/termsc
## Upcoming Features 🧪
- **Text editor**: possibility to open, read and write file both on remote and on local host; this will also support syntax highlighting.
---
## Contributions 🤝🏻

View file

@ -21,6 +21,8 @@
// Deps
extern crate bytesize;
extern crate content_inspector;
extern crate crossterm;
// Locals
use super::{FileTransferActivity, FsEntry, InputMode, LogLevel, PopupType};
@ -28,6 +30,8 @@ use crate::utils::fmt_millis;
// Ext
use bytesize::ByteSize;
use crossterm::terminal::{disable_raw_mode, enable_raw_mode};
use std::fs::{File, OpenOptions};
use std::io::{Read, Seek, Write};
use std::path::{Path, PathBuf};
use std::time::Instant;
@ -852,28 +856,62 @@ impl FileTransferActivity {
}
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),
));
);
}
}
}
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();
}
}