Remove wezterm fork from dependencie (#8998)
Improves build time by removing wezterm dependency ([#8604](https://github.com/zed-industries/zed/issues/8604)). Release Notes: - N/A
This commit is contained in:
parent
e7289c385d
commit
41d8ba12ec
8 changed files with 183 additions and 110 deletions
|
@ -22,13 +22,13 @@ dirs = "4.0.0"
|
|||
futures.workspace = true
|
||||
gpui.workspace = true
|
||||
libc = "0.2"
|
||||
procinfo = { git = "https://github.com/zed-industries/wezterm", rev = "0c13436f4fa8b126f46dd4a20106419b41666897", default-features = false }
|
||||
task.workspace = true
|
||||
schemars.workspace = true
|
||||
serde.workspace = true
|
||||
serde_derive.workspace = true
|
||||
serde_json.workspace = true
|
||||
settings.workspace = true
|
||||
sysinfo.workspace = true
|
||||
smol.workspace = true
|
||||
theme.workspace = true
|
||||
thiserror.workspace = true
|
||||
|
|
134
crates/terminal/src/pty_info.rs
Normal file
134
crates/terminal/src/pty_info.rs
Normal file
|
@ -0,0 +1,134 @@
|
|||
use alacritty_terminal::tty::Pty;
|
||||
#[cfg(target_os = "windows")]
|
||||
use std::num::NonZeroU32;
|
||||
#[cfg(unix)]
|
||||
use std::os::fd::AsRawFd;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
use windows::Win32::{Foundation::HANDLE, System::Threading::GetProcessId};
|
||||
|
||||
use sysinfo::{Pid, Process, ProcessRefreshKind, RefreshKind, System, UpdateKind};
|
||||
|
||||
struct ProcessIdGetter {
|
||||
handle: i32,
|
||||
fallback_pid: u32,
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
impl ProcessIdGetter {
|
||||
fn new(pty: &Pty) -> ProcessIdGetter {
|
||||
ProcessIdGetter {
|
||||
handle: pty.file().as_raw_fd(),
|
||||
fallback_pid: pty.child().id(),
|
||||
}
|
||||
}
|
||||
|
||||
fn pid(&self) -> Option<Pid> {
|
||||
let pid = unsafe { libc::tcgetpgrp(self.handle) };
|
||||
if pid < 0 {
|
||||
return Some(Pid::from_u32(self.fallback_pid));
|
||||
}
|
||||
Some(Pid::from_u32(pid as u32))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
impl ProcessIdGetter {
|
||||
fn new(pty: &Pty) -> ProcessIdGetter {
|
||||
let child = pty.child_watcher();
|
||||
let handle = child.raw_handle();
|
||||
let fallback_pid = child
|
||||
.pid()
|
||||
.unwrap_or_else(|| unsafe { NonZeroU32::new_unchecked(GetProcessId(HANDLE(handle))) });
|
||||
|
||||
ProcessIdGetter {
|
||||
handle: handle as i32,
|
||||
fallback_pid: u32::from(fallback_pid),
|
||||
}
|
||||
}
|
||||
|
||||
fn pid(&self) -> Option<Pid> {
|
||||
let pid = unsafe { GetProcessId(HANDLE(self.handle as _)) };
|
||||
// the GetProcessId may fail and returns zero, which will lead to a stack overflow issue
|
||||
if pid == 0 {
|
||||
// in the builder process, there is a small chance, almost negligible,
|
||||
// that this value could be zero, which means child_watcher returns None,
|
||||
// GetProcessId returns 0.
|
||||
if self.fallback_pid == 0 {
|
||||
return None;
|
||||
}
|
||||
return Some(Pid::from_u32(self.fallback_pid));
|
||||
}
|
||||
Some(Pid::from_u32(pid as u32))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct ProcessInfo {
|
||||
pub name: String,
|
||||
pub cwd: PathBuf,
|
||||
pub argv: Vec<String>,
|
||||
}
|
||||
|
||||
/// Fetches Zed-relevant Pseudo-Terminal (PTY) process information
|
||||
pub struct PtyProcessInfo {
|
||||
system: System,
|
||||
refresh_kind: ProcessRefreshKind,
|
||||
pid_getter: ProcessIdGetter,
|
||||
pub current: Option<ProcessInfo>,
|
||||
}
|
||||
|
||||
impl PtyProcessInfo {
|
||||
pub fn new(pty: &Pty) -> PtyProcessInfo {
|
||||
let process_refresh_kind = ProcessRefreshKind::new()
|
||||
.with_cmd(UpdateKind::Always)
|
||||
.with_cwd(UpdateKind::Always)
|
||||
.with_exe(UpdateKind::Always);
|
||||
let refresh_kind = RefreshKind::new().with_processes(process_refresh_kind);
|
||||
let system = System::new_with_specifics(refresh_kind);
|
||||
|
||||
PtyProcessInfo {
|
||||
system,
|
||||
refresh_kind: process_refresh_kind,
|
||||
pid_getter: ProcessIdGetter::new(pty),
|
||||
current: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn refresh(&mut self) -> Option<&Process> {
|
||||
let pid = self.pid_getter.pid()?;
|
||||
self.system.refresh_processes_specifics(self.refresh_kind);
|
||||
self.system.process(pid)
|
||||
}
|
||||
|
||||
fn load(&mut self) -> Option<ProcessInfo> {
|
||||
let process = self.refresh()?;
|
||||
let cwd = process
|
||||
.cwd()
|
||||
.take()
|
||||
.map_or(PathBuf::new(), |p| p.to_owned());
|
||||
|
||||
let info = ProcessInfo {
|
||||
name: process.name().to_owned(),
|
||||
cwd,
|
||||
argv: process.cmd().to_vec(),
|
||||
};
|
||||
self.current = Some(info.clone());
|
||||
Some(info)
|
||||
}
|
||||
|
||||
/// Updates the cached process info, returns whether the Zed-relevant info has changed
|
||||
pub fn has_changed(&mut self) -> bool {
|
||||
let current = self.load();
|
||||
let has_changed = match (self.current.as_ref(), current.as_ref()) {
|
||||
(None, None) => false,
|
||||
(Some(prev), Some(now)) => prev.cwd != now.cwd || prev.name != now.name,
|
||||
_ => true,
|
||||
};
|
||||
if has_changed {
|
||||
self.current = current;
|
||||
}
|
||||
has_changed
|
||||
}
|
||||
}
|
|
@ -2,6 +2,7 @@ pub mod mappings;
|
|||
|
||||
pub use alacritty_terminal;
|
||||
|
||||
mod pty_info;
|
||||
pub mod terminal_settings;
|
||||
|
||||
use alacritty_terminal::{
|
||||
|
@ -34,18 +35,14 @@ use mappings::mouse::{
|
|||
|
||||
use collections::{HashMap, VecDeque};
|
||||
use futures::StreamExt;
|
||||
use procinfo::LocalProcessInfo;
|
||||
use pty_info::PtyProcessInfo;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use settings::Settings;
|
||||
use smol::channel::{Receiver, Sender};
|
||||
#[cfg(target_os = "windows")]
|
||||
use std::num::NonZeroU32;
|
||||
use task::TaskId;
|
||||
use terminal_settings::{AlternateScroll, Shell, TerminalBlink, TerminalSettings};
|
||||
use theme::{ActiveTheme, Theme};
|
||||
use util::truncate_and_trailoff;
|
||||
#[cfg(target_os = "windows")]
|
||||
use windows::Win32::{Foundation::HANDLE, System::Threading::GetProcessId};
|
||||
|
||||
use std::{
|
||||
cmp::{self, min},
|
||||
|
@ -57,9 +54,6 @@ use std::{
|
|||
};
|
||||
use thiserror::Error;
|
||||
|
||||
#[cfg(unix)]
|
||||
use std::os::unix::prelude::AsRawFd;
|
||||
|
||||
use gpui::{
|
||||
actions, black, px, AnyWindowHandle, AppContext, Bounds, ClipboardItem, EventEmitter, Hsla,
|
||||
Keystroke, ModelContext, Modifiers, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent,
|
||||
|
@ -401,19 +395,7 @@ impl TerminalBuilder {
|
|||
}
|
||||
};
|
||||
|
||||
#[cfg(unix)]
|
||||
let (fd, shell_pid) = (pty.file().as_raw_fd(), pty.child().id());
|
||||
|
||||
// todo("windows")
|
||||
#[cfg(windows)]
|
||||
let (fd, shell_pid) = {
|
||||
let child = pty.child_watcher();
|
||||
let handle = child.raw_handle();
|
||||
let pid = child.pid().unwrap_or_else(|| unsafe {
|
||||
NonZeroU32::new_unchecked(GetProcessId(HANDLE(handle)))
|
||||
});
|
||||
(handle, u32::from(pid))
|
||||
};
|
||||
let pty_info = PtyProcessInfo::new(&pty);
|
||||
|
||||
//And connect them together
|
||||
let event_loop = EventLoop::new(
|
||||
|
@ -441,9 +423,7 @@ impl TerminalBuilder {
|
|||
last_mouse: None,
|
||||
matches: Vec::new(),
|
||||
selection_head: None,
|
||||
shell_fd: fd as u32,
|
||||
shell_pid,
|
||||
foreground_process_info: None,
|
||||
pty_info,
|
||||
breadcrumb_text: String::new(),
|
||||
scroll_px: px(0.),
|
||||
last_mouse_position: None,
|
||||
|
@ -598,9 +578,7 @@ pub struct Terminal {
|
|||
pub last_content: TerminalContent,
|
||||
pub selection_head: Option<AlacPoint>,
|
||||
pub breadcrumb_text: String,
|
||||
shell_pid: u32,
|
||||
shell_fd: u32,
|
||||
pub foreground_process_info: Option<LocalProcessInfo>,
|
||||
pub pty_info: PtyProcessInfo,
|
||||
scroll_px: Pixels,
|
||||
next_link_id: usize,
|
||||
selection_phase: SelectionPhase,
|
||||
|
@ -660,7 +638,7 @@ impl Terminal {
|
|||
AlacTermEvent::Wakeup => {
|
||||
cx.emit(Event::Wakeup);
|
||||
|
||||
if self.update_process_info() {
|
||||
if self.pty_info.has_changed() {
|
||||
cx.emit(Event::TitleChanged);
|
||||
}
|
||||
}
|
||||
|
@ -675,56 +653,8 @@ impl Terminal {
|
|||
self.selection_phase == SelectionPhase::Selecting
|
||||
}
|
||||
|
||||
/// Updates the cached process info, returns whether the Zed-relevant info has changed
|
||||
fn update_process_info(&mut self) -> bool {
|
||||
#[cfg(unix)]
|
||||
let pid = {
|
||||
let ret = unsafe { libc::tcgetpgrp(self.shell_fd as i32) };
|
||||
if ret < 0 {
|
||||
self.shell_pid as i32
|
||||
} else {
|
||||
ret
|
||||
}
|
||||
};
|
||||
|
||||
#[cfg(windows)]
|
||||
let pid = {
|
||||
let ret = unsafe { GetProcessId(HANDLE(self.shell_fd as _)) };
|
||||
// the GetProcessId may fail and returns zero, which will lead to a stack overflow issue
|
||||
if ret == 0 {
|
||||
// in the builder process, there is a small chance, almost negligible,
|
||||
// that this value could be zero, which means child_watcher returns None,
|
||||
// GetProcessId returns 0.
|
||||
if self.shell_pid == 0 {
|
||||
return false;
|
||||
}
|
||||
self.shell_pid
|
||||
} else {
|
||||
ret
|
||||
}
|
||||
} as i32;
|
||||
|
||||
if let Some(process_info) = LocalProcessInfo::with_root_pid(pid as u32) {
|
||||
let res = self
|
||||
.foreground_process_info
|
||||
.as_ref()
|
||||
.map(|old_info| {
|
||||
process_info.cwd != old_info.cwd || process_info.name != old_info.name
|
||||
})
|
||||
.unwrap_or(true);
|
||||
|
||||
self.foreground_process_info = Some(process_info.clone());
|
||||
|
||||
res
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fn get_cwd(&self) -> Option<PathBuf> {
|
||||
self.foreground_process_info
|
||||
.as_ref()
|
||||
.map(|info| info.cwd.clone())
|
||||
pub fn get_cwd(&self) -> Option<PathBuf> {
|
||||
self.pty_info.current.as_ref().map(|info| info.cwd.clone())
|
||||
}
|
||||
|
||||
///Takes events from Alacritty and translates them to behavior on this view
|
||||
|
@ -1402,7 +1332,8 @@ impl Terminal {
|
|||
}
|
||||
}
|
||||
None => self
|
||||
.foreground_process_info
|
||||
.pty_info
|
||||
.current
|
||||
.as_ref()
|
||||
.map(|fpi| {
|
||||
let process_file = fpi
|
||||
|
@ -1410,11 +1341,13 @@ impl Terminal {
|
|||
.file_name()
|
||||
.map(|name| name.to_string_lossy().to_string())
|
||||
.unwrap_or_default();
|
||||
|
||||
let argv = fpi.argv.clone();
|
||||
let process_name = format!(
|
||||
"{}{}",
|
||||
fpi.name,
|
||||
if fpi.argv.len() >= 1 {
|
||||
format!(" {}", (fpi.argv[1..]).join(" "))
|
||||
if argv.len() >= 1 {
|
||||
format!(" {}", (argv[1..]).join(" "))
|
||||
} else {
|
||||
"".to_string()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue