WIP(completion): code skeleton

This commit is contained in:
XOR-op 2023-10-26 16:05:30 -04:00
parent 2b731748e4
commit 0b85e1fc95
12 changed files with 183 additions and 19 deletions

View file

@ -0,0 +1,47 @@
use crate::{completion::CompletionOpt, Position};
#[derive(Default)]
pub struct Completion {
items: Vec<String>,
cursor: usize,
pub position: Position,
pub visible: bool,
}
impl Completion {
pub fn show(&mut self, opt: CompletionOpt) {
self.close(false);
self.visible = true;
self.items = opt.items;
self.position = opt.position;
}
pub fn close(&mut self, submit: bool) -> bool {
self.cursor = 0;
self.visible = false;
true
}
pub fn next(&mut self, step: usize) -> bool {
let len = self.items.len();
if len == 0 {
return false;
}
let old = self.cursor;
self.cursor = (self.cursor + step).min(len - 1);
old != self.cursor
}
pub fn prev(&mut self, step: usize) -> bool {
let old = self.cursor;
self.cursor = self.cursor.saturating_sub(step);
old != self.cursor
}
pub fn list(&self) -> Vec<String> { self.items.clone() }
}

View file

@ -0,0 +1,5 @@
mod completion;
mod option;
pub(super) use completion::Completion;
pub use option::CompletionOpt;

View file

@ -0,0 +1,26 @@
use ratatui::layout::Rect;
use crate::Position;
pub struct CompletionOpt {
pub items: Vec<String>,
pub position: Position,
}
impl CompletionOpt {
pub fn hovered() -> Self {
Self {
items: vec![],
position: Position::Hovered(
// TODO: hardcode
Rect { x: 0, y: 1, width: 50, height: 3 },
),
}
}
#[inline]
pub fn with_items(mut self, items: Vec<String>) -> Self {
self.items = items;
self
}
}

View file

@ -0,0 +1,20 @@
use crate::{completion::CompletionOpt, input::Input};
impl Input {
pub fn complete(&mut self) -> bool {
if !self.completion.visible {
let current = self.snaps.current().value.clone();
let result = self.completion_callback.as_ref().map(|f| f(current)).unwrap_or_default();
match result.len() {
0 => false,
1 => self.type_str(result.get(0).unwrap()),
_ => {
self.completion.show(CompletionOpt::hovered().with_items(result));
false
}
}
} else {
self.completion.next(1)
}
}
}

View file

@ -7,12 +7,12 @@ use yazi_config::keymap::Key;
use yazi_shared::{CharKind, InputError};
use super::{mode::InputMode, op::InputOp, InputOpt, InputSnap, InputSnaps};
use crate::{external, Position};
use crate::{completion::Completion, external, Position};
#[derive(Default)]
pub struct Input {
snaps: InputSnaps,
pub visible: bool,
pub(super) snaps: InputSnaps,
pub visible: bool,
title: String,
pub position: Position,
@ -23,6 +23,9 @@ pub struct Input {
// Shell
pub(super) highlight: bool,
pub completion: Completion,
pub(super) completion_callback: Option<Box<dyn Fn(String) -> Vec<String> + Send>>,
}
impl Input {
@ -189,6 +192,11 @@ impl Input {
return false;
}
match key {
Key { code: KeyCode::Tab, shift: false, ctrl: false, alt: false } => return self.complete(),
_ => (),
}
if let Some(c) = key.plain() {
return self.type_char(c);
}

View file

@ -1,3 +1,4 @@
mod completion;
mod input;
mod mode;
mod op;

View file

@ -3,34 +3,40 @@ use ratatui::prelude::Rect;
use crate::Position;
pub struct InputOpt {
pub title: String,
pub value: String,
pub position: Position,
pub realtime: bool,
pub highlight: bool,
pub title: String,
pub value: String,
pub position: Position,
pub realtime: bool,
pub highlight: bool,
pub completion_callback: Option<Box<dyn Fn(String) -> Vec<String> + Send>>,
}
impl InputOpt {
pub fn top(title: impl AsRef<str>) -> Self {
Self {
title: title.as_ref().to_owned(),
value: String::new(),
position: Position::Top(/* TODO: hardcode */ Rect { x: 0, y: 2, width: 50, height: 3 }),
realtime: false,
highlight: false,
title: title.as_ref().to_owned(),
value: String::new(),
position: Position::Top(
// TODO: hardcode
Rect { x: 0, y: 2, width: 50, height: 3 },
),
realtime: false,
highlight: false,
completion_callback: None,
}
}
pub fn hovered(title: impl AsRef<str>) -> Self {
Self {
title: title.as_ref().to_owned(),
value: String::new(),
position: Position::Hovered(
title: title.as_ref().to_owned(),
value: String::new(),
position: Position::Hovered(
// TODO: hardcode
Rect { x: 0, y: 1, width: 50, height: 3 },
),
realtime: false,
highlight: false,
realtime: false,
highlight: false,
completion_callback: None,
}
}

View file

@ -7,6 +7,7 @@
)]
mod blocker;
pub mod completion;
mod context;
mod event;
pub mod external;

View file

@ -0,0 +1,43 @@
use std::mem;
use ratatui::{buffer::Buffer, layout::Rect, widgets::{Block, BorderType, Borders, Clear, Row, Table, Widget}};
use yazi_config::THEME;
use yazi_core::Ctx;
pub(crate) struct Completion<'a> {
cx: &'a Ctx,
}
impl<'a> Completion<'a> {
pub(crate) fn new(cx: &'a Ctx) -> Self { Self { cx } }
}
impl<'a> Widget for Completion<'a> {
fn render(self, _: Rect, buf: &mut Buffer) {
let completion = &self.cx.input.completion;
let area = self.cx.area(&completion.position);
let table = {
let mut table = vec![];
let mut cur_row = vec![];
const COLUMN_CNT: usize = 4;
for (idx, s) in completion.list().into_iter().enumerate() {
if idx != 0 && idx % COLUMN_CNT == 0 {
let t = mem::take(&mut cur_row);
table.push(Row::new(t));
}
cur_row.push(s);
}
Table::new(table).block(
Block::new()
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
// todo
.border_style(THEME.select.border.into()),
)
};
Clear.render(area, buf);
table.render(area, buf);
}
}

View file

@ -0,0 +1,2 @@
mod completion;
pub(super) use completion::*;

View file

@ -1,6 +1,7 @@
#![allow(clippy::module_inception)]
mod app;
mod completion;
mod executor;
mod help;
mod input;

View file

@ -2,7 +2,7 @@ use ratatui::{buffer::Buffer, layout::{Constraint, Direction, Layout, Rect}, wid
use yazi_core::Ctx;
use yazi_plugin::components;
use super::{input, select, tasks, which};
use super::{completion, input, select, tasks, which};
use crate::help;
pub(super) struct Root<'a> {
@ -36,6 +36,10 @@ impl<'a> Widget for Root<'a> {
input::Input::new(self.cx).render(area, buf);
}
if self.cx.input.completion.visible {
completion::Completion::new(self.cx).render(area, buf);
}
if self.cx.help.visible {
help::Layout::new(self.cx).render(area, buf);
}