summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJiayi Zhao <jeff.no.zhao@gmail.com>2019-01-04 17:23:15 -0500
committerJiayi Zhao <jeff.no.zhao@gmail.com>2019-01-04 17:23:15 -0500
commit84f72f16ba0799e6226d8575032db87e4591b07e (patch)
tree5c70f1e6e7c2b39d466e016d10888dc60337dbee
parenta9886b2c4fd08a35b614b17171097924af67dcd5 (diff)
clean up code
- prevent errors from breaking the ui - move functionality in unix.rs into open_file.rs - comment out unused code
-rw-r--r--src/joshuto/command/cursor_move.rs81
-rw-r--r--src/joshuto/command/open_file.rs40
-rw-r--r--src/joshuto/ui.rs14
-rw-r--r--src/joshuto/unix.rs65
4 files changed, 112 insertions, 88 deletions
diff --git a/src/joshuto/command/cursor_move.rs b/src/joshuto/command/cursor_move.rs
index ad10e8e..e2f6050 100644
--- a/src/joshuto/command/cursor_move.rs
+++ b/src/joshuto/command/cursor_move.rs
@@ -60,16 +60,21 @@ impl command::Runnable for CursorMove {
if new_path.is_dir() {
match context.history.pop_or_create(new_path.as_path(),
&context.config_t.sort_type) {
- Ok(s) => context.preview_list = Some(s),
- Err(e) => eprintln!("{}", e),
+ Ok(s) => {
+ context.preview_list = Some(s);
+ ui::redraw_view(&context.views.right_win, context.preview_list.as_ref());
+ },
+ Err(e) => ui::wprint_err(&context.views.right_win, format!("{}", e).as_str()),
}
+ } else {
+ ncurses::werase(context.views.right_win.win);
+ ncurses::wnoutrefresh(context.views.right_win.win);
}
},
None => {},
}
ui::redraw_view(&context.views.mid_win, context.curr_list.as_ref());
- ui::redraw_view(&context.views.right_win, context.preview_list.as_ref());
ui::redraw_status(&context.views, context.curr_list.as_ref(), &context.curr_path,
&context.config_t.username, &context.config_t.hostname);
@@ -121,20 +126,26 @@ impl command::Runnable for CursorMovePageUp {
curr_list.index = new_index;
let curr_index = curr_list.index as usize;
+ let new_path = &curr_list.contents[curr_index].path;
- if curr_list.contents[curr_index].path.is_dir() {
- match context.history.pop_or_create(&curr_list.contents[curr_index].path,
+ if new_path.is_dir() {
+ match context.history.pop_or_create(new_path.as_path(),
&context.config_t.sort_type) {
- Ok(s) => context.preview_list = Some(s),
- Err(e) => eprintln!("{}", e),
+ Ok(s) => {
+ context.preview_list = Some(s);
+ ui::redraw_view(&context.views.right_win, context.preview_list.as_ref());
+ },
+ Err(e) => ui::wprint_err(&context.views.right_win, format!("{}", e).as_str()),
}
+ } else {
+ ncurses::werase(context.views.right_win.win);
+ ncurses::wnoutrefresh(context.views.right_win.win);
}
},
None => {},
}
ui::redraw_view(&context.views.mid_win, context.curr_list.as_ref());
- ui::redraw_view(&context.views.right_win, context.preview_list.as_ref());
ui::redraw_status(&context.views, context.curr_list.as_ref(), &context.curr_path,
&context.config_t.username, &context.config_t.hostname);
@@ -164,19 +175,19 @@ impl command::Runnable for CursorMovePageDown {
{
match context.curr_list {
Some(ref mut curr_list) => {
- let curr_index = curr_list.index as usize;
+ let curr_index = curr_list.index;
let dir_len = curr_list.contents.len();
- if curr_index >= dir_len - 1 {
+ if curr_index >= dir_len as i32 - 1 {
return;
}
let half_page = context.views.mid_win.cols as usize / 2;
- let new_index = if curr_index as usize + half_page >= dir_len {
- (dir_len - 1)
+ let new_index = if curr_index + half_page as i32 >= dir_len as i32 {
+ (dir_len - 1) as i32
} else {
- curr_index as usize + half_page
+ curr_index + half_page as i32
};
let dir_list = context.preview_list.take();
@@ -184,22 +195,28 @@ impl command::Runnable for CursorMovePageDown {
context.history.insert(s);
}
- curr_list.index = new_index as i32;
- let new_path = &curr_list.contents[new_index].path;
+ curr_list.index = new_index;
+ let curr_index = curr_list.index as usize;
+ let new_path = &curr_list.contents[curr_index].path;
if new_path.is_dir() {
match context.history.pop_or_create(new_path.as_path(),
&context.config_t.sort_type) {
- Ok(s) => context.preview_list = Some(s),
- Err(e) => eprintln!("{}", e),
+ Ok(s) => {
+ context.preview_list = Some(s);
+ ui::redraw_view(&context.views.right_win, context.preview_list.as_ref());
+ },
+ Err(e) => ui::wprint_err(&context.views.right_win, format!("{}", e).as_str()),
}
+ } else {
+ ncurses::werase(context.views.right_win.win);
+ ncurses::wnoutrefresh(context.views.right_win.win);
}
},
None => {},
}
ui::redraw_view(&context.views.mid_win, context.curr_list.as_ref());
- ui::redraw_view(&context.views.right_win, context.preview_list.as_ref());
ui::redraw_status(&context.views, context.curr_list.as_ref(), &context.curr_path,
&context.config_t.username, &context.config_t.hostname);
@@ -245,16 +262,21 @@ impl command::Runnable for CursorMoveHome {
if new_path.is_dir() {
match context.history.pop_or_create(new_path.as_path(),
&context.config_t.sort_type) {
- Ok(s) => context.preview_list = Some(s),
- Err(e) => eprintln!("{}", e),
+ Ok(s) => {
+ context.preview_list = Some(s);
+ ui::redraw_view(&context.views.right_win, context.preview_list.as_ref());
+ },
+ Err(e) => ui::wprint_err(&context.views.right_win, format!("{}", e).as_str()),
}
+ } else {
+ ncurses::werase(context.views.right_win.win);
+ ncurses::wnoutrefresh(context.views.right_win.win);
}
},
None => {},
}
ui::redraw_view(&context.views.mid_win, context.curr_list.as_ref());
- ui::redraw_view(&context.views.right_win, context.preview_list.as_ref());
ui::redraw_status(&context.views, context.curr_list.as_ref(), &context.curr_path,
&context.config_t.username, &context.config_t.hostname);
@@ -297,20 +319,27 @@ impl command::Runnable for CursorMoveEnd {
}
curr_list.index = dir_len as i32 - 1;
+ let curr_index = curr_list.index as usize;
+ let new_path = &curr_list.contents[curr_index].path;
- if curr_list.contents[curr_list.index as usize].path.is_dir() {
- match context.history.pop_or_create(&curr_list.contents[curr_list.index as usize].path,
+ if new_path.is_dir() {
+ match context.history.pop_or_create(new_path.as_path(),
&context.config_t.sort_type) {
- Ok(s) => context.preview_list = Some(s),
- Err(e) => eprintln!("{}", e),
+ Ok(s) => {
+ context.preview_list = Some(s);
+ ui::redraw_view(&context.views.right_win, context.preview_list.as_ref());
+ },
+ Err(e) => ui::wprint_err(&context.views.right_win, format!("{}", e).as_str()),
}
+ } else {
+ ncurses::werase(context.views.right_win.win);
+ ncurses::wnoutrefresh(context.views.right_win.win);
}
},
None => {},
}
ui::redraw_view(&context.views.mid_win, context.curr_list.as_ref());
- ui::redraw_view(&context.views.right_win, context.preview_list.as_ref());
ui::redraw_status(&context.views, context.curr_list.as_ref(), &context.curr_path,
&context.config_t.username, &context.config_t.hostname);
diff --git a/src/joshuto/command/open_file.rs b/src/joshuto/command/open_file.rs
index 627bf49..261eb5b 100644
--- a/src/joshuto/command/open_file.rs
+++ b/src/joshuto/command/open_file.rs
@@ -49,7 +49,45 @@ impl command::Runnable for OpenFile {
}
if path.is_file() {
- unix::open_file(&context.mimetype_t, &context.views.bot_win, path.as_path());
+ let file_ext: Option<&str> = match path.extension() {
+ Some(s) => s.to_str(),
+ None => None,
+ };
+
+ let mimetype: Option<&str> = match file_ext {
+ Some(extstr) => mime_guess::get_mime_type_str(extstr),
+ None => None,
+ };
+
+ let empty_vec: Vec<Vec<String>> = Vec::new();
+ let mimetype_options: &Vec<Vec<String>> = match mimetype {
+ Some(mimetype) => {
+ match context.mimetype_t.mimetypes.get(mimetype) {
+ Some(s) => s,
+ None => &empty_vec,
+ }
+ },
+ None => {
+ &empty_vec
+ }
+ };
+
+ if mimetype_options.len() > 0 {
+ ncurses::savetty();
+ ncurses::endwin();
+ unix::open_with(path.as_path(), &mimetype_options[0]);
+ ncurses::resetty();
+ ncurses::refresh();
+ } else {
+ match mimetype {
+ Some(s) => ui::wprint_err(&context.views.bot_win,
+ format!("Don't know how to open: {}", s).as_str()),
+ None => ui::wprint_err(&context.views.bot_win,
+ "Uh oh, mime_guess says unknown file type :("),
+ };
+ }
+ ncurses::doupdate();
+
} else if path.is_dir() {
match env::set_current_dir(&path) {
Ok(_) => {},
diff --git a/src/joshuto/ui.rs b/src/joshuto/ui.rs
index 45575b9..e054eac 100644
--- a/src/joshuto/ui.rs
+++ b/src/joshuto/ui.rs
@@ -17,6 +17,7 @@ pub const IMG_COLOR : i16 = 12;
pub const VID_COLOR : i16 = 13;
pub const SELECT_COLOR : i16 = 25;
pub const ERR_COLOR : i16 = 40;
+pub const EMPTY_COLOR : i16 = 50;
pub fn init_ncurses()
{
@@ -50,6 +51,8 @@ pub fn init_ncurses()
ncurses::init_pair(SELECT_COLOR, ncurses::COLOR_YELLOW, -1);
/* error message */
ncurses::init_pair(ERR_COLOR, ncurses::COLOR_RED, -1);
+ /* empty */
+ ncurses::init_pair(EMPTY_COLOR, ncurses::COLOR_WHITE, ncurses::COLOR_RED);
ncurses::printw("Loading...");
@@ -79,6 +82,15 @@ pub fn wprint_err(win: &window::JoshutoPanel, msg : &str)
ncurses::wnoutrefresh(win.win);
}
+pub fn wprint_empty(win: &window::JoshutoPanel, msg : &str)
+{
+ ncurses::werase(win.win);
+ ncurses::wattron(win.win, ncurses::COLOR_PAIR(EMPTY_COLOR));
+ ncurses::mvwaddstr(win.win, 0, 0, msg);
+ ncurses::wattroff(win.win, ncurses::COLOR_PAIR(EMPTY_COLOR));
+ ncurses::wnoutrefresh(win.win);
+}
+
pub fn wprint_path(win: &window::JoshutoPanel, username: &str,
hostname: &str, path: &path::PathBuf, file_name: &str)
{
@@ -255,7 +267,7 @@ pub fn display_contents(win: &window::JoshutoPanel,
let dir_contents = &entry.contents;
let vec_len = dir_contents.len();
if vec_len == 0 {
- wprint_err(win, "empty");
+ wprint_empty(win, "empty");
return;
}
diff --git a/src/joshuto/unix.rs b/src/joshuto/unix.rs
index 486ede8..ea8b4dd 100644
--- a/src/joshuto/unix.rs
+++ b/src/joshuto/unix.rs
@@ -1,15 +1,9 @@
extern crate libc;
-extern crate toml;
extern crate ncurses;
-use std::fs;
use std::path;
use std::process;
-use joshuto::ui;
-use joshuto::mimetype;
-use joshuto::window;
-
pub const BITMASK : u32 = 0o170000;
pub const S_IFSOCK : u32 = 0o140000; /* socket */
pub const S_IFLNK : u32 = 0o120000; /* symbolic link */
@@ -19,9 +13,12 @@ pub const S_IFDIR : u32 = 0o040000; /* directory */
pub const S_IFCHR : u32 = 0o020000; /* character device */
pub const S_IFIFO : u32 = 0o010000; /* FIFO */
-pub fn is_reg(mode: u32) -> bool { mode & BITMASK == S_IFREG }
-
/*
+pub const fn is_reg(mode: u32) -> bool
+{
+ mode >> 9 & S_IFREG >> 9 == mode >> 9
+}
+
pub fn get_unix_filetype(mode : u32) -> &'static str
{
match mode & BITMASK {
@@ -98,58 +95,6 @@ pub fn stringify_mode(mode: u32) -> String
mode_str
}
-pub fn open_file(mimetype_t: &mimetype::JoshutoMimetype,
- win: &window::JoshutoPanel, path: &path::Path) {
- use std::os::unix::fs::PermissionsExt;
-
- if let Ok(metadata) = fs::metadata(path) {
- let permissions : fs::Permissions = metadata.permissions();
- let mode = permissions.mode();
- if !is_reg(mode) {
- ui::wprint_err(win, "Failed to read metadata, unable to determine filetype");
- ncurses::doupdate();
- return;
- }
-
- let file_ext: Option<&str> = match path.extension() {
- Some(s) => s.to_str(),
- None => None,
- };
-
- let mimetype: Option<&str> = match file_ext {
- Some(extstr) => mime_guess::get_mime_type_str(extstr),
- None => None,
- };
-
- let empty_vec: Vec<Vec<String>> = Vec::new();
- let mimetype_options: &Vec<Vec<String>> = match mimetype {
- Some(mimetype) => {
- match mimetype_t.mimetypes.get(mimetype) {
- Some(s) => s,
- None => &empty_vec,
- }
- },
- None => {
- &empty_vec
- }
- };
-
- if mimetype_options.len() > 0 {
- ncurses::savetty();
- ncurses::endwin();
- open_with(path, &mimetype_options[0]);
- ncurses::resetty();
- ncurses::refresh();
- } else {
- match mimetype {
- Some(s) => ui::wprint_err(win, format!("Don't know how to open: {}", s).as_str()),
- None => ui::wprint_err(win, "Uh oh, mime_guess says unknown file type :("),
- };
- }
- ncurses::doupdate();
- }
-}
-
pub fn open_with(path: &path::Path, args: &Vec<String>)
{
let program = args[0].clone();