From 8ed569b729dc841bf09fb10c5f32194e0da545ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E9=9B=85=20=C2=B7=20Misaki=20Masa?= Date: Wed, 30 Apr 2025 23:22:52 +0800 Subject: [PATCH] refactor: move `tty` to the new `yazi-term` crate (#2701) --- Cargo.lock | 9 +++++++++ yazi-adapter/Cargo.toml | 1 + yazi-adapter/src/drivers/kgp_old.rs | 2 +- yazi-adapter/src/emulator.rs | 3 ++- yazi-adapter/src/mux.rs | 2 +- yazi-config/Cargo.toml | 1 + yazi-config/src/lib.rs | 3 ++- yazi-core/Cargo.toml | 1 + yazi-core/src/mgr/commands/bulk_rename.rs | 3 ++- yazi-core/src/mgr/commands/refresh.rs | 3 ++- yazi-core/src/tasks/commands/inspect.rs | 3 ++- yazi-fm/src/app/commands/render.rs | 3 ++- yazi-fm/src/main.rs | 2 ++ yazi-fm/src/term.rs | 3 ++- yazi-plugin/Cargo.toml | 3 ++- yazi-plugin/src/clipboard.rs | 2 +- yazi-shared/Cargo.toml | 2 +- yazi-shared/src/lib.rs | 4 +--- yazi-term/Cargo.toml | 18 ++++++++++++++++-- yazi-term/src/lib.rs | 4 ++++ {yazi-shared => yazi-term}/src/tty/handle.rs | 0 {yazi-shared => yazi-term}/src/tty/mod.rs | 2 +- {yazi-shared => yazi-term}/src/tty/tty.rs | 0 {yazi-shared => yazi-term}/src/tty/windows.rs | 3 +-- yazi-widgets/Cargo.toml | 3 +++ 25 files changed, 60 insertions(+), 20 deletions(-) rename {yazi-shared => yazi-term}/src/tty/handle.rs (100%) rename {yazi-shared => yazi-term}/src/tty/mod.rs (65%) rename {yazi-shared => yazi-term}/src/tty/tty.rs (100%) rename {yazi-shared => yazi-term}/src/tty/windows.rs (99%) diff --git a/Cargo.lock b/Cargo.lock index 954e8a6e..8a1510de 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3244,6 +3244,7 @@ dependencies = [ "yazi-config", "yazi-macro", "yazi-shared", + "yazi-term", ] [[package]] @@ -3326,6 +3327,7 @@ dependencies = [ "yazi-fs", "yazi-macro", "yazi-shared", + "yazi-term", ] [[package]] @@ -3360,6 +3362,7 @@ dependencies = [ "yazi-proxy", "yazi-scheduler", "yazi-shared", + "yazi-term", "yazi-widgets", ] @@ -3503,6 +3506,7 @@ dependencies = [ "yazi-prebuilt", "yazi-proxy", "yazi-shared", + "yazi-term", ] [[package]] @@ -3572,7 +3576,12 @@ name = "yazi-term" version = "25.4.8" dependencies = [ "crossterm 0.29.0", + "libc", + "parking_lot", + "tracing", + "windows-sys 0.59.0", "yazi-macro", + "yazi-shared", ] [[package]] diff --git a/yazi-adapter/Cargo.toml b/yazi-adapter/Cargo.toml index 0b544e4a..a2925f84 100644 --- a/yazi-adapter/Cargo.toml +++ b/yazi-adapter/Cargo.toml @@ -12,6 +12,7 @@ repository = "https://github.com/sxyazi/yazi" yazi-config = { path = "../yazi-config", version = "25.4.8" } yazi-macro = { path = "../yazi-macro", version = "25.4.8" } yazi-shared = { path = "../yazi-shared", version = "25.4.8" } +yazi-term = { path = "../yazi-term", version = "25.4.8" } # External dependencies ansi-to-tui = { workspace = true } diff --git a/yazi-adapter/src/drivers/kgp_old.rs b/yazi-adapter/src/drivers/kgp_old.rs index e21bc6fa..fe4d58f1 100644 --- a/yazi-adapter/src/drivers/kgp_old.rs +++ b/yazi-adapter/src/drivers/kgp_old.rs @@ -5,7 +5,7 @@ use anyhow::Result; use base64::{Engine, engine::general_purpose}; use image::DynamicImage; use ratatui::layout::Rect; -use yazi_shared::tty::TTY; +use yazi_term::tty::TTY; use crate::{CLOSE, ESCAPE, Emulator, Image, START, adapter::Adapter}; diff --git a/yazi-adapter/src/emulator.rs b/yazi-adapter/src/emulator.rs index 41b8e2c2..fb9410b5 100644 --- a/yazi-adapter/src/emulator.rs +++ b/yazi-adapter/src/emulator.rs @@ -5,7 +5,8 @@ use crossterm::{cursor::{RestorePosition, SavePosition}, execute, style::Print, use scopeguard::defer; use tokio::time::sleep; use tracing::{debug, error, warn}; -use yazi_shared::{Either, tty::{Handle, TTY}}; +use yazi_shared::Either; +use yazi_term::tty::{Handle, TTY}; use crate::{Adapter, Brand, Dimension, Mux, TMUX, Unknown}; diff --git a/yazi-adapter/src/mux.rs b/yazi-adapter/src/mux.rs index a08acc79..07eb6001 100644 --- a/yazi-adapter/src/mux.rs +++ b/yazi-adapter/src/mux.rs @@ -1,7 +1,7 @@ use anyhow::Result; use tracing::error; use yazi_macro::time; -use yazi_shared::tty::TTY; +use yazi_term::tty::TTY; use crate::{CLOSE, ESCAPE, Emulator, START, TMUX}; diff --git a/yazi-config/Cargo.toml b/yazi-config/Cargo.toml index b201d0a6..79a898e2 100644 --- a/yazi-config/Cargo.toml +++ b/yazi-config/Cargo.toml @@ -13,6 +13,7 @@ yazi-codegen = { path = "../yazi-codegen", version = "25.4.8" } yazi-fs = { path = "../yazi-fs", version = "25.4.8" } yazi-macro = { path = "../yazi-macro", version = "25.4.8" } yazi-shared = { path = "../yazi-shared", version = "25.4.8" } +yazi-term = { path = "../yazi-term", version = "25.4.8" } # External dependencies anyhow = { workspace = true } diff --git a/yazi-config/src/lib.rs b/yazi-config/src/lib.rs index 2649a490..f5ccbdb7 100644 --- a/yazi-config/src/lib.rs +++ b/yazi-config/src/lib.rs @@ -6,7 +6,8 @@ yazi_macro::mod_flat!(layout pattern platform preset priority yazi); use std::io::{Read, Write}; -use yazi_shared::{RoCell, SyncCell, tty::TTY}; +use yazi_shared::{RoCell, SyncCell}; +use yazi_term::tty::TTY; pub static YAZI: RoCell = RoCell::new(); pub static KEYMAP: RoCell = RoCell::new(); diff --git a/yazi-core/Cargo.toml b/yazi-core/Cargo.toml index 6f333f0c..52cd981f 100644 --- a/yazi-core/Cargo.toml +++ b/yazi-core/Cargo.toml @@ -20,6 +20,7 @@ yazi-plugin = { path = "../yazi-plugin", version = "25.4.8" } yazi-proxy = { path = "../yazi-proxy", version = "25.4.8" } yazi-scheduler = { path = "../yazi-scheduler", version = "25.4.8" } yazi-shared = { path = "../yazi-shared", version = "25.4.8" } +yazi-term = { path = "../yazi-term", version = "25.4.8" } yazi-widgets = { path = "../yazi-widgets", version = "25.4.8" } # External dependencies diff --git a/yazi-core/src/mgr/commands/bulk_rename.rs b/yazi-core/src/mgr/commands/bulk_rename.rs index 48c81579..f70c1a70 100644 --- a/yazi-core/src/mgr/commands/bulk_rename.rs +++ b/yazi-core/src/mgr/commands/bulk_rename.rs @@ -8,7 +8,8 @@ use yazi_config::YAZI; use yazi_dds::Pubsub; use yazi_fs::{File, FilesOp, max_common_root, maybe_exists, paths_to_same_file}; use yazi_proxy::{AppProxy, HIDER, TasksProxy, WATCHER}; -use yazi_shared::{terminal_clear, tty::TTY, url::Url}; +use yazi_shared::{terminal_clear, url::Url}; +use yazi_term::tty::TTY; use crate::mgr::Mgr; diff --git a/yazi-core/src/mgr/commands/refresh.rs b/yazi-core/src/mgr/commands/refresh.rs index e2fe4341..8b32983d 100644 --- a/yazi-core/src/mgr/commands/refresh.rs +++ b/yazi-core/src/mgr/commands/refresh.rs @@ -1,7 +1,8 @@ use crossterm::{execute, terminal::SetTitle}; use yazi_config::YAZI; use yazi_fs::CWD; -use yazi_shared::{event::CmdCow, tty::TTY}; +use yazi_shared::event::CmdCow; +use yazi_term::tty::TTY; use crate::{mgr::Mgr, tasks::Tasks}; diff --git a/yazi-core/src/tasks/commands/inspect.rs b/yazi-core/src/tasks/commands/inspect.rs index 78b97771..c363be78 100644 --- a/yazi-core/src/tasks/commands/inspect.rs +++ b/yazi-core/src/tasks/commands/inspect.rs @@ -4,7 +4,8 @@ use crossterm::{execute, terminal::{disable_raw_mode, enable_raw_mode}}; use scopeguard::defer; use tokio::{io::{AsyncReadExt, stdin}, select, sync::mpsc, time}; use yazi_proxy::{AppProxy, HIDER}; -use yazi_shared::{event::CmdCow, terminal_clear, tty::TTY}; +use yazi_shared::{event::CmdCow, terminal_clear}; +use yazi_term::tty::TTY; use crate::tasks::Tasks; diff --git a/yazi-fm/src/app/commands/render.rs b/yazi-fm/src/app/commands/render.rs index a7e2fadd..b0382de7 100644 --- a/yazi-fm/src/app/commands/render.rs +++ b/yazi-fm/src/app/commands/render.rs @@ -3,7 +3,8 @@ use std::sync::atomic::{AtomicU8, Ordering}; use crossterm::{cursor::{MoveTo, SetCursorStyle, Show}, execute, queue, terminal::{BeginSynchronizedUpdate, EndSynchronizedUpdate}}; use ratatui::{CompletedFrame, backend::{Backend, CrosstermBackend}, buffer::Buffer, layout::Position}; use yazi_plugin::elements::COLLISION; -use yazi_shared::{event::NEED_RENDER, tty::TTY}; +use yazi_shared::event::NEED_RENDER; +use yazi_term::tty::TTY; use crate::{app::App, lives::Lives, root::Root}; diff --git a/yazi-fm/src/main.rs b/yazi-fm/src/main.rs index 53fa047a..35c8e6a0 100644 --- a/yazi-fm/src/main.rs +++ b/yazi-fm/src/main.rs @@ -16,6 +16,8 @@ async fn main() -> anyhow::Result<()> { Logs::start()?; _ = fdlimit::raise_fd_limit(); + yazi_term::init(); + yazi_fs::init(); yazi_config::init()?; diff --git a/yazi-fm/src/term.rs b/yazi-fm/src/term.rs index 1f57c4a6..c715c3ee 100644 --- a/yazi-fm/src/term.rs +++ b/yazi-fm/src/term.rs @@ -5,7 +5,8 @@ use crossterm::{Command, event::{DisableBracketedPaste, DisableMouseCapture, Ena use ratatui::{CompletedFrame, Frame, Terminal, backend::CrosstermBackend, buffer::Buffer, layout::Rect}; use yazi_adapter::{Emulator, Mux, TMUX}; use yazi_config::YAZI; -use yazi_shared::{SyncCell, tty::{TTY, TtyWriter}}; +use yazi_shared::SyncCell; +use yazi_term::tty::{TTY, TtyWriter}; static CSI_U: AtomicBool = AtomicBool::new(false); diff --git a/yazi-plugin/Cargo.toml b/yazi-plugin/Cargo.toml index 16fb1787..d4ba8f26 100644 --- a/yazi-plugin/Cargo.toml +++ b/yazi-plugin/Cargo.toml @@ -22,6 +22,7 @@ yazi-fs = { path = "../yazi-fs", version = "25.4.8" } yazi-macro = { path = "../yazi-macro", version = "25.4.8" } yazi-proxy = { path = "../yazi-proxy", version = "25.4.8" } yazi-shared = { path = "../yazi-shared", version = "25.4.8" } +yazi-term = { path = "../yazi-term", version = "25.4.8" } # External dependencies ansi-to-tui = { workspace = true } @@ -51,7 +52,7 @@ uzers = { workspace = true } [target."cfg(windows)".dependencies] clipboard-win = "5.4.0" -windows-sys = { version = "0.59.0", features = [ "Win32_Security", "Win32_System_JobObjects", "Win32_System_Threading" ] } +windows-sys = { version = "0.59.0", features = [ "Win32_System_JobObjects" ] } [target.'cfg(target_os = "macos")'.dependencies] crossterm = { workspace = true, features = [ "use-dev-tty", "libc" ] } diff --git a/yazi-plugin/src/clipboard.rs b/yazi-plugin/src/clipboard.rs index a5d3642c..cdb5bbe9 100644 --- a/yazi-plugin/src/clipboard.rs +++ b/yazi-plugin/src/clipboard.rs @@ -59,7 +59,7 @@ impl Clipboard { use crossterm::execute; use tokio::{io::AsyncWriteExt, process::Command}; - use yazi_shared::tty::TTY; + use yazi_term::tty::TTY; s.as_ref().clone_into(&mut self.content.lock()); execute!(TTY.writer(), osc52::SetClipboard::new(s.as_ref())).ok(); diff --git a/yazi-shared/Cargo.toml b/yazi-shared/Cargo.toml index 453609e2..18d9aa24 100644 --- a/yazi-shared/Cargo.toml +++ b/yazi-shared/Cargo.toml @@ -29,7 +29,7 @@ libc = { workspace = true } uzers = { workspace = true } [target.'cfg(windows)'.dependencies] -windows-sys = { version = "0.59.0", features = [ "Win32_Globalization", "Win32_Security", "Win32_Storage_FileSystem", "Win32_System_Console", "Win32_System_IO", "Win32_System_Threading", "Win32_UI_Shell" ] } +windows-sys = { version = "0.59.0", features = [ "Win32_UI_Shell" ] } [target.'cfg(target_os = "macos")'.dependencies] crossterm = { workspace = true, features = [ "use-dev-tty", "libc" ] } diff --git a/yazi-shared/src/lib.rs b/yazi-shared/src/lib.rs index abc04594..cdebe1a4 100644 --- a/yazi-shared/src/lib.rs +++ b/yazi-shared/src/lib.rs @@ -1,12 +1,10 @@ #![allow(clippy::option_map_unit_fn)] -yazi_macro::mod_pub!(errors event shell theme translit tty url); +yazi_macro::mod_pub!(errors event shell theme translit url); yazi_macro::mod_flat!(chars condition debounce either env id layer natsort number os rand ro_cell sync_cell terminal throttle time utf8); pub fn init() { - tty::init(); - LOG_LEVEL.replace(<_>::from(std::env::var("YAZI_LOG").unwrap_or_default())); #[cfg(unix)] diff --git a/yazi-term/Cargo.toml b/yazi-term/Cargo.toml index b4bd5bad..69c080ec 100644 --- a/yazi-term/Cargo.toml +++ b/yazi-term/Cargo.toml @@ -10,7 +10,21 @@ repository = "https://github.com/sxyazi/yazi" rust-version = "1.85.0" [dependencies] -yazi-macro = { path = "../yazi-macro", version = "25.4.8" } +yazi-macro = { path = "../yazi-macro", version = "25.4.8" } +yazi-shared = { path = "../yazi-shared", version = "25.4.8" } + +# Logging +tracing = { workspace = true } # External dependencies -crossterm = { workspace = true } +crossterm = { workspace = true } +parking_lot = { workspace = true } + +[target."cfg(unix)".dependencies] +libc = { workspace = true } + +[target.'cfg(target_os = "macos")'.dependencies] +crossterm = { workspace = true, features = [ "use-dev-tty", "libc" ] } + +[target.'cfg(windows)'.dependencies] +windows-sys = { version = "0.59.0", features = [ "Win32_Globalization", "Win32_Storage_FileSystem", "Win32_System_IO", "Win32_System_Console", "Win32_System_Threading" ] } diff --git a/yazi-term/src/lib.rs b/yazi-term/src/lib.rs index 72748161..b57edfd2 100644 --- a/yazi-term/src/lib.rs +++ b/yazi-term/src/lib.rs @@ -1,3 +1,7 @@ #![allow(clippy::unit_arg)] +yazi_macro::mod_pub!(tty); + yazi_macro::mod_flat!(cursor if_); + +pub fn init() { tty::init(); } diff --git a/yazi-shared/src/tty/handle.rs b/yazi-term/src/tty/handle.rs similarity index 100% rename from yazi-shared/src/tty/handle.rs rename to yazi-term/src/tty/handle.rs diff --git a/yazi-shared/src/tty/mod.rs b/yazi-term/src/tty/mod.rs similarity index 65% rename from yazi-shared/src/tty/mod.rs rename to yazi-term/src/tty/mod.rs index faef2c9c..b02aab83 100644 --- a/yazi-shared/src/tty/mod.rs +++ b/yazi-term/src/tty/mod.rs @@ -3,6 +3,6 @@ yazi_macro::mod_flat!(handle tty); #[cfg(windows)] yazi_macro::mod_flat!(windows); -pub static TTY: crate::RoCell = crate::RoCell::new(); +pub static TTY: yazi_shared::RoCell = yazi_shared::RoCell::new(); pub(super) fn init() { TTY.with(<_>::default); } diff --git a/yazi-shared/src/tty/tty.rs b/yazi-term/src/tty/tty.rs similarity index 100% rename from yazi-shared/src/tty/tty.rs rename to yazi-term/src/tty/tty.rs diff --git a/yazi-shared/src/tty/windows.rs b/yazi-term/src/tty/windows.rs similarity index 99% rename from yazi-shared/src/tty/windows.rs rename to yazi-term/src/tty/windows.rs index 72d8c2bd..7ee03691 100644 --- a/yazi-shared/src/tty/windows.rs +++ b/yazi-term/src/tty/windows.rs @@ -3,8 +3,7 @@ use std::{mem::MaybeUninit, os::windows::io::RawHandle, str}; use windows_sys::Win32::{Globalization::{CP_UTF8, MB_ERR_INVALID_CHARS, MultiByteToWideChar}, System::Console::WriteConsoleW}; - -use crate::{floor_char_boundary, utf8_char_width}; +use yazi_shared::{floor_char_boundary, utf8_char_width}; // Apparently Windows doesn't handle large reads on stdin or writes to // stdout/stderr well (see #13304 for details). diff --git a/yazi-widgets/Cargo.toml b/yazi-widgets/Cargo.toml index ff15ece9..8e04168c 100644 --- a/yazi-widgets/Cargo.toml +++ b/yazi-widgets/Cargo.toml @@ -20,3 +20,6 @@ crossterm = { workspace = true } futures = { workspace = true } ratatui = { workspace = true } unicode-width = { workspace = true } + +[target.'cfg(target_os = "macos")'.dependencies] +crossterm = { workspace = true, features = [ "use-dev-tty", "libc" ] }