Skip to content

Commit

Permalink
add support for showing mimetype options with open_with
Browse files Browse the repository at this point in the history
  • Loading branch information
kamiyaa committed Mar 1, 2020
1 parent 49efef0 commit 6b84901
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 32 deletions.
8 changes: 4 additions & 4 deletions src/commands/delete_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
78 changes: 52 additions & 26 deletions src/commands/open_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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(()),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/ui/widgets/tui_textfield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 6b84901

Please sign in to comment.