chore: define core keybinds at config.rs

This commit is contained in:
ynqa
2025-03-25 05:00:38 +09:00
parent 5e45fc6c88
commit a09acb5bc0
4 changed files with 37 additions and 20 deletions

View File

@@ -152,13 +152,18 @@ impl Default for CompletionConfig {
}
}
#[derive(Serialize, Deserialize)]
// TODO: remove Clone derive
#[derive(Clone, Serialize, Deserialize)]
pub struct Keybinds {
pub exit: EventDefSet,
pub copy_query: EventDefSet,
pub copy_result: EventDefSet,
pub switch_mode: EventDefSet,
pub on_editor: EditorKeybinds,
pub on_completion: CompletionKeybinds,
}
#[derive(Serialize, Deserialize)]
#[derive(Clone, Serialize, Deserialize)]
pub struct EditorKeybinds {
pub backward: EventDefSet,
pub forward: EventDefSet,
@@ -173,7 +178,7 @@ pub struct EditorKeybinds {
pub completion: EventDefSet,
}
#[derive(Serialize, Deserialize)]
#[derive(Clone, Serialize, Deserialize)]
pub struct CompletionKeybinds {
pub up: EventDefSet,
pub down: EventDefSet,
@@ -182,6 +187,19 @@ pub struct CompletionKeybinds {
impl Default for Keybinds {
fn default() -> Self {
Self {
exit: EventDefSet::from(KeyEventDef::new(KeyCode::Char('c'), KeyModifiers::CONTROL)),
copy_query: EventDefSet::from(KeyEventDef::new(
KeyCode::Char('q'),
KeyModifiers::CONTROL,
)),
copy_result: EventDefSet::from(KeyEventDef::new(
KeyCode::Char('o'),
KeyModifiers::CONTROL,
)),
switch_mode: EventDefSet::from_iter([
EventDef::Key(KeyEventDef::new(KeyCode::Down, KeyModifiers::SHIFT)),
EventDef::Key(KeyEventDef::new(KeyCode::Up, KeyModifiers::SHIFT)),
]),
on_editor: EditorKeybinds {
backward: EventDefSet::from(KeyEventDef::new(KeyCode::Left, KeyModifiers::NONE)),
forward: EventDefSet::from(KeyEventDef::new(KeyCode::Right, KeyModifiers::NONE)),

View File

@@ -12,7 +12,7 @@ use crate::{
};
pub struct Editor {
keymap: Keymap,
handler: Handler,
state: text_editor::State,
focus_theme: EditorTheme,
defocus_theme: EditorTheme,
@@ -32,7 +32,7 @@ impl Editor {
completion_keybinds: CompletionKeybinds,
) -> Self {
Self {
keymap: BOXED_EDITOR_KEYMAP,
handler: BOXED_EDITOR_HANDLER,
state,
focus_theme,
defocus_theme,
@@ -60,7 +60,7 @@ impl Editor {
self.state.active_char_style = self.defocus_theme.active_char_style;
self.searcher.leave_search();
self.keymap = BOXED_EDITOR_KEYMAP;
self.handler = BOXED_EDITOR_HANDLER;
self.guide.text = Default::default();
}
@@ -82,20 +82,20 @@ impl Editor {
}
pub async fn operate(&mut self, event: &Event) -> anyhow::Result<()> {
(self.keymap)(event, self).await
(self.handler)(event, self).await
}
}
pub type Keymap = for<'a> fn(
pub type Handler = for<'a> fn(
&'a Event,
&'a mut Editor,
) -> Pin<Box<dyn Future<Output = anyhow::Result<()>> + Send + 'a>>;
const BOXED_EDITOR_KEYMAP: Keymap =
const BOXED_EDITOR_HANDLER: Handler =
|event, editor| -> Pin<Box<dyn Future<Output = anyhow::Result<()>> + Send + '_>> {
Box::pin(edit(event, editor))
};
const BOXED_SEARCHER_KEYMAP: Keymap =
const BOXED_SEARCHER_HANDLER: Handler =
|event, editor| -> Pin<Box<dyn Future<Output = anyhow::Result<()>> + Send + '_>> {
Box::pin(search(event, editor))
};
@@ -123,7 +123,7 @@ pub async fn edit<'a>(event: &'a Event, editor: &'a mut Editor) -> anyhow::Resul
editor.guide.style = StyleBuilder::new().fgc(Color::Green).build();
}
editor.state.texteditor.replace(&head);
editor.keymap = BOXED_SEARCHER_KEYMAP;
editor.handler = BOXED_SEARCHER_HANDLER;
}
None => {
editor.guide.text = format!("No suggestion found for '{}'", prefix);
@@ -233,7 +233,7 @@ pub async fn search<'a>(event: &'a Event, editor: &'a mut Editor) -> anyhow::Res
_ => {
editor.searcher.leave_search();
editor.keymap = BOXED_EDITOR_KEYMAP;
editor.handler = BOXED_EDITOR_HANDLER;
return edit(event, editor).await;
}
}

View File

@@ -218,8 +218,9 @@ async fn main() -> anyhow::Result<()> {
searcher,
config.editor.theme_on_focus,
config.editor.theme_on_defocus,
config.keybinds.on_editor,
config.keybinds.on_completion,
// TODO: remove clones
config.keybinds.on_editor.clone(),
config.keybinds.on_completion.clone(),
);
prompt::run(
@@ -231,6 +232,7 @@ async fn main() -> anyhow::Result<()> {
editor,
loading_suggestions_task,
args.no_hint,
config.keybinds,
)
.await?;

View File

@@ -17,6 +17,7 @@ use tokio::{
};
use crate::{
config::{event::Matcher, Keybinds},
Context, ContextMonitor, Editor, PaneIndex, Processor, Renderer, SearchProvider,
SpinnerSpawner, ViewInitializer, ViewProvider, Visualizer, EMPTY_PANE,
};
@@ -87,6 +88,7 @@ pub async fn run<T: ViewProvider + SearchProvider>(
editor: Editor,
loading_suggestions_task: JoinHandle<anyhow::Result<()>>,
no_hint: bool,
keybinds: Keybinds,
) -> anyhow::Result<()> {
enable_raw_mode()?;
execute!(io::stdout(), cursor::Hide)?;
@@ -146,12 +148,7 @@ pub async fn run<T: ViewProvider + SearchProvider>(
Event::Resize(width, height) => {
debounce_resize_tx.send((width, height)).await?;
},
Event::Key(KeyEvent {
code: KeyCode::Char('c'),
modifiers: KeyModifiers::CONTROL,
kind: KeyEventKind::Press,
state: KeyEventState::NONE,
}) => {
event if keybinds.exit.matches(&event) => {
break 'main
},
Event::Key(KeyEvent {