forked from herdrdev/herdr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterminal.rs
More file actions
6124 lines (5474 loc) · 215 KB
/
Copy pathterminal.rs
File metadata and controls
6124 lines (5474 loc) · 215 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use std::borrow::Cow;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use bytes::Bytes;
use ratatui::style::{Color, Modifier, Style};
use ratatui::{layout::Rect, Frame};
use serde::{Deserialize, Serialize};
use tokio::sync::mpsc;
use tracing::{debug, error};
use unicode_width::UnicodeWidthStr;
use crate::layout::PaneId;
use crate::protocol::CellData;
#[cfg(windows)]
mod windows_recent_fallback;
use super::cursor::{CursorPositionSettleState, DecscusrTracker, CURSOR_POSITION_SETTLE};
use super::{
input::{
ghostty_key_event_from_terminal_key, ghostty_mouse_encoder_for_terminal,
ghostty_mouse_event_from_button_kind, ghostty_mouse_event_from_motion_kind,
ghostty_mouse_event_from_wheel_kind, ghostty_prefers_herdr_text_encoding,
},
kitty_keyboard::KittyKeyboardTracker,
osc::{
contains_scrollback_clear_sequence, current_transient_default_color_owner,
maybe_filter_primary_screen_scrollback_clear, parse_reported_cwd,
restore_host_terminal_theme_if_needed, write_host_terminal_theme_selective,
AgentOscStateTracker, DefaultColorEvent, DefaultColorEventTracker, DefaultColorOscTracker,
DefaultColorQuery, DefaultColorTrackedEvent, OscDebugTracker,
},
xtgettcap::{XtgettcapQueryTracker, XtgettcapResponse},
};
const DEFAULT_DETECTION_ROWS: usize = 24;
const KITTY_GRAPHICS_REDRAW_SETTLE: Duration = Duration::from_millis(20);
const CURSOR_POSITION_SETTLE_ENABLED: bool = cfg!(windows);
const MODE_MOUSE_X10: u16 = 9;
const MODE_MOUSE_PRESS_RELEASE: u16 = 1000;
const MODE_MOUSE_BUTTON_MOTION: u16 = 1002;
const MODE_MOUSE_ANY_MOTION: u16 = 1003;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ScrollMetrics {
pub offset_from_bottom: usize,
pub max_offset_from_bottom: usize,
pub viewport_rows: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) struct TerminalTextPoint {
pub row: u32,
pub col: u16,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct TerminalTextMatch {
pub start: TerminalTextPoint,
pub end: TerminalTextPoint,
pub source_fingerprint: u64,
pub scan_cols: u16,
pub scan_screen: crate::ghostty::ActiveScreen,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum TerminalWordMotion {
NextStart,
PreviousStart,
NextEnd,
}
const COPY_MODE_WORD_SEPARATORS: &str = "!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TerminalCursorState {
pub x: u16,
pub y: u16,
pub visible: bool,
/// DECSCUSR parameter (0–6). 0 means terminal default.
pub shape: u8,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct TerminalDirtyPatch {
pub rows: Vec<(u16, Vec<CellData>)>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum TerminalDirtyPatchOutcome {
Clean,
Patch(TerminalDirtyPatch),
Fallback,
}
fn decscusr_cursor_shape(style: crate::ghostty::CursorVisualStyle, blinking: bool) -> u8 {
match (style, blinking) {
(crate::ghostty::CursorVisualStyle::Block, true)
| (crate::ghostty::CursorVisualStyle::BlockHollow, true) => 1,
(crate::ghostty::CursorVisualStyle::Block, false)
| (crate::ghostty::CursorVisualStyle::BlockHollow, false) => 2,
(crate::ghostty::CursorVisualStyle::Underline, true) => 3,
(crate::ghostty::CursorVisualStyle::Underline, false) => 4,
(crate::ghostty::CursorVisualStyle::Bar, true) => 5,
(crate::ghostty::CursorVisualStyle::Bar, false) => 6,
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct InputState {
pub alternate_screen: bool,
pub application_cursor: bool,
pub bracketed_paste: bool,
pub focus_reporting: bool,
pub mouse_protocol_mode: crate::input::MouseProtocolMode,
pub mouse_protocol_encoding: crate::input::MouseProtocolEncoding,
pub mouse_alternate_scroll: bool,
#[serde(default)]
pub modify_other_keys: bool,
#[serde(default)]
pub color_scheme_reporting: bool,
}
impl InputState {
pub fn mouse_reporting_enabled(self) -> bool {
self.mouse_protocol_mode.reporting_enabled()
}
pub fn plain_page_keys_use_host_scrollback(self) -> bool {
!self.alternate_screen
&& !self.mouse_reporting_enabled()
// Bracketed paste distinguishes zsh's line editor (where it's on)
// from e.g. less -X (where it's off).
&& (!self.application_cursor || self.bracketed_paste)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ProcessBytesResult {
pub request_render: bool,
pub render_delay: Option<Duration>,
pub clipboard_writes: Vec<Vec<u8>>,
pub reported_cwd: Option<std::path::PathBuf>,
pub terminal_responses: Vec<Bytes>,
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub(crate) struct TerminalReadSnapshot {
pub text: String,
pub truncated: bool,
}
pub(crate) struct GhosttyPaneTerminal {
pub core: Mutex<GhosttyPaneCore>,
key_encoder: Mutex<crate::ghostty::KeyEncoder>,
pending_pty_responses: Arc<Mutex<Vec<Bytes>>>,
}
pub(crate) struct GhosttyPaneCore {
pub terminal: crate::ghostty::Terminal,
#[cfg(windows)]
recent_fallback: windows_recent_fallback::Cache,
pub render_state: crate::ghostty::RenderState,
pub kitty_keyboard: KittyKeyboardTracker,
pub initial_default_foreground: Option<crate::ghostty::RgbColor>,
pub initial_default_background: Option<crate::ghostty::RgbColor>,
pub host_terminal_theme: crate::terminal_theme::TerminalTheme,
pub transient_default_color_owner_pgid: Option<u32>,
pub default_color_tracker: DefaultColorOscTracker,
pub default_color_event_tracker: DefaultColorEventTracker,
pub child_default_foreground_changed: bool,
pub child_default_background_changed: bool,
pub osc_debug_tracker: OscDebugTracker,
pub agent_osc_state: AgentOscStateTracker,
pub xtgettcap_query_tracker: XtgettcapQueryTracker,
decscusr_tracker: DecscusrTracker,
cursor_settle_state: CursorPositionSettleState,
windows_powershell_prompt_cwd_reporting: bool,
}
pub(crate) struct PaneTerminal {
pub(crate) ghostty: GhosttyPaneTerminal,
}
impl PaneTerminal {
pub(crate) fn new(ghostty: GhosttyPaneTerminal) -> Self {
Self { ghostty }
}
pub fn process_pty_bytes(
&self,
pane_id: PaneId,
shell_pid: u32,
bytes: &[u8],
response_writer: &mpsc::Sender<Bytes>,
) -> ProcessBytesResult {
self.ghostty
.process_pty_bytes(pane_id, shell_pid, bytes, response_writer)
}
pub fn resize(
&self,
rows: u16,
cols: u16,
cell_width_px: u32,
cell_height_px: u32,
) -> Vec<Bytes> {
self.ghostty
.resize(rows, cols, cell_width_px, cell_height_px)
}
pub fn scroll_up(&self, lines: usize) {
self.ghostty.scroll_up(lines);
}
pub fn scroll_down(&self, lines: usize) {
self.ghostty.scroll_down(lines);
}
pub fn scroll_reset(&self) {
self.ghostty.scroll_reset();
}
pub fn set_scroll_offset_from_bottom(&self, lines: usize) {
self.ghostty.set_scroll_offset_from_bottom(lines);
}
pub fn scroll_metrics(&self) -> Option<ScrollMetrics> {
self.ghostty.scroll_metrics()
}
pub(crate) fn search_text_matches(
&self,
query: &str,
case_sensitive: bool,
) -> Vec<TerminalTextMatch> {
let Some((buffer, active_screen)) = self.retained_text_buffer() else {
return Vec::new();
};
buffer.search(query, case_sensitive, active_screen)
}
pub(crate) fn text_match_is_current(&self, text_match: TerminalTextMatch) -> bool {
self.text_matches_are_current(&[text_match])
.first()
.copied()
.unwrap_or(false)
}
pub(crate) fn text_matches_are_current(&self, text_matches: &[TerminalTextMatch]) -> Vec<bool> {
if text_matches.is_empty() {
return Vec::new();
}
let Ok(core) = self.ghostty.core.lock() else {
return vec![false; text_matches.len()];
};
let Some(cols) = core.terminal.cols().ok() else {
return vec![false; text_matches.len()];
};
let Some(active_screen) = core.terminal.active_screen().ok() else {
return vec![false; text_matches.len()];
};
let row_range = text_matches
.iter()
.filter(|text_match| {
text_match.scan_cols == cols && text_match.scan_screen == active_screen
})
.fold(None::<(u32, u32)>, |range, text_match| {
Some(match range {
Some((start_row, end_row)) => (
start_row.min(text_match.start.row),
end_row.max(text_match.end.row),
),
None => (text_match.start.row, text_match.end.row),
})
});
let Some((start_row, end_row)) = row_range else {
return vec![false; text_matches.len()];
};
let Ok(rows) = core
.terminal
.screen_text_rows_range(start_row as usize, end_row.saturating_add(1) as usize)
else {
return vec![false; text_matches.len()];
};
let buffer = RetainedTextBuffer::new_search(cols, rows, start_row);
text_matches
.iter()
.map(|text_match| {
text_match.scan_cols == cols
&& text_match.scan_screen == active_screen
&& buffer.contains_match(*text_match)
})
.collect()
}
pub(crate) fn word_motion_target(
&self,
row: u32,
col: u16,
motion: TerminalWordMotion,
) -> Option<TerminalTextPoint> {
let core = self.ghostty.core.lock().ok()?;
let cols = core.terminal.cols().ok()?;
let total_rows = core.terminal.total_rows().ok()?;
let row = usize::try_from(row).ok()?;
if row >= total_rows {
return None;
}
let mut window_rows = 64usize;
loop {
let (start_row, end_row) = match motion {
TerminalWordMotion::PreviousStart => {
(row.saturating_sub(window_rows.saturating_sub(1)), row + 1)
}
TerminalWordMotion::NextStart | TerminalWordMotion::NextEnd => {
(row, row.saturating_add(window_rows).min(total_rows))
}
};
let rows = core
.terminal
.screen_text_rows_range(start_row, end_row)
.ok()?;
let starts_in_continuation = rows
.first()
.is_some_and(|row| row.wrap_continuation && start_row > 0);
let ends_in_continuation = rows
.last()
.is_some_and(|row| row.soft_wrapped && end_row < total_rows);
let buffer = RetainedTextBuffer::new_words(cols, rows, u32::try_from(start_row).ok()?);
let target = buffer.word_motion(u32::try_from(row).ok()?, col, motion);
let needs_more_history = motion == TerminalWordMotion::PreviousStart
&& target
.is_some_and(|target| starts_in_continuation && target.row == start_row as u32);
let needs_more_future = motion == TerminalWordMotion::NextEnd
&& ends_in_continuation
&& target.is_some_and(|target| buffer.point_is_final_atom(target));
if target.is_some() && !needs_more_history && !needs_more_future {
return target;
}
let reached_edge = match motion {
TerminalWordMotion::PreviousStart => start_row == 0,
TerminalWordMotion::NextStart | TerminalWordMotion::NextEnd => {
end_row == total_rows
}
};
if reached_edge {
return target;
}
window_rows = window_rows.saturating_mul(2).min(total_rows);
}
}
fn retained_text_buffer(&self) -> Option<(RetainedTextBuffer, crate::ghostty::ActiveScreen)> {
let (cols, rows, active_screen) = {
let core = self.ghostty.core.lock().ok()?;
let cols = core.terminal.cols().ok()?;
let rows = core.terminal.screen_text_rows().ok()?;
let active_screen = core.terminal.active_screen().ok()?;
(cols, rows, active_screen)
};
Some((RetainedTextBuffer::new_search(cols, rows, 0), active_screen))
}
pub fn input_state(&self) -> Option<InputState> {
self.ghostty.input_state()
}
pub fn wheel_routing(&self) -> Option<crate::pane::WheelRouting> {
self.ghostty.wheel_routing()
}
pub(crate) fn screen_text_snapshot(
&self,
) -> Option<(
crate::ghostty::ActiveScreen,
u16,
Vec<crate::ghostty::ScreenTextRow>,
)> {
self.ghostty.screen_text_snapshot()
}
pub fn cursor_state(&self) -> Option<TerminalCursorState> {
self.ghostty.cursor_state()
}
pub fn synchronized_output_active(&self) -> bool {
self.ghostty.synchronized_output_active()
}
pub fn visible_text(&self) -> String {
self.ghostty.visible_text()
}
pub fn visible_ansi(&self) -> String {
self.ghostty.visible_ansi()
}
pub fn detection_text(&self) -> String {
self.ghostty.detection_text()
}
pub fn recent_text(&self, lines: usize) -> String {
self.ghostty.recent_text(lines)
}
pub(crate) fn recent_text_snapshot(&self, lines: usize) -> TerminalReadSnapshot {
self.ghostty.recent_text_snapshot(lines)
}
pub(crate) fn recent_ansi_snapshot(&self, lines: usize) -> TerminalReadSnapshot {
self.ghostty.recent_ansi_snapshot(lines)
}
pub(crate) fn recent_unwrapped_text_snapshot(&self, lines: usize) -> TerminalReadSnapshot {
self.ghostty.recent_unwrapped_text_snapshot(lines)
}
pub fn recent_unwrapped_ansi(&self, lines: usize) -> String {
self.ghostty.recent_unwrapped_ansi(lines)
}
pub(crate) fn recent_unwrapped_ansi_snapshot(&self, lines: usize) -> TerminalReadSnapshot {
self.ghostty.recent_unwrapped_ansi_snapshot(lines)
}
pub fn extract_selection(&self, selection: &crate::selection::Selection) -> Option<String> {
self.ghostty.extract_selection(selection)
}
pub fn render(&self, frame: &mut Frame, area: Rect, show_cursor: bool) {
self.ghostty.render(frame, area, show_cursor);
}
pub fn collect_dirty_patch(
&self,
area_width: u16,
area_height: u16,
) -> TerminalDirtyPatchOutcome {
self.ghostty.collect_dirty_patch(area_width, area_height)
}
pub fn visible_hyperlinks(&self, area: Rect) -> Vec<((u16, u16), String, String)> {
self.ghostty.visible_hyperlinks(area)
}
pub fn kitty_image_placements_with_data_filter<F>(
&self,
needs_data: F,
) -> Vec<crate::ghostty::KittyImagePlacement>
where
F: FnMut(crate::ghostty::KittyImageDescriptor) -> bool,
{
self.ghostty
.kitty_image_placements_with_data_filter(needs_data)
}
pub fn apply_host_terminal_theme(&self, theme: crate::terminal_theme::TerminalTheme) {
self.ghostty.apply_host_terminal_theme(theme);
}
pub fn apply_host_terminal_appearance(
&self,
appearance: Option<crate::terminal_theme::HostAppearance>,
) -> Option<Bytes> {
self.ghostty.apply_host_terminal_appearance(appearance)
}
pub fn has_transient_default_color_override(&self) -> bool {
self.ghostty.has_transient_default_color_override()
}
pub fn maybe_restore_host_terminal_theme(&self, pane_id: PaneId, shell_pid: u32) -> bool {
self.ghostty
.maybe_restore_host_terminal_theme(pane_id, shell_pid)
}
pub fn terminal_title(&self) -> Option<String> {
self.ghostty.terminal_title()
}
#[allow(dead_code)] // exposed for Stage C (detection loop wiring)
pub fn agent_osc_title(&self) -> String {
self.ghostty.agent_osc_title()
}
#[allow(dead_code)] // exposed for Stage C (detection loop wiring)
pub fn agent_osc_progress(&self) -> String {
self.ghostty.agent_osc_progress()
}
/// Clears retained OSC title/progress evidence on foreground agent change.
pub fn clear_agent_osc_state(&self) {
self.ghostty.clear_agent_osc_state()
}
pub fn keyboard_protocol(
&self,
fallback: crate::input::KeyboardProtocol,
) -> crate::input::KeyboardProtocol {
self.ghostty.keyboard_protocol().unwrap_or(fallback)
}
#[cfg(unix)]
pub fn kitty_keyboard_state_ansi(&self) -> Option<String> {
self.ghostty
.kitty_keyboard_state_ansi()
.filter(|ansi| !ansi.is_empty())
}
pub fn encode_terminal_key(
&self,
key: crate::input::TerminalKey,
protocol: crate::input::KeyboardProtocol,
) -> Vec<u8> {
self.ghostty.encode_terminal_key(key, protocol)
}
pub fn encode_mouse_button(
&self,
kind: crossterm::event::MouseEventKind,
column: u16,
row: u16,
modifiers: crossterm::event::KeyModifiers,
) -> Option<Vec<u8>> {
self.ghostty
.encode_mouse_button(kind, column, row, modifiers)
}
pub fn encode_mouse_motion(
&self,
kind: crossterm::event::MouseEventKind,
column: u16,
row: u16,
modifiers: crossterm::event::KeyModifiers,
) -> Option<Vec<u8>> {
self.ghostty
.encode_mouse_motion(kind, column, row, modifiers)
}
pub fn encode_mouse_wheel(
&self,
kind: crossterm::event::MouseEventKind,
column: u16,
row: u16,
modifiers: crossterm::event::KeyModifiers,
) -> Option<Vec<u8>> {
self.ghostty
.encode_mouse_wheel(kind, column, row, modifiers)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum TextClass {
Whitespace,
Separator,
Word,
}
#[derive(Debug)]
struct TextAtom {
point: Option<TerminalTextPoint>,
end_col: u16,
class: TextClass,
}
#[derive(Debug)]
struct TextSpan {
byte_start: usize,
byte_end: usize,
start: TerminalTextPoint,
end: TerminalTextPoint,
}
#[derive(Debug, Default)]
struct LogicalTextLine {
text: String,
spans: Vec<TextSpan>,
}
#[derive(Debug)]
struct RetainedTextBuffer {
cols: u16,
lines: Vec<LogicalTextLine>,
atoms: Vec<TextAtom>,
}
impl RetainedTextBuffer {
#[cfg(test)]
fn new(cols: u16, rows: Vec<crate::ghostty::ScreenTextRow>) -> Self {
Self::build(cols, rows, 0, true, true)
}
fn new_search(cols: u16, rows: Vec<crate::ghostty::ScreenTextRow>, row_offset: u32) -> Self {
Self::build(cols, rows, row_offset, true, false)
}
fn new_words(cols: u16, rows: Vec<crate::ghostty::ScreenTextRow>, row_offset: u32) -> Self {
Self::build(cols, rows, row_offset, false, true)
}
fn build(
cols: u16,
rows: Vec<crate::ghostty::ScreenTextRow>,
row_offset: u32,
build_lines: bool,
build_atoms: bool,
) -> Self {
let mut lines = Vec::new();
let mut line = LogicalTextLine::default();
let mut atoms: Vec<TextAtom> = Vec::new();
for (row_idx, row) in rows.into_iter().enumerate() {
let Some(row_idx) = u32::try_from(row_idx).ok() else {
break;
};
let row_idx = row_offset.saturating_add(row_idx);
for (col, cell) in row.cells.into_iter().enumerate() {
let Ok(col) = u16::try_from(col) else {
break;
};
if cell.wide == crate::ghostty::CellWide::SpacerTail {
continue;
}
if cell.wide == crate::ghostty::CellWide::SpacerHead {
if build_atoms {
atoms.push(TextAtom {
point: Some(TerminalTextPoint { row: row_idx, col }),
end_col: col,
class: atoms
.last()
.map_or(TextClass::Whitespace, |atom| atom.class),
});
}
continue;
}
let width = if cell.wide == crate::ghostty::CellWide::Wide {
2
} else {
1
};
let text = terminal_cell_text(&cell.graphemes);
let start = TerminalTextPoint { row: row_idx, col };
let end = TerminalTextPoint {
row: row_idx,
col: col.saturating_add(width - 1),
};
if build_lines {
let byte_start = line.text.len();
line.text.push_str(&text);
let byte_end = line.text.len();
line.spans.push(TextSpan {
byte_start,
byte_end,
start,
end,
});
}
if build_atoms {
atoms.push(TextAtom {
point: Some(start),
end_col: end.col,
class: text_class(&text),
});
}
}
if row.soft_wrapped {
continue;
}
if build_lines {
let trimmed_len = line.text.trim_end().len();
while line
.spans
.last()
.is_some_and(|span| span.byte_start >= trimmed_len)
{
line.spans.pop();
}
line.text.truncate(trimmed_len);
lines.push(std::mem::take(&mut line));
}
if build_atoms {
atoms.push(TextAtom {
point: None,
end_col: 0,
class: TextClass::Whitespace,
});
}
}
if build_lines && (!line.text.is_empty() || !line.spans.is_empty()) {
lines.push(line);
}
Self { cols, lines, atoms }
}
fn search(
&self,
query: &str,
case_sensitive: bool,
active_screen: crate::ghostty::ActiveScreen,
) -> Vec<TerminalTextMatch> {
if query.is_empty() {
return Vec::new();
}
let Ok(regex) = regex::RegexBuilder::new(®ex::escape(query))
.case_insensitive(!case_sensitive)
.build()
else {
return Vec::new();
};
let mut matches = Vec::new();
for line in &self.lines {
for found in regex.find_iter(&line.text) {
let Ok(start_index) = line
.spans
.binary_search_by_key(&found.start(), |span| span.byte_start)
else {
continue;
};
let Ok(end_index) = line
.spans
.binary_search_by_key(&found.end(), |span| span.byte_end)
else {
continue;
};
let start_span = &line.spans[start_index];
let end_span = &line.spans[end_index];
matches.push(TerminalTextMatch {
start: start_span.start,
end: end_span.end,
source_fingerprint: text_fingerprint(found.as_str()),
scan_cols: self.cols,
scan_screen: active_screen,
});
}
}
matches
}
fn contains_match(&self, text_match: TerminalTextMatch) -> bool {
self.lines.iter().any(|line| {
let Ok(start_index) = line
.spans
.binary_search_by_key(&text_match.start, |span| span.start)
else {
return false;
};
let Ok(end_index) = line
.spans
.binary_search_by_key(&text_match.end, |span| span.end)
else {
return false;
};
let start_span = &line.spans[start_index];
let end_span = &line.spans[end_index];
start_span.byte_start <= end_span.byte_end
&& text_fingerprint(&line.text[start_span.byte_start..end_span.byte_end])
== text_match.source_fingerprint
})
}
fn word_motion(
&self,
row: u32,
col: u16,
motion: TerminalWordMotion,
) -> Option<TerminalTextPoint> {
let current = self.atoms.iter().position(|atom| {
atom.point
.is_some_and(|point| point.row == row && col >= point.col && col <= atom.end_col)
})?;
match motion {
TerminalWordMotion::NextStart => self.next_word_start(current),
TerminalWordMotion::PreviousStart => self.previous_word_start(current),
TerminalWordMotion::NextEnd => self.next_word_end(current),
}
}
fn next_word_start(&self, current: usize) -> Option<TerminalTextPoint> {
let current_class = self.atoms.get(current)?.class;
let mut next = current.saturating_add(1);
if current_class != TextClass::Whitespace {
while self
.atoms
.get(next)
.is_some_and(|atom| atom.class == current_class)
{
next += 1;
}
}
while self
.atoms
.get(next)
.is_some_and(|atom| atom.class == TextClass::Whitespace)
{
next += 1;
}
self.next_point(next)
}
fn previous_word_start(&self, current: usize) -> Option<TerminalTextPoint> {
let mut previous = current.checked_sub(1)?;
while self
.atoms
.get(previous)
.is_some_and(|atom| atom.class == TextClass::Whitespace)
{
previous = previous.checked_sub(1)?;
}
let class = self.atoms.get(previous)?.class;
while previous > 0
&& self
.atoms
.get(previous - 1)
.is_some_and(|atom| atom.class == class)
{
previous -= 1;
}
self.previous_point(previous)
}
fn next_word_end(&self, current: usize) -> Option<TerminalTextPoint> {
let mut next = current.saturating_add(1);
while self
.atoms
.get(next)
.is_some_and(|atom| atom.class == TextClass::Whitespace)
{
next += 1;
}
let class = self.atoms.get(next)?.class;
while self
.atoms
.get(next + 1)
.is_some_and(|atom| atom.class == class)
{
next += 1;
}
self.previous_point(next)
}
fn next_point(&self, mut index: usize) -> Option<TerminalTextPoint> {
while let Some(atom) = self.atoms.get(index) {
if let Some(point) = atom.point {
return Some(point);
}
index += 1;
}
None
}
fn previous_point(&self, mut index: usize) -> Option<TerminalTextPoint> {
loop {
if let Some(point) = self.atoms.get(index)?.point {
return Some(point);
}
index = index.checked_sub(1)?;
}
}
fn point_is_final_atom(&self, point: TerminalTextPoint) -> bool {
// Word motion targets are atom start points, so compare against the
// final atom's start point. Comparing against `end_col` would never
// match a wide glyph, whose end column is one past its start.
self.atoms
.iter()
.rev()
.find(|atom| atom.point.is_some())
.is_some_and(|atom| atom.point == Some(point))
}
}
fn terminal_cell_text(graphemes: &[u32]) -> String {
if graphemes.is_empty()
|| graphemes.first().copied() == Some(crate::ghostty::KITTY_UNICODE_PLACEHOLDER)
{
return " ".to_string();
}
graphemes
.iter()
.map(|codepoint| char::from_u32(*codepoint).unwrap_or(char::REPLACEMENT_CHARACTER))
.collect()
}
fn text_class(text: &str) -> TextClass {
let Some(ch) = text.chars().next() else {
return TextClass::Whitespace;
};
if ch.is_whitespace() {
TextClass::Whitespace
} else if ch.is_ascii() && COPY_MODE_WORD_SEPARATORS.contains(ch) {
TextClass::Separator
} else {
TextClass::Word
}
}
fn text_fingerprint(text: &str) -> u64 {
let mut hasher = DefaultHasher::new();
text.hash(&mut hasher);
hasher.finish()
}
impl GhosttyPaneTerminal {
pub fn new(
mut terminal: crate::ghostty::Terminal,
_response_writer: mpsc::Sender<Bytes>,
) -> std::io::Result<Self> {
let pending_pty_responses = Arc::new(Mutex::new(Vec::new()));
let callback_responses = pending_pty_responses.clone();
terminal
.set_write_pty_callback(move |bytes| {
if let Ok(mut responses) = callback_responses.lock() {
responses.push(Bytes::copy_from_slice(bytes));
}
})
.map_err(|e| std::io::Error::other(e.to_string()))?;
let mut render_state =
crate::ghostty::RenderState::new().map_err(|e| std::io::Error::other(e.to_string()))?;
let initial_colors = render_state
.update(&terminal)
.ok()
.and_then(|_| render_state.colors().ok());
let initial_default_foreground = initial_colors.map(|colors| colors.foreground);
let initial_default_background = initial_colors.map(|colors| colors.background);
let mut key_encoder =
crate::ghostty::KeyEncoder::new().map_err(|e| std::io::Error::other(e.to_string()))?;
key_encoder.set_from_terminal(&terminal);
Ok(Self {
core: Mutex::new(GhosttyPaneCore {
terminal,
#[cfg(windows)]
recent_fallback: windows_recent_fallback::Cache::default(),
render_state,
kitty_keyboard: KittyKeyboardTracker::default(),
initial_default_foreground,
initial_default_background,
host_terminal_theme: crate::terminal_theme::TerminalTheme::default(),
transient_default_color_owner_pgid: None,
default_color_tracker: DefaultColorOscTracker::default(),
default_color_event_tracker: DefaultColorEventTracker::default(),
child_default_foreground_changed: false,
child_default_background_changed: false,
osc_debug_tracker: OscDebugTracker::default(),
agent_osc_state: AgentOscStateTracker::default(),
xtgettcap_query_tracker: XtgettcapQueryTracker::default(),
decscusr_tracker: DecscusrTracker::default(),
cursor_settle_state: CursorPositionSettleState::default(),
windows_powershell_prompt_cwd_reporting: false,
}),
key_encoder: Mutex::new(key_encoder),
pending_pty_responses,
})
}
pub(super) fn set_windows_powershell_prompt_cwd_reporting(&self, enabled: bool) {
if let Ok(mut core) = self.core.lock() {
core.windows_powershell_prompt_cwd_reporting = enabled;
}
}
pub fn apply_host_terminal_theme(&self, theme: crate::terminal_theme::TerminalTheme) {
if let Ok(mut core) = self.core.lock() {
let foreground_unowned = !core.child_default_foreground_changed;
let background_unowned = !core.child_default_background_changed;
core.host_terminal_theme = theme;
if foreground_unowned && background_unowned {
core.transient_default_color_owner_pgid = None;
}
let mut palette = crate::ghostty::default_palette();
for (index, color) in theme.palette.iter().enumerate() {
if let Some(color) = color {
palette[index] = crate::ghostty::RgbColor {
r: color.r,
g: color.g,
b: color.b,
};
}
}
if let Err(err) = core.terminal.set_default_palette(&palette) {
debug!(err = %err, "failed to apply host terminal palette");
}
write_host_terminal_theme_selective(
&mut core.terminal,
theme,
foreground_unowned,
background_unowned,
);