summaryrefslogtreecommitdiffstats
path: root/src/preview.rs
diff options
context:
space:
mode:
authorJiayi Zhao <jeff.no.zhao@gmail.com>2019-04-25 08:41:39 -0400
committerJiayi Zhao <jeff.no.zhao@gmail.com>2019-04-25 08:42:09 -0400
commite411fc0be674ff01b614802a1e706071593c6647 (patch)
treeaf6175466c9c739266539b45fae5d1b264522e78 /src/preview.rs
parentd41770f76a9d19c9df7fd44e971c636eb1c23e1e (diff)
rework how previewing works
- should be more extensible now
Diffstat (limited to 'src/preview.rs')
-rw-r--r--src/preview.rs175
1 files changed, 71 insertions, 104 deletions
diff --git a/src/preview.rs b/src/preview.rs
index 48c045a..c207463 100644
--- a/src/preview.rs
+++ b/src/preview.rs
@@ -1,128 +1,90 @@
-use std::collections::hash_map::Entry;
-
-// use std::process;
+use std::collections::{hash_map::Entry, HashMap};
+use std::ffi::OsStr;
+use std::io::BufRead;
+use std::path;
+use std::process;
use crate::config::JoshutoConfig;
use crate::structs::{JoshutoDirEntry, JoshutoDirList};
use crate::tab::JoshutoTab;
+use crate::ui;
use crate::window::panel::JoshutoPanel;
+use crate::MIMETYPE_T;
pub fn preview_parent(curr_tab: &mut JoshutoTab, win: &JoshutoPanel, config_t: &JoshutoConfig) {
if let Some(path) = curr_tab.curr_path.parent() {
- match curr_tab
- .history
- .entry(path.clone().to_path_buf())
- {
- Entry::Occupied(mut entry) => {
- win.display_contents(entry.get_mut(), config_t.scroll_offset);
- win.queue_for_refresh();
- }
- Entry::Vacant(entry) => {
- if let Ok(s) = JoshutoDirList::new(path.to_path_buf(), &config_t.sort_option) {
- win.display_contents(entry.insert(s), config_t.scroll_offset);
- win.queue_for_refresh();
- }
- }
- }
+ preview_directory(&mut curr_tab.history, path, win, config_t);
}
}
-pub fn preview_entry(curr_tab: &mut JoshutoTab, win: &JoshutoPanel, config_t: &JoshutoConfig)-> Result<(), std::io::Error> {
+pub fn preview_entry(curr_tab: &mut JoshutoTab, win: &JoshutoPanel, config_t: &JoshutoConfig) {
+ ncurses::werase(win.win);
match curr_tab.curr_list.get_curr_ref() {
Some(s) => {
if s.path.is_dir() {
- match curr_tab
- .history
- .entry(s.path.clone().to_path_buf())
- {
- Entry::Occupied(mut entry) => {
- win.display_contents(entry.get_mut(), config_t.scroll_offset);
- win.queue_for_refresh();
- }
- Entry::Vacant(entry) => {
- if let Ok(s) = JoshutoDirList::new(s.path.to_path_buf(), &config_t.sort_option) {
- win.display_contents(entry.insert(s), config_t.scroll_offset);
- win.queue_for_refresh();
- }
- }
- }
+ preview_directory(&mut curr_tab.history, s.path.as_path(), win, config_t);
+ } else if s.metadata.len <= config_t.max_preview_size {
+ preview_file(s.path.as_path(), win);
} else {
- ncurses::werase(win.win);
- win.queue_for_refresh();
+ ui::wprint_err(win, "File size exceeds max preview size");
}
}
- None => {},
+ None => {}
};
- Ok(())
+ win.queue_for_refresh();
}
-/*
-pub fn preview_file_old(curr_tab: &mut JoshutoTab, views: &JoshutoView, config_t: &JoshutoConfig) {
- if let Some(entry) = curr_tab.curr_list.get_curr_ref() {
- if entry.path.is_dir() {
- match curr_tab
- .history
- .get_mut_or_create(&entry.path, &config_t.sort_option)
- {
- Ok(dirlist) => {
- views
- .right_win
- .display_contents(dirlist, config_t.scroll_offset);
- views.right_win.queue_for_refresh();
- }
- Err(e) => {
- ui::wprint_err(&views.right_win, &e.to_string());
- }
+fn preview_directory(
+ history: &mut HashMap<path::PathBuf, JoshutoDirList>,
+ path: &path::Path,
+ win: &JoshutoPanel,
+ config_t: &JoshutoConfig,
+) {
+ match history.entry(path.clone().to_path_buf()) {
+ Entry::Occupied(mut entry) => {
+ win.display_contents(entry.get_mut(), config_t.scroll_offset);
+ }
+ Entry::Vacant(entry) => {
+ if let Ok(s) = JoshutoDirList::new(path.clone().to_path_buf(), &config_t.sort_option) {
+ win.display_contents(entry.insert(s), config_t.scroll_offset);
}
- } else {
- ncurses::werase(views.right_win.win);
- ncurses::wnoutrefresh(views.right_win.win);
}
- else {
- ncurses::werase(views.right_win.win);
+ }
+ win.queue_for_refresh();
+}
- if let Some(file_ext) = entry.path.extension() {
- if let Some(file_ext) = file_ext.to_str() {
- match file_ext {
- "o" | "a" | "avi" | "mp3" | "mp4" | "wmv" | "wma" |
- "mkv" | "flv" | "vob" | "wav" | "mpc" | "flac" |
- "divx" | "xcf" | "pdf" | "torrent" | "class" | "so" |
- "img" | "pyc" | "dmg" | "png" | "jpg" | "jpeg" | "out" | "svg" => {
- ui::wprint_err(&context.views.right_win, "Binary File");
- },
- _ => {
- 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);
- },
- _ => {
- ui::wprint_err(&context.views.right_win, mime_type.type_().as_str());
- },
- }
- },
- Err(e) => {
- ui::wprint_err(&context.views.right_win, e.to_string().as_str());
- },
- }
- }
- }
- }
- }
+fn preview_file(path: &path::Path, win: &JoshutoPanel) {
+ match path.extension() {
+ Some(file_ext) => {
+ let mimetype_str = tree_magic::from_filepath(&path);
+ /* mime subtype have second priority */
+ if let Some(s) = MIMETYPE_T.mimetype.get(&mimetype_str) {}
+
+ /* generic mime type have last priority */
+ if let Some(s) = mimetype_str.find('/') {
+ let mimetype_type = &mimetype_str[..s];
+ if mimetype_type == "text" {
+ preview_text(path, win);
+ }
+ }
+ }
+ None => {
+ let mimetype_str = tree_magic::from_filepath(&path);
+ /* mime subtype have second priority */
+ if let Some(s) = MIMETYPE_T.mimetype.get(&mimetype_str) {}
- ncurses::wnoutrefresh(context.views.right_win.win);
+ /* generic mime type have last priority */
+ if let Some(s) = mimetype_str.find('/') {
+ let mimetype_type = &mimetype_str[..s];
+ if mimetype_type == "text" {
+ preview_text(path, win);
}
- } else {
- ncurses::werase(views.right_win.win);
- ncurses::wnoutrefresh(views.right_win.win);
+ }
+ }
}
}
-*/
-/*
-pub fn text_preview(win: &JoshutoPanel, path: &PathBuf) {
+fn preview_text(path: &path::Path, win: &JoshutoPanel) {
let mut command = process::Command::new("head");
command.arg("-n");
command.arg(win.cols.to_string());
@@ -134,16 +96,21 @@ pub fn text_preview(win: &JoshutoPanel, path: &PathBuf) {
match command.spawn() {
Ok(child) => {
if let Some(output) = child.stdout {
- let mut reader = std::io::BufReader::new(output);
- let mut buffer = String::new();
+ let reader = std::io::BufReader::new(output);
- // reader.read_line(&mut buffer);
+ let mut i = 0;
+ for line in reader.lines() {
+ if let Ok(line) = line {
+ ncurses::mvwaddnstr(win.win, i, 0, &line, win.cols);
+ i += 1;
+ }
+ if i == win.rows {
+ break;
+ }
+ }
}
}
- Err(e) => {
- ncurses::waddstr(win.win, e.to_string().as_str());
- }
+ Err(e) => ui::wprint_err(win, e.to_string().as_str()),
}
// bat joshuto.rs --terminal-width 20 --wrap=never --line-range 0:26 --style='numbers'
}
-*/