summaryrefslogtreecommitdiffstats
path: root/src/commands/delete_files.rs
diff options
context:
space:
mode:
authorJiayi Zhao <jeff.no.zhao@gmail.com>2020-02-22 19:54:41 -0500
committerJiayi Zhao <jeff.no.zhao@gmail.com>2020-02-22 19:54:41 -0500
commitd38dcdbbee44187bdb605dbf9bbf9c6c6d3e4f35 (patch)
tree91b059b99793fe250c67f66be3930a298117e0b3 /src/commands/delete_files.rs
parent03594099dafb4cda04e50f087df61cf76e2034d0 (diff)
implement textfield widget
- for asking users for long input strings - implement prompt widget - for prompting users for a single key response
Diffstat (limited to 'src/commands/delete_files.rs')
-rw-r--r--src/commands/delete_files.rs91
1 files changed, 27 insertions, 64 deletions
diff --git a/src/commands/delete_files.rs b/src/commands/delete_files.rs
index 2219283..78344d1 100644
--- a/src/commands/delete_files.rs
+++ b/src/commands/delete_files.rs
@@ -2,8 +2,7 @@ use std::fs;
use std::io::{self, Write};
use std::path;
-use termion::clear;
-use termion::cursor::Goto;
+use termion::event::Key;
use crate::commands::{JoshutoCommand, JoshutoRunnable, ReloadDirList};
use crate::context::JoshutoContext;
@@ -11,6 +10,8 @@ use crate::error::JoshutoResult;
use crate::ui::TuiBackend;
use crate::util::event::Event;
+use crate::ui::widgets::TuiPrompt;
+
#[derive(Clone, Debug)]
pub struct DeleteFiles;
@@ -36,84 +37,46 @@ impl DeleteFiles {
}
fn delete_files(context: &mut JoshutoContext, backend: &mut TuiBackend) -> std::io::Result<()> {
- let curr_tab = &mut context.tabs[context.curr_tab_index];
+ let curr_tab = &context.tabs[context.curr_tab_index];
let paths = match curr_tab.curr_list_ref() {
Some(s) => s.get_selected_paths(),
None => Vec::new(),
};
+ let paths_len = paths.len();
- if paths.is_empty() {
+ if paths_len == 0 {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
"no files selected",
));
}
- let frame = backend.terminal.get_frame();
- let f_size = frame.size();
-
- let termion_terminal = backend.terminal.backend_mut();
-
- write!(
- termion_terminal,
- "{}Delete {} files? (y/N){}",
- Goto(1, f_size.height),
- paths.len(),
- clear::AfterCursor
- );
-
- io::stdout().flush().ok();
-
- let mut ch = termion::event::Key::Char('n');
- while let Ok(evt) = context.events.next() {
- match evt {
- Event::Input(key) => {
- if key == termion::event::Key::Char('y')
- || key == termion::event::Key::Char('\n')
- {
- ch = termion::event::Key::Char('y');
- }
- break;
- }
- _ => {}
- }
- }
-
- if ch == termion::event::Key::Char('y') {
- if paths.len() > 1 {
- write!(
- termion_terminal,
- "{}Are you sure? (Y/n){}",
- Goto(1, f_size.height),
- clear::AfterCursor
- );
-
- io::stdout().flush().ok();
+ let ch = {
+ let prompt_str = format!("Delete {} files? (y/N)", paths_len);
+ let mut prompt = TuiPrompt::new(&prompt_str);
+ prompt.get_key(backend, &context)
+ };
- while let Ok(evt) = context.events.next() {
- match evt {
- Event::Input(key) => {
- ch = key;
- break;
- }
- _ => {}
- }
+ if ch == Key::Char('y') {
+ if paths_len > 1 {
+ let ch = {
+ 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') {
+ Self::remove_files(&paths)?;
+ ReloadDirList::reload(context.curr_tab_index, context)?;
+ let msg = format!("Deleted {} files", paths_len);
+ context.message_queue.push_back(msg);
}
} else {
- ch = termion::event::Key::Char('y');
+ Self::remove_files(&paths)?;
+ ReloadDirList::reload(context.curr_tab_index, context)?;
+ let msg = format!("Deleted {} files", paths_len);
+ context.message_queue.push_back(msg);
}
}
-
- if ch == termion::event::Key::Char('y') {
- Self::remove_files(&paths)?;
- ReloadDirList::reload(context.curr_tab_index, context)?;
- }
- write!(
- termion_terminal,
- "{}{}",
- Goto(1, f_size.height),
- clear::AfterCursor
- );
Ok(())
}
}