From ca4327eb22de48a588c82ed93e2add189c1d8675 Mon Sep 17 00:00:00 2001 From: sxyazi Date: Wed, 30 Aug 2023 13:07:44 +0800 Subject: [PATCH] .. --- app/src/executor.rs | 2 +- app/src/help/bindings.rs | 42 ++++++++++++++++++++---------------- app/src/which/side.rs | 3 +-- config/docs/keymap.md | 2 +- config/preset/keymap.toml | 10 ++++----- config/src/keymap/control.rs | 25 +++++++++++++++++++++ config/src/keymap/exec.rs | 23 +++++++++++--------- config/src/keymap/keymap.rs | 9 +------- config/src/keymap/mod.rs | 2 ++ 9 files changed, 73 insertions(+), 45 deletions(-) create mode 100644 config/src/keymap/control.rs diff --git a/app/src/executor.rs b/app/src/executor.rs index 7b3558bc..24b35b6f 100644 --- a/app/src/executor.rs +++ b/app/src/executor.rs @@ -21,7 +21,7 @@ impl Executor { } } - for Control { on, exec } in KEYMAP.get(layer) { + for Control { on, exec, .. } in KEYMAP.get(layer) { if on.is_empty() || on[0] != key { continue; } diff --git a/app/src/help/bindings.rs b/app/src/help/bindings.rs index c273132a..69dc7025 100644 --- a/app/src/help/bindings.rs +++ b/app/src/help/bindings.rs @@ -1,4 +1,4 @@ -use ratatui::{layout::{self, Constraint}, prelude::{Buffer, Direction, Rect}, style::{Modifier, Style}, widgets::{List, ListItem, Widget}}; +use ratatui::{layout::{self, Constraint}, prelude::{Buffer, Direction, Rect}, style::{Color, Style, Stylize}, widgets::{List, ListItem, Widget}}; use crate::context::Ctx; @@ -13,32 +13,31 @@ impl<'a> Bindings<'a> { impl Widget for Bindings<'_> { fn render(self, area: Rect, buf: &mut Buffer) { let bindings = &self.cx.help.window(); - let cursor = self.cx.help.rel_cursor(); + if bindings.is_empty() { + return; + } let col1 = bindings .iter() - .enumerate() - .map(|(i, c)| { - let mut x = ListItem::new(c.on.iter().map(ToString::to_string).collect::()); - - if i == cursor { - x = x.style(Style::new().add_modifier(Modifier::UNDERLINED)); - } - x + .map(|c| { + let item = ListItem::new(c.on.iter().map(ToString::to_string).collect::()); + item }) .collect::>(); let col2 = bindings .iter() - .enumerate() - .map(|(i, c)| { - let mut x = - ListItem::new(c.exec.iter().map(ToString::to_string).collect::>().join("; ")); + .map(|c| { + let item = ListItem::new(c.exec()); + item + }) + .collect::>(); - if i == cursor { - x = x.style(Style::new().add_modifier(Modifier::UNDERLINED)); - } - x + let col3 = bindings + .iter() + .map(|c| { + let item = ListItem::new(if let Some(ref desc) = c.desc { desc } else { "-" }); + item }) .collect::>(); @@ -49,7 +48,14 @@ impl Widget for Bindings<'_> { ) .split(area); + let cursor = self.cx.help.rel_cursor() as u16; + buf.set_style( + Rect { x: area.x, y: area.y + cursor, width: area.width, height: 1 }, + Style::new().bg(Color::Blue).fg(Color::Black).bold(), + ); + List::new(col1).render(chunks[0], buf); List::new(col2).render(chunks[1], buf); + List::new(col3).render(chunks[2], buf); } } diff --git a/app/src/which/side.rs b/app/src/which/side.rs index bbdd95e8..a0c10a69 100644 --- a/app/src/which/side.rs +++ b/app/src/which/side.rs @@ -33,8 +33,7 @@ impl Widget for Side<'_> { spans.push(Span::styled("  ".to_string(), Style::new().fg(Color::DarkGray))); // Exec - let exec = c.exec.iter().map(ToString::to_string).collect::>().join("; "); - spans.push(Span::styled(exec, Style::new().fg(Color::Magenta))); + spans.push(Span::styled(c.desc_or_exec(), Style::new().fg(Color::Magenta))); ListItem::new(Line::from(spans)) }) diff --git a/config/docs/keymap.md b/config/docs/keymap.md index c3d591d9..18660dc8 100644 --- a/config/docs/keymap.md +++ b/config/docs/keymap.md @@ -66,7 +66,7 @@ - rename: Rename a file or directory. - copy: Copy the path of files or directories that are selected or hovered on. - - `path`: Copy the full absolute path. + - `path`: Copy the absolute path. - `dirname`: Copy the path of the parent directory. - `filename`: Copy the name of the file. - `name_without_ext`: Copy the name of the file without the extension. diff --git a/config/preset/keymap.toml b/config/preset/keymap.toml index 23e31b93..c56ba252 100644 --- a/config/preset/keymap.toml +++ b/config/preset/keymap.toml @@ -3,14 +3,14 @@ keymap = [ { on = [ "" ], exec = "escape", desc = "Exit visual mode, clear selected, or cancel search" }, { on = [ "q" ], exec = "quit", desc = "Exit the process" }, - { on = [ "" ], exec = "close", desc = "Close the current tab, or quit if it's last tab" }, + { on = [ "" ], exec = "close", desc = "Close the current tab, or quit if it is last tab" }, # Navigation { on = [ "k" ], exec = "arrow -1", desc = "Move cursor up" }, { on = [ "j" ], exec = "arrow 1", desc = "Move cursor down" }, { on = [ "K" ], exec = "arrow -5", desc = "Move cursor up 5 lines" }, - { on = [ "J" ], exec = "arrow 5", desc = "Move cursor down 5 line" }, + { on = [ "J" ], exec = "arrow 5", desc = "Move cursor down 5 lines" }, { on = [ "h" ], exec = "leave", desc = "Go back to the parent directory" }, { on = [ "l" ], exec = "enter", desc = "Enter the child directory" }, @@ -54,11 +54,11 @@ keymap = [ { on = [ "s" ], exec = "search fd", desc = "Search files by content using ripgrep" }, { on = [ "S" ], exec = "search rg", desc = "Search files by name using fd" }, { on = [ "" ], exec = "search none", desc = "Cancel the ongoing search" }, - { on = [ "z" ], exec = "jump zoxide", desc = "Jump to a directory, or reveal a file using fzf" }, - { on = [ "Z" ], exec = "jump fzf", desc = "Jump to a directory using zoxide" }, + { on = [ "z" ], exec = "jump zoxide", desc = "Jump to a directory using zoxide" }, + { on = [ "Z" ], exec = "jump fzf", desc = "Jump to a directory, or reveal a file using fzf" }, # Copy - { on = [ "c", "c" ], exec = "copy path", desc = "Copy the full absolute path" }, + { on = [ "c", "c" ], exec = "copy path", desc = "Copy the absolute path" }, { on = [ "c", "d" ], exec = "copy dirname", desc = "Copy the path of the parent directory" }, { on = [ "c", "f" ], exec = "copy filename", desc = "Copy the name of the file" }, { on = [ "c", "n" ], exec = "copy name_without_ext", desc = "Copy the name of the file without the extension" }, diff --git a/config/src/keymap/control.rs b/config/src/keymap/control.rs new file mode 100644 index 00000000..6f856ffc --- /dev/null +++ b/config/src/keymap/control.rs @@ -0,0 +1,25 @@ +use std::borrow::Cow; + +use serde::Deserialize; + +use super::{Exec, Key}; + +#[derive(Clone, Debug, Deserialize)] +pub struct Control { + pub on: Vec, + #[serde(deserialize_with = "Exec::deserialize")] + pub exec: Vec, + pub desc: Option, +} + +impl Control { + #[inline] + pub fn exec(&self) -> String { + self.exec.iter().map(|e| e.to_string()).collect::>().join("; ") + } + + #[inline] + pub fn desc_or_exec(&self) -> Cow { + if let Some(ref s) = self.desc { Cow::Borrowed(s) } else { self.exec().into() } + } +} diff --git a/config/src/keymap/exec.rs b/config/src/keymap/exec.rs index cb1ebf72..3180567f 100644 --- a/config/src/keymap/exec.rs +++ b/config/src/keymap/exec.rs @@ -1,4 +1,4 @@ -use std::{collections::BTreeMap, fmt::{self, Debug}}; +use std::{collections::BTreeMap, fmt::{self, Debug, Display}}; use anyhow::bail; use serde::{de::{self, Visitor}, Deserializer}; @@ -34,16 +34,19 @@ impl TryFrom<&str> for Exec { } } -impl ToString for Exec { - fn to_string(&self) -> String { - let mut s = Vec::with_capacity(self.args.len() + self.named.len() + 1); - s.push(self.cmd.clone()); - s.extend(self.args.iter().cloned()); - for (key, val) in self.named.iter() { - s.push(format!("--{}={}", key, val)); +impl Display for Exec { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.cmd)?; + if !self.args.is_empty() { + write!(f, " {}", self.args.join(" "))?; } - - shell_words::join(s) + for (k, v) in &self.named { + write!(f, " --{k}")?; + if !v.is_empty() { + write!(f, "={v}")?; + } + } + Ok(()) } } diff --git a/config/src/keymap/keymap.rs b/config/src/keymap/keymap.rs index 392eb9f6..9e4dc020 100644 --- a/config/src/keymap/keymap.rs +++ b/config/src/keymap/keymap.rs @@ -1,15 +1,8 @@ use serde::{Deserialize, Deserializer}; -use super::{Exec, Key}; +use super::Control; use crate::MERGED_KEYMAP; -#[derive(Clone, Debug, Deserialize)] -pub struct Control { - pub on: Vec, - #[serde(deserialize_with = "Exec::deserialize")] - pub exec: Vec, -} - #[derive(Debug)] pub struct Keymap { pub manager: Vec, diff --git a/config/src/keymap/mod.rs b/config/src/keymap/mod.rs index 12bd2809..4ca907b5 100644 --- a/config/src/keymap/mod.rs +++ b/config/src/keymap/mod.rs @@ -1,7 +1,9 @@ +mod control; mod exec; mod key; mod keymap; +pub use control::*; pub use exec::*; pub use key::*; pub use keymap::*;