WIP [no ci]

This commit is contained in:
sxyazi 2023-11-02 01:58:07 +08:00
parent 208baffddd
commit 530f8ca317
No known key found for this signature in database
3 changed files with 41 additions and 8 deletions

View file

@ -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
}
}

View file

@ -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::<Vec<_>>();
// 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);

View file

@ -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 {