summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJiayi Zhao <jeff.no.zhao@gmail.com>2019-01-13 12:44:59 -0500
committerJiayi Zhao <jeff.no.zhao@gmail.com>2019-01-13 12:44:59 -0500
commit97884b4be589b900e300b13e797678edc9210c75 (patch)
tree0ce8c584f502b608e178eb6bee804086e48b05a5
parent280d2ff401e5b0fd9df0a2dfbe6ef3c138963caf (diff)
add support for previewing text files
-rw-r--r--Cargo.toml3
-rw-r--r--src/joshuto/command/open_file.rs1
-rw-r--r--src/joshuto/preview.rs59
3 files changed, 60 insertions, 3 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 28bc66a..9bdda7d 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -12,7 +12,8 @@ chrono = "0.4.6"
fs_extra = "1.1.0"
lazy_static = "1.2.0"
libc = "0.2.46"
-mime_guess = "1.8.6"
+mime = "0.3.13"
+mime-detective = "0.2.1"
serde = "1.0.84"
serde_derive = "1.0.84"
toml = "0.4.10"
diff --git a/src/joshuto/command/open_file.rs b/src/joshuto/command/open_file.rs
index dd05756..72dcc9e 100644
--- a/src/joshuto/command/open_file.rs
+++ b/src/joshuto/command/open_file.rs
@@ -1,6 +1,5 @@
extern crate fs_extra;
extern crate ncurses;
-extern crate mime_guess;
use std;
use std::env;
diff --git a/src/joshuto/preview.rs b/src/joshuto/preview.rs
index 5136949..6ec76d5 100644
--- a/src/joshuto/preview.rs
+++ b/src/joshuto/preview.rs
@@ -1,7 +1,13 @@
extern crate ncurses;
+extern crate mime_detective;
+extern crate mime;
+
+use std::path;
+use std::process;
use joshuto;
use joshuto::ui;
+use joshuto::window;
pub fn preview_file(context: &mut joshuto::JoshutoContext)
{
@@ -19,9 +25,60 @@ pub fn preview_file(context: &mut joshuto::JoshutoContext)
}
} else {
ncurses::werase(context.views.right_win.win);
- ncurses::waddstr(context.views.right_win.win, "Not a directory");
+
+ let detective = mime_detective::MimeDetective::new().unwrap();
+ match detective.detect_filepath(&entry.path) {
+ Ok(mime_type) => {
+ match mime_type.type_() {
+ mime::TEXT => {
+ text_preview(&context.views.right_win, &entry.path);
+ },
+ _ => {},
+ }
+ },
+ Err(e) => {
+ ncurses::waddstr(context.views.right_win.win, e.to_string().as_str());
+ },
+ }
ncurses::wnoutrefresh(context.views.right_win.win);
}
}
}
}
+
+pub fn text_preview(win: &window::JoshutoPanel, path: &path::PathBuf)
+{
+/*
+ let mut command = process::Command::new("bat");
+ command.arg("--terminal-width");
+ command.arg(win.cols.to_string());
+// command.arg("--wrap=never");
+ command.arg("line-range");
+ command.arg(format!("{}:{}", 0, win.rows));
+ command.arg("--style=numbers");
+ command.arg("--tabs");
+ command.arg("4");
+// command.arg("--color");
+// command.arg("always");
+ command.arg(path.as_os_str());
+ command.stdout(process::Stdio::piped());
+ // eprintln!("{:?}", command);
+
+*/
+ let mut command = process::Command::new("head");
+ command.arg("-n");
+ command.arg(win.cols.to_string());
+ command.arg(path.as_os_str());
+ command.stdout(process::Stdio::piped());
+
+ match command.output() {
+ Ok(s) => {
+ let output = String::from_utf8_lossy(&s.stdout);
+ ncurses::waddstr(win.win, &output);
+ },
+ Err(e) => {
+ ncurses::waddstr(win.win, e.to_string().as_str());
+ }
+ }
+ // bat joshuto.rs --terminal-width 20 --wrap=never --line-range 0:26 --style='numbers'
+}