Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle delete key in cmdline #132

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub enum Command {
Write(Option<String>),
WriteFrames(Option<String>),
WriteQuit,
SaveAs(String),
Quit,
QuitAll,
ForceQuit,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -287,6 +289,7 @@ impl From<Command> 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),
Expand Down Expand Up @@ -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()))
Expand Down Expand Up @@ -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())
}
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,11 @@ pub fn init<P: AsRef<Path>>(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()));
}
Expand Down
25 changes: 25 additions & 0 deletions src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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())));
}
Expand Down Expand Up @@ -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,
Expand Down