mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
feat: interactive cd completion
This commit is contained in:
parent
6a31ed14ce
commit
03fd890f10
7 changed files with 167 additions and 63 deletions
|
|
@ -5,20 +5,24 @@ pub struct Completion {
|
||||||
items: Vec<String>,
|
items: Vec<String>,
|
||||||
cursor: usize,
|
cursor: usize,
|
||||||
|
|
||||||
pub position: Position,
|
pub position: Position,
|
||||||
pub visible: bool,
|
pub column_cnt: u8,
|
||||||
|
pub max_width: u16,
|
||||||
|
pub visible: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Completion {
|
impl Completion {
|
||||||
pub fn show(&mut self, opt: CompletionOpt) {
|
pub fn show(&mut self, opt: CompletionOpt) {
|
||||||
self.close(false);
|
self.close();
|
||||||
self.visible = true;
|
self.visible = true;
|
||||||
|
|
||||||
self.items = opt.items;
|
self.items = opt.items;
|
||||||
self.position = opt.position;
|
self.position = opt.position;
|
||||||
|
self.column_cnt = opt.column_cnt;
|
||||||
|
self.max_width = opt.max_width;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn close(&mut self, submit: bool) -> bool {
|
pub fn close(&mut self) -> bool {
|
||||||
self.cursor = 0;
|
self.cursor = 0;
|
||||||
self.visible = false;
|
self.visible = false;
|
||||||
true
|
true
|
||||||
|
|
@ -33,8 +37,6 @@ impl Completion {
|
||||||
let old = self.cursor;
|
let old = self.cursor;
|
||||||
self.cursor = (self.cursor + step).min(len - 1);
|
self.cursor = (self.cursor + step).min(len - 1);
|
||||||
|
|
||||||
eprintln!("Cur: {}", self.items.get(self.cursor).unwrap());
|
|
||||||
|
|
||||||
old != self.cursor
|
old != self.cursor
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -47,5 +49,7 @@ impl Completion {
|
||||||
|
|
||||||
pub fn list(&self) -> Vec<String> { self.items.clone() }
|
pub fn list(&self) -> Vec<String> { self.items.clone() }
|
||||||
|
|
||||||
pub fn selected_cursor(&self) -> usize { self.cursor }
|
pub fn cursor(&self) -> usize { self.cursor }
|
||||||
|
|
||||||
|
pub fn get_selection(&self) -> Option<String> { self.items.get(self.cursor).cloned() }
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,28 +3,34 @@ use ratatui::layout::Rect;
|
||||||
use crate::Position;
|
use crate::Position;
|
||||||
|
|
||||||
pub struct CompletionOpt {
|
pub struct CompletionOpt {
|
||||||
pub items: Vec<String>,
|
pub items: Vec<String>,
|
||||||
pub position: Position,
|
pub position: Position,
|
||||||
|
pub column_cnt: u8,
|
||||||
|
pub max_width: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CompletionOpt {
|
impl CompletionOpt {
|
||||||
pub fn top() -> Self {
|
pub fn top() -> Self {
|
||||||
Self {
|
Self {
|
||||||
items: vec![],
|
items: vec![],
|
||||||
position: Position::Top(
|
position: Position::Top(
|
||||||
// TODO: hardcode
|
// TODO: hardcode
|
||||||
Rect { x: 0, y: 5, width: 100, height: 10 },
|
Rect { x: 0, y: 5, width: 80, height: 10 },
|
||||||
),
|
),
|
||||||
|
column_cnt: 4,
|
||||||
|
max_width: 20,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn hover() -> Self {
|
pub fn hover() -> Self {
|
||||||
Self {
|
Self {
|
||||||
items: vec![],
|
items: vec![],
|
||||||
position: Position::Hovered(
|
position: Position::Hovered(
|
||||||
// TODO: hardcode
|
// TODO: hardcode
|
||||||
Rect { x: 0, y: 3, width: 40, height: 4 },
|
Rect { x: 0, y: 3, width: 80, height: 10 },
|
||||||
),
|
),
|
||||||
|
column_cnt: 4,
|
||||||
|
max_width: 20,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,23 +1,47 @@
|
||||||
|
use crossterm::event::KeyCode;
|
||||||
|
use yazi_config::keymap::Key;
|
||||||
|
|
||||||
use crate::{completion::CompletionOpt, input::Input};
|
use crate::{completion::CompletionOpt, input::Input};
|
||||||
|
|
||||||
impl Input {
|
impl Input {
|
||||||
pub fn complete(&mut self) -> bool {
|
pub fn complete(&mut self) -> bool {
|
||||||
if !self.completion.visible {
|
if !self.completion.visible {
|
||||||
eprintln!("Completing 1: {}", self.completion_callback.is_some());
|
let current = self.snaps.current().value.as_str();
|
||||||
let current = self.snaps.current().value.clone();
|
let result = self.init_completion.as_ref().map(|f| f(current)).unwrap_or_default();
|
||||||
let result = self.completion_callback.as_ref().map(|f| f(current)).unwrap_or_default();
|
|
||||||
eprintln!("Completing {:?}", result);
|
|
||||||
match result.len() {
|
match result.len() {
|
||||||
0 => false,
|
0 => false,
|
||||||
1 => self.type_str(result.get(0).unwrap()),
|
1 => {
|
||||||
|
if let Some(f) = self.finish_completion.as_ref() {
|
||||||
|
self.replace_str(f(current, result.get(0).unwrap()).as_str())
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
_ => {
|
_ => {
|
||||||
self.completion.show(CompletionOpt::top().with_items(result));
|
self.completion.show(CompletionOpt::top().with_items(result));
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
eprintln!("Completing @");
|
false
|
||||||
self.completion.next(1)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn navigate_completion(&mut self, key: &Key) -> bool {
|
||||||
|
match key.code {
|
||||||
|
KeyCode::Up => self.completion.prev(self.completion.column_cnt as usize),
|
||||||
|
KeyCode::Down => self.completion.next(self.completion.column_cnt as usize),
|
||||||
|
KeyCode::Left => self.completion.prev(1),
|
||||||
|
KeyCode::Right | KeyCode::Tab => self.completion.next(1),
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn finish_completion(&mut self) -> bool {
|
||||||
|
if let (Some(val), Some(f)) = (self.completion.get_selection(), &self.finish_completion) {
|
||||||
|
let final_val = f(self.snaps.current().value.as_str(), val.as_str());
|
||||||
|
self.replace_str(final_val.as_str());
|
||||||
|
}
|
||||||
|
self.completion.close()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,8 +24,9 @@ pub struct Input {
|
||||||
// Shell
|
// Shell
|
||||||
pub(super) highlight: bool,
|
pub(super) highlight: bool,
|
||||||
|
|
||||||
pub completion: Completion,
|
pub completion: Completion,
|
||||||
pub(super) completion_callback: Option<Box<dyn Fn(String) -> Vec<String> + Send>>,
|
pub(super) init_completion: Option<Box<dyn Fn(&str) -> Vec<String> + Send>>,
|
||||||
|
pub(super) finish_completion: Option<Box<dyn Fn(&str, &str) -> String + Send>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Input {
|
impl Input {
|
||||||
|
|
@ -44,7 +45,8 @@ impl Input {
|
||||||
// Shell
|
// Shell
|
||||||
self.highlight = opt.highlight;
|
self.highlight = opt.highlight;
|
||||||
|
|
||||||
self.completion_callback = opt.completion_callback
|
self.init_completion = opt.init_completion;
|
||||||
|
self.finish_completion = opt.finish_completion;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn close(&mut self, submit: bool) -> bool {
|
pub fn close(&mut self, submit: bool) -> bool {
|
||||||
|
|
@ -54,7 +56,7 @@ impl Input {
|
||||||
}
|
}
|
||||||
|
|
||||||
self.visible = false;
|
self.visible = false;
|
||||||
self.completion.close(false);
|
self.completion.close();
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -195,10 +197,28 @@ impl Input {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
eprintln!("Type key {:?}", key);
|
if self.completion.visible {
|
||||||
match key {
|
match key {
|
||||||
Key { code: KeyCode::Tab, shift: false, ctrl: false, alt: false } => return self.complete(),
|
Key {
|
||||||
_ => (),
|
code: KeyCode::Tab | KeyCode::Up | KeyCode::Down | KeyCode::Left | KeyCode::Right,
|
||||||
|
shift: false,
|
||||||
|
ctrl: false,
|
||||||
|
alt: false,
|
||||||
|
} => return self.navigate_completion(key),
|
||||||
|
Key { code: KeyCode::Enter, shift: false, ctrl: false, alt: false } => {
|
||||||
|
return self.finish_completion();
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
self.completion.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
match key {
|
||||||
|
Key { code: KeyCode::Tab, shift: false, ctrl: false, alt: false } => {
|
||||||
|
return self.complete();
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(c) = key.plain() {
|
if let Some(c) = key.plain() {
|
||||||
|
|
@ -229,6 +249,13 @@ impl Input {
|
||||||
self.move_(s.chars().count() as isize)
|
self.move_(s.chars().count() as isize)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn replace_str(&mut self, s: &str) -> bool {
|
||||||
|
let snap = self.snaps.current_mut();
|
||||||
|
snap.value = s.to_string();
|
||||||
|
self.flush_value();
|
||||||
|
self.move_(s.chars().count() as isize)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn backspace(&mut self) -> bool {
|
pub fn backspace(&mut self) -> bool {
|
||||||
let snap = self.snaps.current_mut();
|
let snap = self.snaps.current_mut();
|
||||||
if snap.cursor < 1 {
|
if snap.cursor < 1 {
|
||||||
|
|
|
||||||
|
|
@ -3,40 +3,43 @@ use ratatui::prelude::Rect;
|
||||||
use crate::Position;
|
use crate::Position;
|
||||||
|
|
||||||
pub struct InputOpt {
|
pub struct InputOpt {
|
||||||
pub title: String,
|
pub title: String,
|
||||||
pub value: String,
|
pub value: String,
|
||||||
pub position: Position,
|
pub position: Position,
|
||||||
pub realtime: bool,
|
pub realtime: bool,
|
||||||
pub highlight: bool,
|
pub highlight: bool,
|
||||||
pub completion_callback: Option<Box<dyn Fn(String) -> Vec<String> + Send>>,
|
pub init_completion: Option<Box<dyn Fn(&str) -> Vec<String> + Send>>,
|
||||||
|
pub finish_completion: Option<Box<dyn Fn(&str, &str) -> String + Send>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl InputOpt {
|
impl InputOpt {
|
||||||
pub fn top(title: impl AsRef<str>) -> Self {
|
pub fn top(title: impl AsRef<str>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
title: title.as_ref().to_owned(),
|
title: title.as_ref().to_owned(),
|
||||||
value: String::new(),
|
value: String::new(),
|
||||||
position: Position::Top(
|
position: Position::Top(
|
||||||
// TODO: hardcode
|
// TODO: hardcode
|
||||||
Rect { x: 0, y: 2, width: 50, height: 3 },
|
Rect { x: 0, y: 2, width: 50, height: 3 },
|
||||||
),
|
),
|
||||||
realtime: false,
|
realtime: false,
|
||||||
highlight: false,
|
highlight: false,
|
||||||
completion_callback: None,
|
init_completion: None,
|
||||||
|
finish_completion: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn hovered(title: impl AsRef<str>) -> Self {
|
pub fn hovered(title: impl AsRef<str>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
title: title.as_ref().to_owned(),
|
title: title.as_ref().to_owned(),
|
||||||
value: String::new(),
|
value: String::new(),
|
||||||
position: Position::Hovered(
|
position: Position::Hovered(
|
||||||
// TODO: hardcode
|
// TODO: hardcode
|
||||||
Rect { x: 0, y: 1, width: 50, height: 3 },
|
Rect { x: 0, y: 1, width: 50, height: 3 },
|
||||||
),
|
),
|
||||||
realtime: false,
|
realtime: false,
|
||||||
highlight: false,
|
highlight: false,
|
||||||
completion_callback: None,
|
init_completion: None,
|
||||||
|
finish_completion: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -59,11 +62,16 @@ impl InputOpt {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn with_completion<F: Fn(String) -> Vec<String> + Send + 'static>(
|
pub fn with_completion<
|
||||||
|
F1: Fn(&str) -> Vec<String> + Send + 'static,
|
||||||
|
F2: Fn(&str, &str) -> String + Send + 'static,
|
||||||
|
>(
|
||||||
mut self,
|
mut self,
|
||||||
callback: F,
|
init_completion: F1,
|
||||||
|
finish_completion: F2,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
self.completion_callback = Some(Box::new(callback));
|
self.init_completion = Some(Box::new(init_completion));
|
||||||
|
self.finish_completion = Some(Box::new(finish_completion));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -60,9 +60,44 @@ impl Tab {
|
||||||
pub fn cd_interactive(&mut self, target: Url) -> bool {
|
pub fn cd_interactive(&mut self, target: Url) -> bool {
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
let mut result = emit!(Input(
|
let mut result = emit!(Input(
|
||||||
InputOpt::top("Change directory:")
|
InputOpt::top("Change directory:").with_value(target.to_string_lossy()).with_completion(
|
||||||
.with_value(target.to_string_lossy())
|
|prefix| {
|
||||||
.with_completion(|s| vec![s, "Hi".to_string()])
|
let mut cmp_prefix = prefix;
|
||||||
|
if prefix.contains('/') {
|
||||||
|
let (old_prefix, old_file_prefix) = prefix.rsplit_once('/').unwrap();
|
||||||
|
cmp_prefix = old_file_prefix;
|
||||||
|
std::fs::read_dir(old_prefix.to_string() + "/")
|
||||||
|
} else {
|
||||||
|
std::fs::read_dir(".")
|
||||||
|
}
|
||||||
|
.ok()
|
||||||
|
.map(|list| {
|
||||||
|
list
|
||||||
|
.filter_map(|file| {
|
||||||
|
file
|
||||||
|
.ok()
|
||||||
|
.map(|f| {
|
||||||
|
let name = f.file_name().to_string_lossy().to_string();
|
||||||
|
if f.metadata().is_ok_and(|m| m.is_dir()) && name.starts_with(&cmp_prefix) {
|
||||||
|
Some(name)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.flatten()
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
})
|
||||||
|
.unwrap_or_default()
|
||||||
|
},
|
||||||
|
|current, new| {
|
||||||
|
if let Some((prefix, _)) = current.rsplit_once("/") {
|
||||||
|
format!("{prefix}/{new}/")
|
||||||
|
} else {
|
||||||
|
format!("{new}/")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
));
|
));
|
||||||
|
|
||||||
if let Some(Ok(s)) = result.recv().await {
|
if let Some(Ok(s)) = result.recv().await {
|
||||||
|
|
|
||||||
|
|
@ -17,26 +17,26 @@ impl<'a> Widget for Completion<'a> {
|
||||||
let completion = &self.cx.input.completion;
|
let completion = &self.cx.input.completion;
|
||||||
let area = self.cx.area(&completion.position);
|
let area = self.cx.area(&completion.position);
|
||||||
|
|
||||||
const COLUMN_CNT: usize = 4;
|
let constraint = (0..completion.column_cnt)
|
||||||
const MAX_WIDTH: usize = 20;
|
.map(|_| Constraint::Percentage(completion.max_width))
|
||||||
let constraint =
|
.collect::<Vec<Constraint>>();
|
||||||
(0..COLUMN_CNT).map(|_| Constraint::Ratio(1, MAX_WIDTH as u32)).collect::<Vec<Constraint>>();
|
|
||||||
let table = {
|
let table = {
|
||||||
|
let max_width = completion.max_width as usize;
|
||||||
let mut table = vec![];
|
let mut table = vec![];
|
||||||
let mut cur_row = vec![];
|
let mut cur_row = vec![];
|
||||||
for (idx, s) in completion.list().into_iter().enumerate() {
|
for (idx, s) in completion.list().into_iter().enumerate() {
|
||||||
if idx != 0 && idx % COLUMN_CNT == 0 {
|
if idx != 0 && idx % completion.column_cnt as usize == 0 {
|
||||||
let t = mem::take(&mut cur_row);
|
let t = mem::take(&mut cur_row);
|
||||||
table.push(Row::new(t));
|
table.push(Row::new(t));
|
||||||
}
|
}
|
||||||
// todo
|
|
||||||
cur_row.push(
|
cur_row.push(
|
||||||
Cell::from(if s.len() < MAX_WIDTH {
|
Cell::from(if s.len() < max_width {
|
||||||
s
|
s
|
||||||
} else {
|
} else {
|
||||||
s.split_at(MAX_WIDTH - 1).0.to_string() + "…"
|
s.split_at(max_width - 1).0.to_string() + "…"
|
||||||
})
|
})
|
||||||
.style(if completion.selected_cursor() == idx {
|
// todo
|
||||||
|
.style(if completion.cursor() == idx {
|
||||||
THEME.select.active.into()
|
THEME.select.active.into()
|
||||||
} else {
|
} else {
|
||||||
THEME.select.inactive.into()
|
THEME.select.inactive.into()
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue