refactor: extract into a type alias

pull/12902/head
Nikita Revenco 2025-02-18 14:52:49 +00:00
parent 6dbb09f1fa
commit a9612dad1d
2 changed files with 13 additions and 8 deletions

View File

@ -37,6 +37,8 @@ use std::fs;
use std::path::Path;
use std::{error::Error, path::PathBuf};
use self::picker::PickerKeyHandler;
struct Utf8PathBuf {
path: String,
is_dir: bool,
@ -667,6 +669,8 @@ pub fn file_explorer(
)
};
type KeyHandler = PickerKeyHandler<(PathBuf, bool)>;
let picker = Picker::new(
columns,
0,
@ -689,11 +693,11 @@ pub fn file_explorer(
.with_cursor(cursor.unwrap_or_default())
.with_preview(|_editor, (path, _is_dir)| Some((path.as_path().into(), None)))
.with_key_handlers(hashmap! {
alt!('n') => Box::new(create_file) as Box<dyn Fn(&mut Context, &(PathBuf, bool), u32) + 'static>,
alt!('m') => Box::new(move_file) as Box<dyn Fn(&mut Context, &(PathBuf, bool), u32) + 'static>,
alt!('d') => Box::new(delete_file) as Box<dyn Fn(&mut Context, &(PathBuf, bool), u32) + 'static>,
alt!('c') => Box::new(copy_file) as Box<dyn Fn(&mut Context, &(PathBuf, bool), u32) + 'static>,
alt!('y') => Box::new(copy_path) as Box<dyn Fn(&mut Context, &(PathBuf, bool), u32) + 'static>
alt!('n') => Box::new(create_file) as KeyHandler,
alt!('m') => Box::new(move_file) as KeyHandler,
alt!('d') => Box::new(delete_file) as KeyHandler,
alt!('c') => Box::new(copy_file) as KeyHandler,
alt!('y') => Box::new(copy_path) as KeyHandler,
});
Ok(picker)

View File

@ -259,7 +259,7 @@ pub struct Picker<T: 'static + Send + Sync, D: 'static> {
widths: Vec<Constraint>,
callback_fn: PickerCallback<T>,
custom_key_handlers: PickerKeyHandler<T>,
custom_key_handlers: PickerKeyHandlers<T>,
pub truncate_start: bool,
/// Caches paths to documents
@ -395,7 +395,7 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
}
}
pub fn with_key_handlers(mut self, handlers: PickerKeyHandler<T>) -> Self {
pub fn with_key_handlers(mut self, handlers: PickerKeyHandlers<T>) -> Self {
self.custom_key_handlers = handlers;
self
}
@ -1185,4 +1185,5 @@ impl<T: 'static + Send + Sync, D> Drop for Picker<T, D> {
}
type PickerCallback<T> = Box<dyn Fn(&mut Context, &T, Action)>;
pub type PickerKeyHandler<T> = HashMap<KeyEvent, Box<dyn Fn(&mut Context, &T, u32) + 'static>>;
pub type PickerKeyHandler<T> = Box<dyn Fn(&mut Context, &T, u32) + 'static>;
pub type PickerKeyHandlers<T> = HashMap<KeyEvent, PickerKeyHandler<T>>;