use super::key::Binding;
use crate::style::{Color, Position, Style, join_horizontal, width};
use crate::{Cmd, Msg};
pub trait KeyMap {
fn short_help(&self) -> Vec<Binding>;
fn full_help(&self) -> Vec<Vec<Binding>>;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Styles {
pub ellipsis: Style,
pub short_key: Style,
pub short_desc: Style,
pub short_separator: Style,
pub full_key: Style,
pub full_desc: Style,
pub full_separator: Style,
}
impl Styles {
#[must_use]
pub fn new(is_dark: bool) -> Self {
let pick = |dark: Color, light: Color| if is_dark { dark } else { light };
let key = Style::new().fg(pick(
Color::Rgb(0x62, 0x62, 0x62),
Color::Rgb(0x90, 0x90, 0x90),
));
let desc = Style::new().fg(pick(
Color::Rgb(0x4A, 0x4A, 0x4A),
Color::Rgb(0xB2, 0xB2, 0xB2),
));
let sep = Style::new().fg(pick(
Color::Rgb(0x3C, 0x3C, 0x3C),
Color::Rgb(0xDA, 0xDA, 0xDA),
));
Self {
ellipsis: sep,
short_key: key,
short_desc: desc,
short_separator: sep,
full_key: key,
full_desc: desc,
full_separator: sep,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Model {
pub show_all: bool,
pub short_separator: String,
pub full_separator: String,
pub ellipsis: String,
pub styles: Styles,
width: usize,
}
impl Default for Model {
fn default() -> Self {
Self::new()
}
}
impl Model {
#[must_use]
pub fn new() -> Self {
Self {
show_all: false,
short_separator: " • ".into(),
full_separator: " ".into(),
ellipsis: "…".into(),
styles: Styles::new(true),
width: 0,
}
}
pub fn update<M>(&mut self, _msg: &Msg<M>) -> Cmd<M> {
Cmd::none()
}
#[must_use]
pub fn view(&self, keys: &impl KeyMap) -> String {
if self.show_all {
self.full_help_view(&keys.full_help())
} else {
self.short_help_view(&keys.short_help())
}
}
pub const fn set_width(&mut self, columns: usize) {
self.width = columns;
}
#[must_use]
pub const fn width(&self) -> usize {
self.width
}
#[must_use]
pub fn short_help_view(&self, bindings: &[Binding]) -> String {
let separator = self.styles.short_separator.paint(&self.short_separator);
let mut out = String::new();
let mut total = 0;
for b in bindings.iter().filter(|b| b.enabled()) {
let sep = if total > 0 { separator.as_str() } else { "" };
let item = format!(
"{sep}{} {}",
self.styles.short_key.paint(&b.help().key),
self.styles.short_desc.paint(&b.help().desc)
);
let w = width(&item);
if let Err(tail) = self.fits(total, w) {
out.push_str(&tail);
break;
}
total += w;
out.push_str(&item);
}
out
}
#[must_use]
pub fn full_help_view(&self, groups: &[Vec<Binding>]) -> String {
let mut columns = Vec::new();
let mut total = 0;
for group in groups {
let on: Vec<&Binding> = group.iter().filter(|b| b.enabled()).collect();
if on.is_empty() {
continue;
}
let column = self.column(&on, total > 0);
let w = column.split('\n').map(width).max().unwrap_or(0);
if let Err(tail) = self.fits(total, w) {
columns.push(tail);
break;
}
total += w;
columns.push(column);
}
let blocks: Vec<&str> = columns.iter().map(String::as_str).collect();
join_horizontal(Position::TOP, &blocks)
}
fn column(&self, bindings: &[&Binding], separated: bool) -> String {
let keys: Vec<&str> = bindings.iter().map(|b| b.help().key.as_str()).collect();
let descs: Vec<&str> = bindings.iter().map(|b| b.help().desc.as_str()).collect();
let sep = if separated {
let blank = " ".repeat(width(&self.full_separator));
let mut lines = vec![self.full_separator.clone()];
lines.resize(bindings.len(), blank);
self.styles.full_separator.render(&lines.join("\n"))
} else {
String::new()
};
let keys = self.styles.full_key.render(&keys.join("\n"));
let descs = self.styles.full_desc.render(&descs.join("\n"));
join_horizontal(Position::TOP, &[&sep, &keys, " ", &descs])
}
fn fits(&self, total: usize, w: usize) -> Result<(), String> {
if self.width == 0 || total + w <= self.width {
return Ok(());
}
let tail = format!(" {}", self.styles.ellipsis.paint(&self.ellipsis));
if total + width(&tail) < self.width {
Err(tail)
} else {
Err(String::new())
}
}
}
#[cfg(test)]
mod tests;