diff --git a/src/cmd.rs b/src/cmd.rs index 5a74f5e..8fb334f 100644 --- a/src/cmd.rs +++ b/src/cmd.rs @@ -53,6 +53,7 @@ pub enum Command { Write(Option), WriteFrames(Option), WriteQuit, + SaveAs(String), Quit, QuitAll, ForceQuit, @@ -210,7 +211,8 @@ impl fmt::Display for Command { Self::ViewPrev => write!(f, "Go to previous view"), Self::Write(None) => write!(f, "Write view to disk"), Self::Write(Some(_)) => write!(f, "Write view to disk as..."), - Self::WriteQuit => write!(f, "Write file to disk and quit"), + Self::WriteQuit => write!(f, "Write view to disk and quit"), + Self::SaveAs(_) => write!(f, "Change view name and write view to disk as..."), Self::Zoom(Op::Incr) => write!(f, "Zoom in view"), Self::Zoom(Op::Decr) => write!(f, "Zoom out view"), Self::Zoom(Op::Set(z)) => write!(f, "Set view zoom to {:.1}", z), @@ -287,6 +289,7 @@ impl From for String { Command::Write(None) => format!("w"), Command::Write(Some(path)) => format!("w {}", path), Command::WriteQuit => format!("wq"), + Command::SaveAs(path) => format!("saveas {}", path), Command::Zoom(Op::Incr) => format!("v/zoom +"), Command::Zoom(Op::Decr) => format!("v/zoom -"), Command::Zoom(Op::Set(z)) => format!("v/zoom {}", z), @@ -744,6 +747,9 @@ impl Default for Commands { .map(|(_, (scale, path))| Command::Export(scale, path)) }) .command("wq", "Write & quit view", |p| p.value(Command::WriteQuit)) + .command("saveas", "Write and change view name", |p| { + p.then(path()).map(|(_, path)| Command::SaveAs(path)) + }) .command("x", "Write & quit view", |p| p.value(Command::WriteQuit)) .command("w", "Write view", |p| { p.then(optional(path())) @@ -1086,6 +1092,9 @@ impl autocomplete::Completer for CommandCompleter { input, FileCompleterOpts { directories: true }, ), + Command::SaveAs(path) => { + self.complete_path(Some(path).as_ref(), input, Default::default()) + } Command::Source(path) | Command::Write(path) => { self.complete_path(path.as_ref(), input, Default::default()) } diff --git a/src/lib.rs b/src/lib.rs index 76e668b..b88a967 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -285,6 +285,11 @@ pub fn init>(paths: &[P], options: Options<'_>) -> std::io::Resul key: Some(platform::Key::Insert), state: platform::InputState::Pressed, modifiers: platform::ModifiersState { shift: true, .. }, + } + | platform::KeyboardInput { + key: Some(platform::Key::V), + state: platform::InputState::Pressed, + modifiers: platform::ModifiersState { ctrl: true, .. }, } => { session_events.push(Event::Paste(win.clipboard())); } diff --git a/src/session.rs b/src/session.rs index b59c479..141b2a3 100644 --- a/src/session.rs +++ b/src/session.rs @@ -2131,6 +2131,9 @@ impl Session { platform::Key::Backspace => { self.cmdline_handle_backspace(); } + platform::Key::Delete => { + self.cmdline_handle_delete(); + } platform::Key::Return => { self.cmdline_handle_enter(); } @@ -2774,6 +2777,19 @@ impl Session { Err(err) => self.message(format!("Error: {}", err), MessageType::Error), } } + Command::SaveAs(ref path) => { + match self.active_view_mut().save_as(&Path::new(path).into()) { + Ok(written) => { + self.message( + format!("\"{}\" {} pixels written", path, written), + MessageType::Info, + ); + self.active_view_mut().file_status = + FileStatus::Saved(FileStorage::Single(Path::new(path).into())); + } + Err(err) => self.message(format!("Error: {}", err), MessageType::Error), + } + } Command::WriteFrames(None) => { self.command(Command::WriteFrames(Some(".".to_owned()))); } @@ -3032,6 +3048,15 @@ impl Session { } } + fn cmdline_handle_delete(&mut self) { + self.cmdline.cursor_forward(); + self.cmdline.delc(); + + if self.cmdline.is_empty() { + self.cmdline_hide(); + } + } + fn cmdline_handle_enter(&mut self) { let input = self.cmdline.input(); // Always hide the command line before executing the command,