feat: supports Ghostty image rendering using kitty unicode placeholders (#1427)

This commit is contained in:
三咲雅 · Misaki Masa 2024-08-07 12:17:14 +08:00 committed by GitHub
parent f865910481
commit 4aaca4c432
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 45 additions and 42 deletions

View File

@ -29,16 +29,7 @@ body:
attributes:
label: "`yazi --debug` output"
description: Please run `yazi --debug` and paste the debug information here.
value: |
<details>
```sh
##### ↓↓↓ Paste the output here: ↓↓↓ #####
```
</details>
render: Shell
validations:
required: true
- type: dropdown

View File

@ -8,16 +8,7 @@ body:
attributes:
label: "`yazi --debug` output"
description: Please run `yazi --debug` and paste the debug information here.
value: |
<details>
```sh
##### ↓↓↓ Paste the output here: ↓↓↓ #####
```
</details>
render: Shell
validations:
required: true
- type: textarea

View File

@ -46,7 +46,7 @@ https://github.com/sxyazi/yazi/assets/17523360/92ff23fa-0cd5-4f04-b387-894c12265
| WezTerm | [Inline images protocol](https://iterm2.com/documentation-images.html) | ✅ Built-in |
| Mintty (Git Bash) | [Inline images protocol](https://iterm2.com/documentation-images.html) | ✅ Built-in |
| foot | [Sixel graphics format](https://www.vt100.net/docs/vt3xx-gp/chapter14.html) | ✅ Built-in |
| Ghostty | [Kitty old protocol](https://github.com/sxyazi/yazi/blob/main/yazi-adapter/src/kitty_old.rs) | ✅ Built-in |
| Ghostty | [Kitty unicode placeholders](https://sw.kovidgoyal.net/kitty/graphics-protocol/#unicode-placeholders) | ✅ Built-in |
| Black Box | [Sixel graphics format](https://www.vt100.net/docs/vt3xx-gp/chapter14.html) | ✅ Built-in |
| VSCode | [Inline images protocol](https://iterm2.com/documentation-images.html) | ✅ Built-in |
| Tabby | [Inline images protocol](https://iterm2.com/documentation-images.html) | ✅ Built-in |

View File

@ -58,8 +58,8 @@ impl Adapter {
pub fn image_erase(self, area: Rect) -> Result<()> {
match self {
Self::Kitty => Kitty::image_erase(area),
Self::Iterm2 => Iterm2::image_erase(area),
Self::KittyOld => KittyOld::image_erase(area),
Self::Iterm2 => Iterm2::image_erase(area),
Self::Sixel => Sixel::image_erase(area),
Self::X11 | Self::Wayland => Ueberzug::image_erase(area),
Self::Chafa => Chafa::image_erase(area),

View File

@ -37,7 +37,7 @@ impl Emulator {
Self::Iterm2 => vec![Adapter::Iterm2, Adapter::Sixel],
Self::WezTerm => vec![Adapter::Iterm2, Adapter::Sixel],
Self::Foot => vec![Adapter::Sixel],
Self::Ghostty => vec![Adapter::KittyOld],
Self::Ghostty => vec![Adapter::Kitty],
Self::BlackBox => vec![Adapter::Sixel],
Self::VSCode => vec![Adapter::Iterm2, Adapter::Sixel],
Self::Tabby => vec![Adapter::Iterm2, Adapter::Sixel],

View File

@ -1,4 +1,4 @@
use std::{path::PathBuf, str::FromStr, time::{SystemTime, UNIX_EPOCH}};
use std::{borrow::Cow, path::PathBuf, str::FromStr, time::{SystemTime, UNIX_EPOCH}};
use anyhow::Context;
use serde::{Deserialize, Serialize};
@ -29,6 +29,22 @@ impl Preview {
let time = SystemTime::now().duration_since(UNIX_EPOCH).expect("Time went backwards");
self.cache_dir.join(format!("{prefix}-{}", time.as_nanos() / 1000))
}
#[inline]
pub fn indent(&self) -> Cow<'static, str> {
match self.tab_size {
0 => Cow::Borrowed(""),
1 => Cow::Borrowed(" "),
2 => Cow::Borrowed(" "),
3 => Cow::Borrowed(" "),
4 => Cow::Borrowed(" "),
5 => Cow::Borrowed(" "),
6 => Cow::Borrowed(" "),
7 => Cow::Borrowed(" "),
8 => Cow::Borrowed(" "),
n => Cow::Owned(" ".repeat(n as usize)),
}
}
}
impl FromStr for Preview {

View File

@ -25,7 +25,7 @@ impl<'a> Input<'a> {
if let Some(syntax) = syntaxes.find_syntax_by_name("Bourne Again Shell (bash)") {
let mut h = HighlightLines::new(syntax, theme);
let regions = h.highlight_line(self.cx.input.value(), syntaxes)?;
return Ok(Highlighter::to_line_widget(regions, &" ".repeat(PREVIEW.tab_size as usize)));
return Ok(Highlighter::to_line_widget(regions, &PREVIEW.indent()));
}
bail!("Failed to find syntax")
}

View File

@ -34,15 +34,13 @@ function Status:style()
end
function Status:mode()
local mode = tostring(self._tab.mode):upper()
if mode == "UNSET" then
mode = "UN-SET"
end
local mode = tostring(self._tab.mode):sub(1, 3):upper()
local style = self:style()
return ui.Line {
ui.Span(THEME.status.separator_open):fg(style.bg),
ui.Span(" " .. mode .. " "):style(style),
ui.Span(THEME.status.separator_close):fg(style.bg):bg(THEME.status.separator_style.fg),
}
end
@ -106,11 +104,11 @@ function Status:percentage()
end
if percent == 0 then
percent = " Top "
percent = " Top "
elseif percent == 100 then
percent = " Bot "
percent = " Bot "
else
percent = string.format(" %3d%% ", percent)
percent = string.format(" %2d%% ", percent)
end
local style = self:style()
@ -126,6 +124,7 @@ function Status:position()
local style = self:style()
return ui.Line {
ui.Span(THEME.status.separator_open):fg(style.bg):bg(THEME.status.separator_style.fg),
ui.Span(string.format(" %2d/%-2d ", cursor + 1, length)):style(style),
ui.Span(THEME.status.separator_close):fg(style.bg),
}

View File

@ -60,9 +60,9 @@ impl Highlighter {
let mut reader = BufReader::new(File::open(&self.path).await?);
let syntax = Self::find_syntax(&self.path).await;
let mut plain = syntax.is_err();
let mut plain = syntax.is_err() as u8;
let mut before = Vec::with_capacity(if plain { 0 } else { skip });
let mut before = Vec::with_capacity(if plain == 0 { skip } else { 0 });
let mut after = Vec::with_capacity(limit);
let mut i = 0;
@ -73,8 +73,11 @@ impl Highlighter {
break;
}
if !plain && (buf.len() > 5000 || buf.contains(&0x1b)) {
plain = true;
if plain == 0 && buf.len() > 5000 {
plain = 1;
drop(mem::take(&mut before));
} else if plain == 0 && buf.contains(&0x1b) {
plain = 2;
drop(mem::take(&mut before));
}
@ -86,7 +89,7 @@ impl Highlighter {
if i > skip {
after.push(String::from_utf8_lossy(&buf).into_owned());
} else if !plain {
} else if plain == 0 {
before.push(String::from_utf8_lossy(&buf).into_owned());
}
buf.clear();
@ -96,11 +99,14 @@ impl Highlighter {
return Err(PeekError::Exceed(i.saturating_sub(limit)));
}
if plain {
let indent = " ".repeat(PREVIEW.tab_size as usize);
Ok(Text::from(after.join("").replace('\x1b', "^[").replace('\t', &indent)))
} else {
if plain == 0 {
Self::highlight_with(before, after, syntax.unwrap()).await
} else if plain == 1 {
Ok(Text::from(after.join("").replace('\t', &PREVIEW.indent())))
} else if plain == 2 {
Ok(Text::from(after.join("").replace('\x1b', "^[").replace('\t', &PREVIEW.indent())))
} else {
unreachable!()
}
}
@ -121,7 +127,7 @@ impl Highlighter {
h.highlight_line(&line, syntaxes).map_err(|e| anyhow!(e))?;
}
let indent = " ".repeat(PREVIEW.tab_size as usize);
let indent = PREVIEW.indent();
let mut lines = Vec::with_capacity(after.len());
for line in after {
if ticket != INCR.load(Ordering::Relaxed) {