use alacritty_terminal::Term;
use alacritty_terminal::event::{Event as AlacEvent, EventListener};
use alacritty_terminal::grid::{Dimensions, GridCell, Row, Scroll};
use alacritty_terminal::index::{Column, Line, Point, Side};
use alacritty_terminal::selection::{Selection as TracerSelection, SelectionType};
use alacritty_terminal::sync::FairMutex;
use alacritty_terminal::term::ClipboardType;
use alacritty_terminal::term::cell::{Cell, Flags};
use alacritty_terminal::term::test::TermSize;
use alacritty_terminal::term::{Config, TermMode};
use alacritty_terminal::vte::ansi::{Color as AnsiColor, NamedColor, Processor, StdSyncHandler};
use anyhow::{Context, Result};
use portable_pty::{CommandBuilder, MasterPty, PtySize, native_pty_system};
use ratatui::{
buffer::Buffer,
layout::Rect,
style::{Color, Modifier, Style},
widgets::{Block, Borders, Widget},
};
use std::io::{Read, Write};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
mod annotations;
mod backend;
mod captures;
mod clock;
mod commands;
mod images;
mod input;
mod mouse;
mod overlays;
mod processes;
mod render;
mod scroll;
mod selection;
mod shell_env;
mod spawn;
mod status;
mod text;
use crate::proc::info::{cwd_of_pid, foreground_group_of};
use backend::*;
use captures::*;
use clock::*;
pub use commands::CommandDecoration;
pub use commands::PromptJump;
use commands::*;
pub use images::SIXEL_HOST;
use images::*;
pub use mouse::MouseAction;
pub use mouse::MouseButtonKind;
pub use mouse::MouseMods;
#[cfg(test)]
use mouse::*;
pub use overlays::HintSpan;
pub use processes::CLEAR_TYPED_LINE;
#[cfg(test)]
use processes::*;
pub(crate) use scroll::resize_term;
use scroll::scroll_line_to_top;
#[cfg(test)]
use scroll::*;
pub use selection::Selection;
pub use selection::is_terminal_word_char;
use selection::*;
pub(crate) use shell_env::apply_pane_env;
pub use shell_env::parse_shells;
#[cfg(test)]
pub(crate) use shell_env::sequence_editor_command;
use shell_env::*;
pub use spawn::publish_pane_settings;
#[cfg(test)]
use status::*;
pub use text::FindLine;
pub(crate) use text::ansi_rows_from;
use text::*;
const SCROLLBACK_LINES: usize = 5000;
struct AltSelAnchor {
rows: Vec<String>,
top: i32,
text: String,
dormant: bool,
visible: Option<Vec<(i32, u16, u16)>>,
ctx_above: Option<String>,
ctx_below: Option<String>,
}
#[derive(Clone, Default)]
pub struct VoidListener {
pty_response_tx: Option<std::sync::mpsc::Sender<String>>,
size: Option<Arc<std::sync::Mutex<(u16, u16)>>>,
bell: Option<Arc<AtomicBool>>,
clipboard: Option<Arc<std::sync::Mutex<Option<String>>>>,
}
#[derive(Clone, Debug)]
struct ScrollClock {
base: i64,
planted: Option<i32>,
hist: i64,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct Wipe {
end: usize,
screen: bool,
hist: bool,
}
struct PaneAnnotation {
line_rec: i32,
clock_rec: i64,
start: u16,
len: u16,
text: String,
}
pub struct PtyTerminal {
term: Arc<FairMutex<Term<VoidListener>>>,
pty_dirty: Arc<AtomicBool>,
pty_pending_bytes: Arc<AtomicUsize>,
last_output_ms: Arc<std::sync::atomic::AtomicU64>,
agent: Option<crate::agents::AgentLane>,
program_status: Arc<std::sync::Mutex<crate::program_status::Records>>,
status_mark: u64,
status_seated: u64,
port_rx: std::sync::mpsc::Receiver<crate::port_detect::PortHit>,
backend: PaneBackend,
input_tx: std::sync::mpsc::Sender<Vec<u8>>,
reader_thread: Option<std::thread::JoinHandle<()>>,
reader_shutdown: Option<std::io::PipeWriter>,
shell_pid: Option<i32>,
exit: Option<Option<i32>>,
reaped_at: Option<std::time::Instant>,
uid: u64,
input_seen: bool,
last_input_ms: u64,
run_pane: bool,
manual_name_seen: bool,
cols: u16,
rows: u16,
size_shared: Arc<std::sync::Mutex<(u16, u16)>>,
pub focused: bool,
pub broadcast_excluded: bool,
pub collapsed: bool,
pub focus_gradient: bool,
pub theme: crate::theme::Theme,
pub last_area: Rect,
pub last_inner: Rect,
selection: Option<Selection>,
copy_cursor: Option<(i32, u16)>,
sel_scrolled: i64,
clock: Arc<std::sync::Mutex<ScrollClock>>,
alt_sel: Option<AltSelAnchor>,
drag_selecting: bool,
manual_name: Option<String>,
auto_label: String,
search_needle: Option<String>,
search_opts: crate::search::SearchOpts,
current_match: Option<(i64, i32, usize, usize)>,
bell: Arc<AtomicBool>,
clipboard_store: Arc<std::sync::Mutex<Option<String>>>,
marks: Arc<std::sync::Mutex<Vec<StoredMark>>>,
osc7_cwd: Arc<std::sync::Mutex<Option<std::path::PathBuf>>>,
osc7_host: Arc<std::sync::Mutex<Option<String>>>,
pub accent: Option<(u8, u8, u8)>,
pub accent_badge: Option<String>,
notifications: Arc<std::sync::Mutex<Vec<String>>>,
progress: Arc<std::sync::Mutex<Option<(u8, u8)>>>,
line_times: Arc<std::sync::Mutex<std::collections::BTreeMap<i64, u64>>>,
pub show_timestamps: bool,
pub reveal_redactions: bool,
pub redacted_on_screen: usize,
annotations: Vec<PaneAnnotation>,
finished_rx: std::sync::mpsc::Receiver<FinishedCommand>,
hints: Option<(i64, Vec<HintSpan>)>,
triggers: Arc<std::sync::Mutex<std::sync::Arc<crate::triggers::TriggerSet>>>,
trigger_rx: std::sync::mpsc::Receiver<crate::triggers::TriggerHit>,
watch_set: Arc<std::sync::Mutex<std::sync::Arc<crate::problem_matchers::WatchSet>>>,
pane_watch:
Arc<std::sync::Mutex<Option<std::sync::Arc<crate::problem_matchers::CompiledMatcher>>>>,
#[allow(clippy::type_complexity)]
watch_rx: std::sync::mpsc::Receiver<(
Option<std::path::PathBuf>,
Vec<crate::build_matchers::BuildDiag>,
)>,
palette: [(u8, u8, u8); 16],
images: Arc<std::sync::Mutex<Vec<StoredImage>>>,
capture_rows: Arc<std::sync::Mutex<CaptureRows>>,
rewind: Arc<std::sync::Mutex<crate::rewind::RewindBuffer>>,
render_scratch: RenderScratch,
#[cfg(test)]
written_for_test: Arc<std::sync::Mutex<Vec<u8>>>,
#[cfg(test)]
finished_tx_for_test: std::sync::mpsc::Sender<FinishedCommand>,
}
#[derive(Default)]
struct RenderScratch {
row_text: String,
colmap: Vec<usize>,
logical: String,
trig_paint: render::TrigRowPaint,
hi_spans: Vec<crate::triggers::HighlightSpan>,
redact_spans: Vec<crate::triggers::RedactSpan>,
}
struct StoredImage {
seq: u64,
data: std::sync::Arc<Vec<u8>>,
line_rec: i32,
clock_rec: i64,
}
struct StoredMark {
id: u64,
kind: crate::shell_integration::OscEvent,
line_rec: i32,
clock_rec: i64,
col_rec: usize,
dur: Option<std::time::Duration>,
}
struct RunningCommand {
started: std::time::Instant,
typed: String,
out_line: i32,
out_clock: i64,
}
#[derive(Default)]
struct CaptureRows {
rows: std::collections::BTreeMap<u64, CapturedRow>,
line: u64,
}
struct CapturedRow {
row: Option<i64>,
text: String,
line: u64,
}
fn now_ms() -> u64 {
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_millis() as u64)
.unwrap_or(0)
}
impl PtyTerminal {
#[cfg(test)]
pub fn feed_bytes_for_test(&self, bytes: &[u8]) {
let mut p = Processor::<StdSyncHandler>::new();
let mut term = self.term.lock();
p.advance(&mut *term, bytes);
}
#[cfg(test)]
pub fn grid_lock_for_test(&self) -> Arc<FairMutex<Term<VoidListener>>> {
self.term.clone()
}
}
#[cfg(test)]
mod lock_probe {
use super::*;
type Grid = Arc<FairMutex<Term<VoidListener>>>;
type Asked = (
std::sync::Weak<FairMutex<Term<VoidListener>>>,
std::sync::mpsc::Sender<bool>,
);
static ASKED: std::sync::Mutex<Vec<Asked>> = std::sync::Mutex::new(Vec::new());
pub(super) fn ask(grid: &Grid) -> std::sync::mpsc::Receiver<bool> {
let (tx, rx) = std::sync::mpsc::channel();
ASKED.lock().unwrap().push((Arc::downgrade(grid), tx));
rx
}
pub(super) fn after_screen_work(grid: &Grid) {
let mut asked = ASKED.lock().unwrap();
asked.retain(|(pane, _)| pane.strong_count() > 0);
for (pane, tx) in asked.iter() {
if std::ptr::eq(pane.as_ptr(), Arc::as_ptr(grid)) {
let _ = tx.send(grid.try_lock_unfair().is_some());
}
}
}
}
struct PaneLink {
writer: Box<dyn Write + Send>,
reader: Box<dyn Read + Send>,
poll_fd: std::os::fd::RawFd,
shell_pid: Option<i32>,
backend: PaneBackend,
}
#[derive(Clone, Copy, Debug, Default)]
struct TrigCell {
fg: Option<Color>,
bg: Option<Color>,
mask: bool,
}
#[cfg(test)]
mod tests;