This commit is contained in:
sxyazi 2023-08-30 13:07:44 +08:00
parent 7a8923a3b6
commit ca4327eb22
No known key found for this signature in database
9 changed files with 73 additions and 45 deletions

View file

@ -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;
}

View file

@ -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::<String>());
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::<String>());
item
})
.collect::<Vec<_>>();
let col2 = bindings
.iter()
.enumerate()
.map(|(i, c)| {
let mut x =
ListItem::new(c.exec.iter().map(ToString::to_string).collect::<Vec<_>>().join("; "));
.map(|c| {
let item = ListItem::new(c.exec());
item
})
.collect::<Vec<_>>();
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::<Vec<_>>();
@ -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);
}
}

View file

@ -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::<Vec<_>>().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))
})

View file

@ -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.

View file

@ -3,14 +3,14 @@
keymap = [
{ on = [ "<Esc>" ], exec = "escape", desc = "Exit visual mode, clear selected, or cancel search" },
{ on = [ "q" ], exec = "quit", desc = "Exit the process" },
{ on = [ "<C-q>" ], exec = "close", desc = "Close the current tab, or quit if it's last tab" },
{ on = [ "<C-q>" ], 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 = [ "<C-s>" ], 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" },

View file

@ -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<Key>,
#[serde(deserialize_with = "Exec::deserialize")]
pub exec: Vec<Exec>,
pub desc: Option<String>,
}
impl Control {
#[inline]
pub fn exec(&self) -> String {
self.exec.iter().map(|e| e.to_string()).collect::<Vec<_>>().join("; ")
}
#[inline]
pub fn desc_or_exec(&self) -> Cow<str> {
if let Some(ref s) = self.desc { Cow::Borrowed(s) } else { self.exec().into() }
}
}

View file

@ -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(())
}
}

View file

@ -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<Key>,
#[serde(deserialize_with = "Exec::deserialize")]
pub exec: Vec<Exec>,
}
#[derive(Debug)]
pub struct Keymap {
pub manager: Vec<Control>,

View file

@ -1,7 +1,9 @@
mod control;
mod exec;
mod key;
mod keymap;
pub use control::*;
pub use exec::*;
pub use key::*;
pub use keymap::*;