diff options
author | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-03-02 11:54:47 -0500 |
---|---|---|
committer | Jiayi Zhao <jeff.no.zhao@gmail.com> | 2020-03-02 11:54:47 -0500 |
commit | abec069c233b5bf6f582882851430b3a5949b22b (patch) | |
tree | 574fdbf185679ea13a2d6f8b9f5fcd603688d0e5 /src/commands | |
parent | 2ac415fe4f4035c8edbcdac33aee8e54a46099f7 (diff) |
add set_mode support
Diffstat (limited to 'src/commands')
-rw-r--r-- | src/commands/set_mode.rs | 67 |
1 files changed, 41 insertions, 26 deletions
diff --git a/src/commands/set_mode.rs b/src/commands/set_mode.rs index 94a6d23..b2c08df 100644 --- a/src/commands/set_mode.rs +++ b/src/commands/set_mode.rs @@ -29,27 +29,15 @@ impl SetMode { "set_mode" } - pub fn set_mode(&self, entry: &mut JoshutoDirEntry, initial: String) -> bool { - use std::os::unix::fs::PermissionsExt; - - const PROMPT: &str = ":set_mode "; - let user_input: Option<String> = None; - - match user_input { - Some(s) => { - let mut mode: u32 = 0; - for (i, ch) in s.chars().enumerate() { - if ch == LIBC_PERMISSION_VALS[i].1 { - let val: u32 = LIBC_PERMISSION_VALS[i].0 as u32; - mode |= val; - } - } - unix::set_mode(entry.file_path().as_path(), mode); - entry.metadata.permissions.set_mode(mode + (1 << 15)); - true + pub fn str_to_mode(s: &str) -> u32 { + let mut mode: u32 = 0; + for (i, ch) in s.chars().enumerate() { + if ch == LIBC_PERMISSION_VALS[i].1 { + let val: u32 = LIBC_PERMISSION_VALS[i].0 as u32; + mode |= val; } - None => false, } + mode } } @@ -64,17 +52,44 @@ impl std::fmt::Display for SetMode { impl JoshutoRunnable for SetMode { fn execute(&self, context: &mut JoshutoContext, backend: &mut TuiBackend) -> JoshutoResult<()> { use std::os::unix::fs::PermissionsExt; - let curr_tab = &mut context.tabs[context.curr_tab_index]; - if let Some(curr_list) = curr_tab.curr_list_mut() { - if let Some(file) = curr_list.get_curr_mut() { - let mode = file.metadata.permissions.mode(); - let mut mode_string = unix::stringify_mode(mode); - mode_string.remove(0); - self.set_mode(file, mode_string); + const PREFIX: &'static str = "set_mode "; + + let entry = context + .tabs[context.curr_tab_index] + .curr_list_ref() + .and_then(|x| x.get_curr_ref()); + + let user_input = match entry { + Some(entry) => { + let mode = entry.metadata.permissions.mode(); + let mode_string = unix::stringify_mode(mode); + let mut textfield = TuiTextField::default() + .prompt(":") + .prefix(PREFIX) + .suffix(&mode_string.as_str()[1..]); + textfield.get_input(backend, context) + } + None => None, + }; + + if let Some(s) = user_input { + if s.starts_with(PREFIX) { + let s = &s[PREFIX.len()..]; + let mode = Self::str_to_mode(s); + + let mut entry = context + .tabs[context.curr_tab_index] + .curr_list_mut() + .and_then(|x| x.get_curr_mut()) + .unwrap(); + + unix::set_mode(entry.file_path().as_path(), mode); + entry.metadata.permissions.set_mode(mode); CursorMoveDown::new(1).execute(context, backend)?; } } + Ok(()) } } |