summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJiayi Zhao <jeff.no.zhao@gmail.com>2020-03-01 15:01:21 -0500
committerJiayi Zhao <jeff.no.zhao@gmail.com>2020-03-01 15:01:21 -0500
commit6b84901e50bfc0052fd93daed4b5b742db96447d (patch)
tree125d8a90ac2129f845eeb437682c644697bd8f16
parent49efef05db72fcff88a8986c0ccb65e7eef016f1 (diff)
add support for showing mimetype options with open_with
-rw-r--r--src/commands/delete_files.rs8
-rw-r--r--src/commands/open_file.rs78
-rw-r--r--src/ui/widgets/tui_textfield.rs4
3 files changed, 58 insertions, 32 deletions
diff --git a/src/commands/delete_files.rs b/src/commands/delete_files.rs
index 78344d1..d89a17d 100644
--- a/src/commands/delete_files.rs
+++ b/src/commands/delete_files.rs
@@ -52,19 +52,19 @@ impl DeleteFiles {
}
let ch = {
- let prompt_str = format!("Delete {} files? (y/N)", paths_len);
+ let prompt_str = format!("Delete {} files? (Y/n)", paths_len);
let mut prompt = TuiPrompt::new(&prompt_str);
prompt.get_key(backend, &context)
};
- if ch == Key::Char('y') {
+ if ch == Key::Char('y') || ch == Key::Char('\n') {
if paths_len > 1 {
let ch = {
- let prompt_str = "Are you sure? (Y/n)";
+ let prompt_str = "Are you sure? (y/N)";
let mut prompt = TuiPrompt::new(prompt_str);
prompt.get_key(backend, &context)
};
- if ch == Key::Char('y') || ch == Key::Char('\n') {
+ if ch == Key::Char('y') {
Self::remove_files(&paths)?;
ReloadDirList::reload(context.curr_tab_index, context)?;
let msg = format!("Deleted {} files", paths_len);
diff --git a/src/commands/open_file.rs b/src/commands/open_file.rs
index 2a900e0..405a286 100644
--- a/src/commands/open_file.rs
+++ b/src/commands/open_file.rs
@@ -5,7 +5,7 @@ use crate::config::mimetype::JoshutoMimetypeEntry;
use crate::context::JoshutoContext;
use crate::error::{JoshutoError, JoshutoErrorKind, JoshutoResult};
use crate::history::DirectoryHistory;
-use crate::ui::widgets::TuiTextField;
+use crate::ui::widgets::{TuiMenu, TuiTextField};
use crate::ui::TuiBackend;
use crate::util::load_child::LoadChild;
@@ -92,42 +92,68 @@ impl OpenFileWith {
}
pub fn open_with(context: &JoshutoContext, backend: &mut TuiBackend, paths: &[&PathBuf]) -> std::io::Result<()> {
+ const PROMPT: &'static str = "open_with ";
+
let mimetype_options: Vec<&JoshutoMimetypeEntry> = OpenFile::get_options(&paths[0]);
- let mut textfield = TuiTextField::default()
- .prompt(":")
- .prefix("open_with ");
- let user_input: Option<String> = textfield.get_input(backend, &context);
+ let user_input: Option<String> = {
+ let menu_options: Vec<String> = mimetype_options
+ .iter()
+ .enumerate()
+ .map(|(i, e)| format!(" {} | {}", i, e))
+ .collect();
+ let menu_options_str: Vec<&str> = menu_options
+ .iter()
+ .map(|e| e.as_str())
+ .collect();
+ let mut menu_widget = TuiMenu::new(&menu_options_str);
+
+ let mut textfield = TuiTextField::default()
+ .prompt(":")
+ .prefix(PROMPT)
+ .menu(&mut menu_widget);
+ textfield.get_input(backend, &context)
+ };
match user_input.as_ref() {
- None => Ok(()),
- Some(user_input) => match user_input.parse::<usize>() {
- Ok(n) if n >= mimetype_options.len() => Err(std::io::Error::new(
- std::io::ErrorKind::InvalidData,
- "option does not exist".to_owned(),
- )),
- Ok(n) => {
- backend.terminal_drop();
- let res = mimetype_options[n].execute_with(paths);
- backend.terminal_restore()?;
- res
- }
- Err(_) => {
- let mut args_iter = user_input.split_whitespace();
- args_iter.next();
- match args_iter.next() {
- Some(cmd) => {
+ Some(user_input) if user_input.starts_with(PROMPT) => {
+ let user_input = &user_input[PROMPT.len()..];
+
+ match user_input.parse::<usize>() {
+ Ok(n) if n >= mimetype_options.len() => Err(std::io::Error::new(
+ std::io::ErrorKind::InvalidData,
+ "option does not exist".to_owned(),
+ )),
+ Ok(n) => {
+
+ let mimetype_entry = &mimetype_options[n];
+ if mimetype_entry.get_fork() {
+ mimetype_entry.execute_with(paths)
+ } else {
backend.terminal_drop();
- let res = JoshutoMimetypeEntry::new(String::from(cmd))
- .args(args_iter)
- .execute_with(paths);
+ let res = mimetype_entry.execute_with(paths);
backend.terminal_restore()?;
res
}
- None => Ok(()),
+ }
+ Err(_) => {
+ let mut args_iter = user_input.split_whitespace();
+ args_iter.next();
+ match args_iter.next() {
+ Some(cmd) => {
+ backend.terminal_drop();
+ let res = JoshutoMimetypeEntry::new(String::from(cmd))
+ .args(args_iter)
+ .execute_with(paths);
+ backend.terminal_restore()?;
+ res
+ }
+ None => Ok(()),
+ }
}
}
}
+ _ => Ok(()),
}
}
}
diff --git a/src/ui/widgets/tui_textfield.rs b/src/ui/widgets/tui_textfield.rs
index 6d89b14..f1e9cc7 100644
--- a/src/ui/widgets/tui_textfield.rs
+++ b/src/ui/widgets/tui_textfield.rs
@@ -109,10 +109,10 @@ impl<'a> TuiTextField<'a> {
if let Some(menu) = self._menu.as_mut() {
let menu_len = menu.len();
- let menu_y = if menu_len + 1 > f_size.height as usize {
+ let menu_y = if menu_len + 2 > f_size.height as usize {
0
} else {
- (f_size.height as usize - menu_len - 1) as u16
+ (f_size.height as usize - menu_len - 2) as u16
};
let rect = Rect {