Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new kitty graphics protocol implementation for better compatibility with tmux through Unicode placeholders #365

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ https://github.com/sxyazi/yazi/assets/17523360/92ff23fa-0cd5-4f04-b387-894c12265

| Platform | Protocol | Support |
| ----------------- | -------------------------------------------------------------------------------- | --------------------- |
| Kitty | [Terminal graphics protocol](https://sw.kovidgoyal.net/kitty/graphics-protocol/) | ✅ Built-in |
| kitty | [Terminal graphics protocol](https://sw.kovidgoyal.net/kitty/graphics-protocol/) | ✅ Built-in |
| WezTerm | [Terminal graphics protocol](https://sw.kovidgoyal.net/kitty/graphics-protocol/) | ✅ Built-in |
| Konsole | [Terminal graphics protocol](https://sw.kovidgoyal.net/kitty/graphics-protocol/) | ✅ Built-in |
| iTerm2 | [Inline images protocol](https://iterm2.com/documentation-images.html) | ✅ Built-in |
Expand Down
2 changes: 1 addition & 1 deletion cspell.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"language":"en","flagWords":[],"version":"0.2","words":["Punct","KEYMAP","splitn","crossterm","YAZI","unar","peekable","ratatui","syntect","pbpaste","pbcopy","ffmpegthumbnailer","oneshot","Posix","Lsar","XADDOS","zoxide","cands","Deque","precache","imageops","IFBLK","IFCHR","IFDIR","IFIFO","IFLNK","IFMT","IFSOCK","IRGRP","IROTH","IRUSR","ISGID","ISUID","ISVTX","IWGRP","IWOTH","IWUSR","IXGRP","IXOTH","IXUSR","libc","winsize","TIOCGWINSZ","xpixel","ypixel","ioerr","appender","Catppuccin","macchiato","gitmodules","Dotfiles","bashprofile","vimrc","flac","webp","exiftool","mediainfo","ripgrep","nvim","indexmap","indexmap","unwatch","canonicalize","serde","fsevent","Ueberzug","iterm","wezterm","sixel","chafa","ueberzugpp","️ Überzug","️ Überzug","Konsole","Alacritty","Überzug","pkgs","paru","unarchiver","pdftoppm","poppler","prebuild","singlefile","jpegopt","EXIF","rustfmt","mktemp","nanos","xclip","xsel","natord","Mintty","nixos","nixpkgs","SIGTSTP","SIGCONT","SIGCONT","mlua","nonstatic","userdata","metatable","natsort","backstack","luajit","Succ","Succ","cand","fileencoding","foldmethod","lightgreen","darkgray","lightred","lightyellow","lightcyan","nushell","msvc","aarch","linemode","sxyazi","rsplit","ZELLIJ","bitflags","bitflags","USERPROFILE"]}
{"words":["Punct","KEYMAP","splitn","crossterm","YAZI","unar","peekable","ratatui","syntect","pbpaste","pbcopy","ffmpegthumbnailer","oneshot","Posix","Lsar","XADDOS","zoxide","cands","Deque","precache","imageops","IFBLK","IFCHR","IFDIR","IFIFO","IFLNK","IFMT","IFSOCK","IRGRP","IROTH","IRUSR","ISGID","ISUID","ISVTX","IWGRP","IWOTH","IWUSR","IXGRP","IXOTH","IXUSR","libc","winsize","TIOCGWINSZ","xpixel","ypixel","ioerr","appender","Catppuccin","macchiato","gitmodules","Dotfiles","bashprofile","vimrc","flac","webp","exiftool","mediainfo","ripgrep","nvim","indexmap","indexmap","unwatch","canonicalize","serde","fsevent","Ueberzug","iterm","wezterm","sixel","chafa","ueberzugpp","️ Überzug","️ Überzug","Konsole","Alacritty","Überzug","pkgs","paru","unarchiver","pdftoppm","poppler","prebuild","singlefile","jpegopt","EXIF","rustfmt","mktemp","nanos","xclip","xsel","natord","Mintty","nixos","nixpkgs","SIGTSTP","SIGCONT","SIGCONT","mlua","nonstatic","userdata","metatable","natsort","backstack","luajit","Succ","Succ","cand","fileencoding","foldmethod","lightgreen","darkgray","lightred","lightyellow","lightcyan","nushell","msvc","aarch","linemode","sxyazi","rsplit","ZELLIJ","bitflags","bitflags","USERPROFILE","Neovim"],"version":"0.2","language":"en","flagWords":[]}
30 changes: 20 additions & 10 deletions yazi-adaptor/src/adaptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use ratatui::prelude::Rect;
use tokio::{fs, sync::mpsc::UnboundedSender};
use tracing::warn;
use yazi_config::PREVIEW;
use yazi_shared::RoCell;
use yazi_shared::{env_exists, RoCell};

use super::{Iterm2, Kitty};
use super::{Iterm2, Kitty, KittyOld};
use crate::{ueberzug::Ueberzug, Sixel, TMUX};

static IMAGE_SHOWN: AtomicBool = AtomicBool::new(false);
Expand All @@ -18,6 +18,7 @@ static UEBERZUG: RoCell<Option<UnboundedSender<Option<(PathBuf, Rect)>>>> = RoCe
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Adaptor {
Kitty,
KittyOld,
Iterm2,
Sixel,

Expand All @@ -39,18 +40,23 @@ enum Emulator {
VSCode,
Hyper,
Mintty,
Neovim,
}

impl Adaptor {
fn emulator() -> Emulator {
if env_exists("NVIM_LOG_FILE") && env_exists("NVIM") {
return Emulator::Neovim;
}

let vars = [
("KITTY_WINDOW_ID", Emulator::Kitty),
("KONSOLE_VERSION", Emulator::Konsole),
("ITERM_SESSION_ID", Emulator::Iterm2),
("WEZTERM_EXECUTABLE", Emulator::WezTerm),
("VSCODE_INJECTION", Emulator::VSCode),
];
match vars.into_iter().find(|v| env::var_os(v.0).is_some_and(|s| !s.is_empty())) {
match vars.into_iter().find(|v| env_exists(v.0)) {
Some(var) => return var.1,
None => warn!("[Adaptor] No special environment variables detected"),
}
Expand Down Expand Up @@ -80,21 +86,22 @@ impl Adaptor {
Emulator::Kitty => vec![Self::Kitty],
Emulator::Konsole => vec![Self::Kitty, Self::Iterm2, Self::Sixel],
Emulator::Iterm2 => vec![Self::Iterm2, Self::Sixel],
Emulator::WezTerm => vec![Self::Kitty, Self::Iterm2, Self::Sixel],
Emulator::WezTerm => vec![Self::KittyOld, Self::Iterm2, Self::Sixel],
Emulator::Foot => vec![Self::Sixel],
Emulator::BlackBox => vec![Self::Sixel],
Emulator::VSCode => vec![Self::Sixel],
Emulator::Hyper => vec![Self::Sixel],
Emulator::Mintty => vec![Self::Iterm2],
Emulator::Neovim => vec![],
};

#[cfg(windows)]
protocols.retain(|p| *p == Self::Iterm2);
if env::var_os("ZELLIJ_SESSION_NAME").is_some_and(|s| !s.is_empty()) {
if env_exists("ZELLIJ_SESSION_NAME") {
protocols.retain(|p| *p == Self::Sixel);
}
if *TMUX && protocols.len() > 1 {
protocols.retain(|p| *p != Self::Kitty);
protocols.retain(|p| *p != Self::KittyOld);
}
if let Some(p) = protocols.first() {
return *p;
Expand All @@ -105,10 +112,10 @@ impl Adaptor {
"wayland" => return Self::Wayland,
_ => warn!("[Adaptor] Could not identify XDG_SESSION_TYPE"),
}
if env::var_os("WAYLAND_DISPLAY").is_some_and(|s| !s.is_empty()) {
if env_exists("WAYLAND_DISPLAY") {
return Self::Wayland;
}
if env::var_os("DISPLAY").is_some_and(|s| !s.is_empty()) {
if env_exists("DISPLAY") {
return Self::X11;
}
if std::fs::symlink_metadata("/proc/sys/fs/binfmt_misc/WSLInterop").is_ok() {
Expand Down Expand Up @@ -145,6 +152,7 @@ impl ToString for Adaptor {
fn to_string(&self) -> String {
match self {
Self::Kitty => "kitty",
Self::KittyOld => "kitty",
Self::Iterm2 => "iterm2",
Self::Sixel => "sixel",
Self::X11 => "x11",
Expand All @@ -171,6 +179,7 @@ impl Adaptor {

match self {
Self::Kitty => Kitty::image_show(path, rect).await,
Self::KittyOld => KittyOld::image_show(path, rect).await,
Self::Iterm2 => Iterm2::image_show(path, rect).await,
Self::Sixel => Sixel::image_show(path, rect).await,
_ => Ok(if let Some(tx) = &*UEBERZUG {
Expand All @@ -185,7 +194,8 @@ impl Adaptor {
}

match self {
Self::Kitty => Kitty::image_hide(),
Self::Kitty => Kitty::image_hide(rect),
Self::KittyOld => KittyOld::image_hide(),
Self::Iterm2 => Iterm2::image_hide(rect),
Self::Sixel => Sixel::image_hide(rect),
_ => Ok(if let Some(tx) = &*UEBERZUG {
Expand All @@ -196,6 +206,6 @@ impl Adaptor {

#[inline]
pub(super) fn needs_ueberzug(self) -> bool {
!matches!(self, Self::Kitty | Self::Iterm2 | Self::Sixel)
!matches!(self, Self::Kitty | Self::KittyOld | Self::Iterm2 | Self::Sixel)
}
}
3 changes: 2 additions & 1 deletion yazi-adaptor/src/iterm2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ impl Iterm2 {

pub(super) fn image_hide(rect: Rect) -> Result<()> {
let stdout = BufWriter::new(stdout().lock());
let s = " ".repeat(rect.width as usize);
Term::move_lock(stdout, (0, 0), |stdout| {
for y in rect.top()..rect.bottom() {
Term::move_to(stdout, rect.x, y)?;
Term::kill_to_end(stdout)?;
stdout.write_all(s.as_bytes())?;
}
Ok(())
})
Expand Down
Loading