yazi/app/src/which/side.rs
三咲雅 · Misaki Masa 4d4586409d
feat: help (#93)
2023-08-31 13:58:13 +08:00

44 lines
1.3 KiB
Rust

use config::keymap::Control;
use ratatui::{prelude::{Buffer, Rect}, style::{Color, Style}, text::{Line, Span}, widgets::{Block, List, ListItem, Padding, Widget}};
pub(super) struct Side<'a> {
times: usize,
cands: Vec<&'a Control>,
}
impl<'a> Side<'a> {
pub(super) fn new(times: usize, cands: Vec<&'a Control>) -> Self { Self { times, cands } }
}
impl Widget for Side<'_> {
fn render(self, area: Rect, buf: &mut Buffer) {
let items = self
.cands
.into_iter()
.map(|c| {
let mut spans = vec![];
// Keys
let keys = c.on[self.times..].iter().map(ToString::to_string).collect::<Vec<_>>();
spans.push(Span::raw(" ".repeat(10usize.saturating_sub(keys.join("").len()))));
spans.push(Span::styled(keys[0].clone(), Style::new().fg(Color::LightCyan)));
spans.extend(
keys
.iter()
.skip(1)
.map(|k| Span::styled(k.to_string(), Style::new().fg(Color::DarkGray))),
);
// Separator
spans.push(Span::styled("".to_string(), Style::new().fg(Color::DarkGray)));
// Exec
spans.push(Span::styled(c.desc_or_exec(), Style::new().fg(Color::Magenta)));
ListItem::new(Line::from(spans))
})
.collect::<Vec<_>>();
List::new(items).block(Block::new().padding(Padding::new(0, 1, 1, 1))).render(area, buf);
}
}