Skip to content

Commit

Permalink
feat: support Super/Command/Windows key notation D- (#1069)
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi committed May 22, 2024
1 parent f41e3c8 commit d9ecffd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
14 changes: 7 additions & 7 deletions yazi-adaptor/src/iterm2.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{io::Write, path::Path};

use anyhow::Result;
use base64::{engine::general_purpose, Engine};
use base64::{engine::{general_purpose::STANDARD, Config}, Engine};
use image::{codecs::jpeg::JpegEncoder, DynamicImage};
use ratatui::layout::Rect;
use yazi_shared::term::Term;
Expand Down Expand Up @@ -38,20 +38,20 @@ impl Iterm2 {

async fn encode(img: DynamicImage) -> Result<Vec<u8>> {
tokio::task::spawn_blocking(move || {
let size = (img.width(), img.height());

let mut jpg = vec![];
JpegEncoder::new_with_quality(&mut jpg, 75).encode_image(&img)?;

let mut buf = vec![];
let len = base64::encoded_len(jpg.len(), STANDARD.config().encode_padding());
let mut buf = Vec::with_capacity(200 + len.unwrap_or(1 << 16));

write!(
buf,
"{}]1337;File=inline=1;size={};width={}px;height={}px;doNotMoveCursor=1:{}\x07{}",
START,
jpg.len(),
size.0,
size.1,
general_purpose::STANDARD.encode(&jpg),
img.width(),
img.height(),
STANDARD.encode(&jpg),
CLOSE
)?;
Ok(buf)
Expand Down
25 changes: 14 additions & 11 deletions yazi-config/src/keymap/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,27 @@ use serde::Deserialize;
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Eq, Hash)]
#[serde(try_from = "String")]
pub struct Key {
pub code: KeyCode,
pub shift: bool,
pub ctrl: bool,
pub alt: bool,
pub code: KeyCode,
pub shift: bool,
pub ctrl: bool,
pub alt: bool,
pub super_: bool,
}

impl Key {
#[inline]
pub fn plain(&self) -> Option<char> {
match self.code {
KeyCode::Char(c) if !self.ctrl && !self.alt => Some(c),
KeyCode::Char(c) if !self.ctrl && !self.alt && !self.super_ => Some(c),
_ => None,
}
}

#[inline]
pub fn is_enter(&self) -> bool {
matches!(self, Key { code: KeyCode::Enter, shift: false, ctrl: false, alt: false })
}
}

impl Default for Key {
fn default() -> Self { Self { code: KeyCode::Null, shift: false, ctrl: false, alt: false } }
fn default() -> Self {
Self { code: KeyCode::Null, shift: false, ctrl: false, alt: false, super_: false }
}
}

impl From<KeyEvent> for Key {
Expand All @@ -56,6 +54,7 @@ impl From<KeyEvent> for Key {
shift,
ctrl: value.modifiers.contains(KeyModifiers::CONTROL),
alt: value.modifiers.contains(KeyModifiers::ALT),
super_: value.modifiers.contains(KeyModifiers::SUPER),
}
}
}
Expand All @@ -82,6 +81,7 @@ impl FromStr for Key {
"S-" => key.shift = true,
"C-" => key.ctrl = true,
"A-" => key.alt = true,
"D-" => key.super_ = true,

"Space" => key.code = KeyCode::Char(' '),
"Backspace" => key.code = KeyCode::Backspace,
Expand Down Expand Up @@ -140,6 +140,9 @@ impl Display for Key {
}

write!(f, "<")?;
if self.super_ {
write!(f, "D-")?;
}
if self.ctrl {
write!(f, "C-")?;
}
Expand Down
6 changes: 3 additions & 3 deletions yazi-core/src/help/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ impl Help {
};

match key {
Key { code: KeyCode::Esc, shift: false, ctrl: false, alt: false } => {
Key { code: KeyCode::Esc, shift: false, ctrl: false, alt: false, super_: false } => {
self.in_filter = None;
render!();
}
Key { code: KeyCode::Enter, shift: false, ctrl: false, alt: false } => {
Key { code: KeyCode::Enter, shift: false, ctrl: false, alt: false, super_: false } => {
self.in_filter = None;
return render_and!(true); // Don't do the `filter_apply` below, since we already have the filtered results.
}
Key { code: KeyCode::Backspace, shift: false, ctrl: false, alt: false } => {
Key { code: KeyCode::Backspace, shift: false, ctrl: false, alt: false, super_: false } => {
input.backspace(false);
}
_ => {
Expand Down

0 comments on commit d9ecffd

Please sign in to comment.