diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-02-22 19:54:41 -0500 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-02-22 19:54:41 -0500 |
commit | d38dcdbbee44187bdb605dbf9bbf9c6c6d3e4f35 (patch) | |
tree | 91b059b99793fe250c67f66be3930a298117e0b3 /src/commands | |
parent | 03594099dafb4cda04e50f087df61cf76e2034d0 (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')
-rw-r--r-- | src/commands/command_line.rs | 7 | ||||
-rw-r--r-- | src/commands/delete_files.rs | 91 | ||||
-rw-r--r-- | src/commands/rename_file.rs | 4 | ||||
-rw-r--r-- | src/commands/set_mode.rs | 2 |
4 files changed, 35 insertions, 69 deletions
diff --git a/src/commands/command_line.rs b/src/commands/command_line.rs index a00f442..f44c4a5 100644 --- a/src/commands/command_line.rs +++ b/src/commands/command_line.rs @@ -23,8 +23,11 @@ impl CommandLine { context: &mut JoshutoContext, backend: &mut TuiBackend, ) -> JoshutoResult<()> { - // let mut textfield = TuiTextField::new(backend, &context.events); - let user_input: Option<String> = None; // textfield.readline(); + let mut textfield = TuiTextField::default() + .prompt(":") + .prefix(self.prefix.as_str()) + .suffix(self.suffix.as_str()); + let user_input: Option<String> = textfield.get_input(backend, &context); if let Some(s) = user_input { let trimmed = s.trim_start(); 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(()) } } diff --git a/src/commands/rename_file.rs b/src/commands/rename_file.rs index dad9632..763a638 100644 --- a/src/commands/rename_file.rs +++ b/src/commands/rename_file.rs @@ -84,8 +84,8 @@ impl RenameFileAppend { let prefix; let suffix; if let Some(ext) = file_name.rfind('.') { - prefix = format!("rename {}", &file_name[0..ext]); - suffix = String::from(&file_name[ext..]); + prefix = format!("rename {}", &file_name[0..ext + 1]); + suffix = String::from(&file_name[ext + 1..]); } else { prefix = format!("rename {}", file_name); suffix = String::new(); diff --git a/src/commands/set_mode.rs b/src/commands/set_mode.rs index 8a974e9..94a6d23 100644 --- a/src/commands/set_mode.rs +++ b/src/commands/set_mode.rs @@ -4,7 +4,7 @@ use crate::error::JoshutoResult; use crate::fs::JoshutoDirEntry; use crate::ui::widgets::TuiTextField; use crate::ui::TuiBackend; -use crate::unix; +use crate::util::unix; #[derive(Clone, Debug)] pub struct SetMode; |