WIP(completion): render components

This commit is contained in:
XOR-op 2023-10-26 21:37:48 -04:00
parent 0b85e1fc95
commit 6a31ed14ce
7 changed files with 64 additions and 13 deletions

View file

@ -33,6 +33,8 @@ impl Completion {
let old = self.cursor;
self.cursor = (self.cursor + step).min(len - 1);
eprintln!("Cur: {}", self.items.get(self.cursor).unwrap());
old != self.cursor
}
@ -44,4 +46,6 @@ impl Completion {
}
pub fn list(&self) -> Vec<String> { self.items.clone() }
pub fn selected_cursor(&self) -> usize { self.cursor }
}

View file

@ -8,12 +8,22 @@ pub struct CompletionOpt {
}
impl CompletionOpt {
pub fn hovered() -> Self {
pub fn top() -> Self {
Self {
items: vec![],
position: Position::Top(
// TODO: hardcode
Rect { x: 0, y: 5, width: 100, height: 10 },
),
}
}
pub fn hover() -> Self {
Self {
items: vec![],
position: Position::Hovered(
// TODO: hardcode
Rect { x: 0, y: 1, width: 50, height: 3 },
Rect { x: 0, y: 3, width: 40, height: 4 },
),
}
}

View file

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

View file

@ -43,6 +43,8 @@ impl Input {
// Shell
self.highlight = opt.highlight;
self.completion_callback = opt.completion_callback
}
pub fn close(&mut self, submit: bool) -> bool {
@ -52,6 +54,7 @@ impl Input {
}
self.visible = false;
self.completion.close(false);
true
}
@ -192,6 +195,7 @@ impl Input {
return false;
}
eprintln!("Type key {:?}", key);
match key {
Key { code: KeyCode::Tab, shift: false, ctrl: false, alt: false } => return self.complete(),
_ => (),

View file

@ -57,4 +57,13 @@ impl InputOpt {
self.highlight = true;
self
}
#[inline]
pub fn with_completion<F: Fn(String) -> Vec<String> + Send + 'static>(
mut self,
callback: F,
) -> Self {
self.completion_callback = Some(Box::new(callback));
self
}
}

View file

@ -59,8 +59,11 @@ impl Tab {
pub fn cd_interactive(&mut self, target: Url) -> bool {
tokio::spawn(async move {
let mut result =
emit!(Input(InputOpt::top("Change directory:").with_value(target.to_string_lossy())));
let mut result = emit!(Input(
InputOpt::top("Change directory:")
.with_value(target.to_string_lossy())
.with_completion(|s| vec![s, "Hi".to_string()])
));
if let Some(Ok(s)) = result.recv().await {
emit!(Cd(Url::from(s.trim())));

View file

@ -1,6 +1,6 @@
use std::mem;
use ratatui::{buffer::Buffer, layout::Rect, widgets::{Block, BorderType, Borders, Clear, Row, Table, Widget}};
use ratatui::{buffer::Buffer, layout::{Constraint, Rect}, widgets::{Block, BorderType, Borders, Cell, Clear, Row, Table, Widget}};
use yazi_config::THEME;
use yazi_core::Ctx;
@ -17,24 +17,42 @@ impl<'a> Widget for Completion<'a> {
let completion = &self.cx.input.completion;
let area = self.cx.area(&completion.position);
const COLUMN_CNT: usize = 4;
const MAX_WIDTH: usize = 20;
let constraint =
(0..COLUMN_CNT).map(|_| Constraint::Ratio(1, MAX_WIDTH as u32)).collect::<Vec<Constraint>>();
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);
// todo
cur_row.push(
Cell::from(if s.len() < MAX_WIDTH {
s
} else {
s.split_at(MAX_WIDTH - 1).0.to_string() + ""
})
.style(if completion.selected_cursor() == idx {
THEME.select.active.into()
} else {
THEME.select.inactive.into()
}),
);
}
Table::new(table).block(
Block::new()
table.push(Row::new(cur_row));
Table::new(table)
.block(
Block::new()
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.border_type(BorderType::Double)
// todo
.border_style(THEME.select.border.into()),
)
)
.widths(&constraint)
};
Clear.render(area, buf);