From 530f8ca31782ba0de37c6c2961f85c87a83dff4f Mon Sep 17 00:00:00 2001 From: sxyazi Date: Thu, 2 Nov 2023 01:58:07 +0800 Subject: [PATCH] WIP [no ci] --- yazi-core/src/completion/commands/trigger.rs | 37 +++++++++++++++++--- yazi-fm/src/completion/completion.rs | 10 ++++-- yazi-fm/src/executor.rs | 2 +- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/yazi-core/src/completion/commands/trigger.rs b/yazi-core/src/completion/commands/trigger.rs index 1b3be274..38fdb7ab 100644 --- a/yazi-core/src/completion/commands/trigger.rs +++ b/yazi-core/src/completion/commands/trigger.rs @@ -1,7 +1,8 @@ +use tokio::fs; use tracing::info; -use yazi_config::keymap::Exec; +use yazi_config::keymap::{Exec, KeymapLayer}; -use crate::completion::Completion; +use crate::{completion::Completion, emit}; pub struct Opt<'a> { word: &'a str, @@ -11,7 +12,7 @@ pub struct Opt<'a> { impl<'a> From<&'a Exec> for Opt<'a> { fn from(e: &'a Exec) -> Self { Self { - word: e.args.get(0).map(|w| w.as_str()).unwrap_or_default(), + word: e.args.first().map(|w| w.as_str()).unwrap_or_default(), ticket: e.named.get("ticket").and_then(|v| v.parse().ok()).unwrap_or(0), } } @@ -27,8 +28,34 @@ impl Completion { self.close(false); self.ticket = opt.ticket; - info!("trigger completion: {}", opt.word); - tokio::spawn(async move {}); + let word = opt.word.to_owned(); + let ticket = self.ticket; + tokio::spawn(async move { + let (parent, child) = word.rsplit_once('/').unwrap_or((".", word.as_str())); + + let mut dir = fs::read_dir(parent).await?; + let mut cands = Vec::new(); + while let Ok(Some(f)) = dir.next_entry().await { + let name = f.file_name().to_string_lossy().into_owned(); + if !name.starts_with(child) { + continue; + } + + cands.push(name); + if cands.len() >= 20 { + break; + } + } + + if !cands.is_empty() { + emit!(Call( + Exec::call("show", cands).with("ticket", ticket).vec(), + KeymapLayer::Completion + )); + } + + Ok::<(), anyhow::Error>(()) + }); false } } diff --git a/yazi-fm/src/completion/completion.rs b/yazi-fm/src/completion/completion.rs index 06958bec..b5cb1bd7 100644 --- a/yazi-fm/src/completion/completion.rs +++ b/yazi-fm/src/completion/completion.rs @@ -1,4 +1,4 @@ -use ratatui::{buffer::Buffer, layout::Rect, widgets::{Block, BorderType, Borders, Cell, Clear, Row, Table, Widget}}; +use ratatui::{buffer::Buffer, layout::Rect, widgets::{Block, BorderType, Borders, Cell, Clear, List, ListItem, Row, Table, Widget}}; use yazi_core::Ctx; pub(crate) struct Completion<'a> { @@ -11,7 +11,13 @@ impl<'a> Completion<'a> { impl<'a> Widget for Completion<'a> { fn render(self, _: Rect, buf: &mut Buffer) { - todo!() + let items = + self.cx.completion.items.iter().map(|x| ListItem::new(x.as_str())).collect::>(); + + // TODO + Clear.render(Rect { x: 10, y: 10, width: 10, height: 20 }, buf); + List::new(items).render(Rect { x: 10, y: 10, width: 10, height: 20 }, buf); + // let completion = &self.cx.input.completion; // let area = self.cx.area(&completion.position); diff --git a/yazi-fm/src/executor.rs b/yazi-fm/src/executor.rs index 0631dfd0..fa6fcc1e 100644 --- a/yazi-fm/src/executor.rs +++ b/yazi-fm/src/executor.rs @@ -286,7 +286,7 @@ impl Executor { "close" => cx.completion.close(exec.named.contains_key("submit")), "arrow" => { - let step: isize = exec.args.get(0).and_then(|s| s.parse().ok()).unwrap_or(0); + let step: isize = exec.args.first().and_then(|s| s.parse().ok()).unwrap_or(0); if step > 0 { cx.completion.next(step as usize) } else {