diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-02-20 19:12:10 -0500 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2019-02-20 19:12:52 -0500 |
commit | c829e7849a44922cb55149f3874a82cda1040c89 (patch) | |
tree | e8e167eef9f0d5bb8d1e008c1bc2bb9936646718 | |
parent | 412b1ebb44382522084f615d58cb75374bc64778 (diff) |
change how preview works
-rw-r--r-- | src/commands/change_directory.rs | 2 | ||||
-rw-r--r-- | src/commands/cursor_move.rs | 2 | ||||
-rw-r--r-- | src/commands/open_file.rs | 2 | ||||
-rw-r--r-- | src/commands/parent_directory.rs | 2 | ||||
-rw-r--r-- | src/commands/reload_dir.rs | 2 | ||||
-rw-r--r-- | src/commands/rename_file.rs | 2 | ||||
-rw-r--r-- | src/preview.rs | 102 | ||||
-rw-r--r-- | src/run.rs | 3 |
8 files changed, 60 insertions, 57 deletions
diff --git a/src/commands/change_directory.rs b/src/commands/change_directory.rs index 87a23bb..4d2e722 100644 --- a/src/commands/change_directory.rs +++ b/src/commands/change_directory.rs @@ -89,7 +89,7 @@ impl std::fmt::Display for ChangeDirectory { impl JoshutoRunnable for ChangeDirectory { fn execute(&self, context: &mut JoshutoContext) { Self::change_directory(&self.path, context); - preview::preview_file(context); + preview::preview_file(&mut context.tabs[context.curr_tab_index], &context.views, &context.config_t); ncurses::doupdate(); } } diff --git a/src/commands/cursor_move.rs b/src/commands/cursor_move.rs index 04e6da4..c59fde6 100644 --- a/src/commands/cursor_move.rs +++ b/src/commands/cursor_move.rs @@ -46,8 +46,8 @@ impl CursorMove { &context.hostname, context.config_t.tilde_in_titlebar, ); + preview::preview_file(curr_tab, &context.views, &context.config_t); } - preview::preview_file(context); ncurses::doupdate(); } } diff --git a/src/commands/open_file.rs b/src/commands/open_file.rs index 7ff7e0a..507ad07 100644 --- a/src/commands/open_file.rs +++ b/src/commands/open_file.rs @@ -133,8 +133,8 @@ impl JoshutoRunnable for OpenFile { &context.username, &context.hostname, ); + preview::preview_file(curr_tab, &context.views, &context.config_t); } - preview::preview_file(context); ncurses::doupdate(); } else { let paths: Option<Vec<PathBuf>> = diff --git a/src/commands/parent_directory.rs b/src/commands/parent_directory.rs index 15d3556..82afe95 100644 --- a/src/commands/parent_directory.rs +++ b/src/commands/parent_directory.rs @@ -53,8 +53,8 @@ impl ParentDirectory { &context.username, &context.hostname, ); + preview::preview_file(curr_tab, &context.views, &context.config_t); } - preview::preview_file(context); } Err(e) => { ui::wprint_err(&context.views.bot_win, e.to_string().as_str()); diff --git a/src/commands/reload_dir.rs b/src/commands/reload_dir.rs index 20981bc..0eff65f 100644 --- a/src/commands/reload_dir.rs +++ b/src/commands/reload_dir.rs @@ -36,7 +36,7 @@ impl std::fmt::Display for ReloadDirList { impl JoshutoRunnable for ReloadDirList { fn execute(&self, context: &mut JoshutoContext) { Self::reload(context); - preview::preview_file(context); + preview::preview_file(&mut context.tabs[context.curr_tab_index], &context.views, &context.config_t); ncurses::doupdate(); } } diff --git a/src/commands/rename_file.rs b/src/commands/rename_file.rs index 9de918b..24b3e96 100644 --- a/src/commands/rename_file.rs +++ b/src/commands/rename_file.rs @@ -107,7 +107,7 @@ impl JoshutoRunnable for RenameFile { if let Some(file_name) = file_name { if let Some(path) = path { self.rename_file(&path, context, file_name); - preview::preview_file(context); + preview::preview_file(&mut context.tabs[context.curr_tab_index], &context.views, &context.config_t); ncurses::doupdate(); } } diff --git a/src/preview.rs b/src/preview.rs index 45b2fb0..6ea3ea3 100644 --- a/src/preview.rs +++ b/src/preview.rs @@ -1,69 +1,71 @@ use std::path::PathBuf; use std::process; -use crate::context::JoshutoContext; -use crate::window; +use crate::config::JoshutoConfig; +use crate::tab::JoshutoTab; +use crate::ui; +use crate::window::{panel::JoshutoPanel, view::JoshutoView}; -pub fn preview_file(context: &mut JoshutoContext) { - /* - let curr_tab = &mut context.tabs[context.tab_index]; - - if let Some(ref curr_list) = curr_tab.curr_list { - if let Some(entry) = curr_list.get_curr_ref() { - if entry.path.is_dir() { - if let Some(dirlist) = curr_tab.history.get_mut_or_create(&entry.path, &context.config_t.sort_type) { - context.views.right_win.display_contents(&context.theme_t, dirlist, context.config_t.scroll_offset); - context.views.right_win.queue_for_refresh(); - } else { - ncurses::werase(context.views.right_win.win); - ncurses::waddstr(context.views.right_win.win, "Can't find direntry"); - context.views.right_win.queue_for_refresh(); - } +pub fn preview_file(curr_tab: &mut JoshutoTab, views: &JoshutoView, config_t: &JoshutoConfig) { + if let Some(ref curr_list) = curr_tab.curr_list { + if let Some(entry) = curr_list.get_curr_ref() { + if entry.path.is_dir() { + if let Some(dirlist) = curr_tab.history.get_mut_or_create(&entry.path, &config_t.sort_type) { + views.right_win.display_contents(dirlist, config_t.scroll_offset); + views.right_win.queue_for_refresh(); } else { - ncurses::werase(context.views.right_win.win); + ui::wprint_err(&views.right_win, "Can't find directory"); + } + } else { + ncurses::werase(views.right_win.win); + ncurses::wnoutrefresh(views.right_win.win); + } +/* + else { + ncurses::werase(views.right_win.win); - 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()); - }, - } + 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()); + }, } } } } - - ncurses::wnoutrefresh(context.views.right_win.win); } - } else { - ncurses::werase(context.views.right_win.win); + ncurses::wnoutrefresh(context.views.right_win.win); } +*/ + } else { + ncurses::werase(views.right_win.win); + ncurses::wnoutrefresh(views.right_win.win); } - */ + } } -pub fn text_preview(win: &window::JoshutoPanel, path: &PathBuf) { +pub fn text_preview(win: &JoshutoPanel, path: &PathBuf) { let mut command = process::Command::new("head"); command.arg("-n"); command.arg(win.cols.to_string()); @@ -139,8 +139,8 @@ fn resize_handler(context: &mut JoshutoContext) { &context.username, &context.hostname, ); + preview::preview_file(curr_tab, &context.views, &context.config_t); } - preview::preview_file(context); ncurses::doupdate(); } @@ -149,6 +149,7 @@ pub fn run(config_t: config::JoshutoConfig, keymap_t: config::JoshutoKeymap) { let mut context = JoshutoContext::new(config_t); commands::NewTab::new_tab(&mut context); + preview::preview_file(&mut context.tabs[context.curr_tab_index], &context.views, &context.config_t); ncurses::doupdate(); while !context.exit { |