mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
WIP(completion): render components
This commit is contained in:
parent
0b85e1fc95
commit
6a31ed14ce
7 changed files with 64 additions and 13 deletions
|
|
@ -33,6 +33,8 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -44,4 +46,6 @@ 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 }
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,12 +8,22 @@ pub struct CompletionOpt {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl 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 {
|
Self {
|
||||||
items: vec![],
|
items: vec![],
|
||||||
position: Position::Hovered(
|
position: Position::Hovered(
|
||||||
// TODO: hardcode
|
// TODO: hardcode
|
||||||
Rect { x: 0, y: 1, width: 50, height: 3 },
|
Rect { x: 0, y: 3, width: 40, height: 4 },
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,17 +3,20 @@ 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.clone();
|
let current = self.snaps.current().value.clone();
|
||||||
let result = self.completion_callback.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 => self.type_str(result.get(0).unwrap()),
|
||||||
_ => {
|
_ => {
|
||||||
self.completion.show(CompletionOpt::hovered().with_items(result));
|
self.completion.show(CompletionOpt::top().with_items(result));
|
||||||
false
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
eprintln!("Completing @");
|
||||||
self.completion.next(1)
|
self.completion.next(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -43,6 +43,8 @@ impl Input {
|
||||||
|
|
||||||
// Shell
|
// Shell
|
||||||
self.highlight = opt.highlight;
|
self.highlight = opt.highlight;
|
||||||
|
|
||||||
|
self.completion_callback = opt.completion_callback
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn close(&mut self, submit: bool) -> bool {
|
pub fn close(&mut self, submit: bool) -> bool {
|
||||||
|
|
@ -52,6 +54,7 @@ impl Input {
|
||||||
}
|
}
|
||||||
|
|
||||||
self.visible = false;
|
self.visible = false;
|
||||||
|
self.completion.close(false);
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -192,6 +195,7 @@ impl Input {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
eprintln!("Type key {:?}", key);
|
||||||
match key {
|
match key {
|
||||||
Key { code: KeyCode::Tab, shift: false, ctrl: false, alt: false } => return self.complete(),
|
Key { code: KeyCode::Tab, shift: false, ctrl: false, alt: false } => return self.complete(),
|
||||||
_ => (),
|
_ => (),
|
||||||
|
|
|
||||||
|
|
@ -57,4 +57,13 @@ impl InputOpt {
|
||||||
self.highlight = true;
|
self.highlight = true;
|
||||||
self
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -59,8 +59,11 @@ 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 =
|
let mut result = emit!(Input(
|
||||||
emit!(Input(InputOpt::top("Change directory:").with_value(target.to_string_lossy())));
|
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 {
|
if let Some(Ok(s)) = result.recv().await {
|
||||||
emit!(Cd(Url::from(s.trim())));
|
emit!(Cd(Url::from(s.trim())));
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use std::mem;
|
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_config::THEME;
|
||||||
use yazi_core::Ctx;
|
use yazi_core::Ctx;
|
||||||
|
|
||||||
|
|
@ -17,24 +17,42 @@ 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;
|
||||||
|
const MAX_WIDTH: usize = 20;
|
||||||
|
let constraint =
|
||||||
|
(0..COLUMN_CNT).map(|_| Constraint::Ratio(1, MAX_WIDTH as u32)).collect::<Vec<Constraint>>();
|
||||||
let table = {
|
let table = {
|
||||||
let mut table = vec![];
|
let mut table = vec![];
|
||||||
let mut cur_row = vec![];
|
let mut cur_row = vec![];
|
||||||
const COLUMN_CNT: usize = 4;
|
|
||||||
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 % COLUMN_CNT == 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));
|
||||||
}
|
}
|
||||||
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(
|
table.push(Row::new(cur_row));
|
||||||
Block::new()
|
Table::new(table)
|
||||||
|
.block(
|
||||||
|
Block::new()
|
||||||
.borders(Borders::ALL)
|
.borders(Borders::ALL)
|
||||||
.border_type(BorderType::Rounded)
|
.border_type(BorderType::Double)
|
||||||
// todo
|
// todo
|
||||||
.border_style(THEME.select.border.into()),
|
.border_style(THEME.select.border.into()),
|
||||||
)
|
)
|
||||||
|
.widths(&constraint)
|
||||||
};
|
};
|
||||||
|
|
||||||
Clear.render(area, buf);
|
Clear.render(area, buf);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue