-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rs
More file actions
1464 lines (1289 loc) · 47.1 KB
/
Copy pathapp.rs
File metadata and controls
1464 lines (1289 loc) · 47.1 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 crate::session::{
load_remote_task_log, load_sessions, refresh_session_statuses_with_cache, CopilotSession,
SessionSource, SessionStatus, SessionStatusCache,
};
use crate::terminal::EmbeddedTerminal;
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::sync::mpsc::{self, Receiver, Sender};
const DETAIL_PAGE_SCROLL_AMOUNT: usize = 5;
const APP_TERMINAL_TITLE: &str = "gh pilot";
// ── Enums ────────────────────────────────────────────────────────────────────
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Panel {
Sessions,
Detail,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Mode {
Normal,
/// User is typing a directory path for a new copilot session.
NewSessionDir,
/// A new copilot session launch has been queued and is starting up.
LaunchingNewSession,
/// User is typing text to filter sessions by directory.
DirectoryFilter,
/// An embedded copilot session is live in the right panel.
Terminal,
/// Shortcut help popup is open.
Help,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum SessionFilter {
All,
Running,
Pending,
Remote,
}
impl SessionFilter {
fn next(self) -> Self {
match self {
SessionFilter::All => SessionFilter::Running,
SessionFilter::Running => SessionFilter::Pending,
SessionFilter::Pending => SessionFilter::Remote,
SessionFilter::Remote => SessionFilter::All,
}
}
fn previous(self) -> Self {
match self {
SessionFilter::All => SessionFilter::Remote,
SessionFilter::Running => SessionFilter::All,
SessionFilter::Pending => SessionFilter::Running,
SessionFilter::Remote => SessionFilter::Pending,
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct SessionFilterCounts {
pub all: usize,
pub running: usize,
pub pending: usize,
pub remote: usize,
}
/// Actions that require access to the terminal size (only available in the
/// event loop) before we can spawn an embedded PTY.
#[derive(Debug, Clone)]
pub enum PendingAction {
None,
/// Resume an existing session by attaching tmux in the user's real terminal.
OpenNative {
id: String,
cwd: PathBuf,
},
/// Resume an existing session embedded in the right panel.
OpenEmbedded {
id: String,
cwd: PathBuf,
},
/// Start a new copilot session in `dir`, attached in the user's real terminal.
LaunchNewNative {
dir: PathBuf,
},
/// Open a remote agent task in the browser.
OpenRemoteTask {
url: String,
},
}
// ── App ──────────────────────────────────────────────────────────────────────
pub struct App {
pub sessions: Vec<CopilotSession>,
/// Session indices shown in the left panel.
pub flat_list: Vec<usize>,
/// Current cursor position in the flat list.
pub cursor: usize,
/// Index of the session currently shown in the detail panel.
pub selected_session: Option<usize>,
pub active_panel: Panel,
/// Root copilot config dir (default: `~/.copilot`).
pub copilot_dir: PathBuf,
/// Directory where pilot was launched (default for new sessions).
pub launch_dir: PathBuf,
pub mode: Mode,
pub input_buffer: String,
pub session_filter: SessionFilter,
pub directory_filter: String,
directory_filter_history: Vec<String>,
directory_suggestion_cursor: Option<usize>,
initial_directory_filter_applied: bool,
pub detail_scroll: usize,
pub help_scroll: usize,
pub should_quit: bool,
pub status_message: Option<String>,
pub pending_action: PendingAction,
/// Session IDs present before launching a new Copilot session.
pub new_session_reload_baseline: Option<HashSet<String>>,
notified_waiting_sessions: HashSet<String>,
status_cache: SessionStatusCache,
/// A live copilot session embedded in the right panel, if any.
pub embedded_terminal: Option<EmbeddedTerminal>,
/// Whether the embedded terminal is taking the full TUI area.
pub terminal_fullscreen: bool,
/// Sends completed remote log loads from background workers to the main loop.
remote_log_sender: Sender<(String, String)>,
/// Receives completed remote log loads without blocking the UI.
remote_log_receiver: Receiver<(String, String)>,
/// Remote session IDs whose logs are currently loading in the background.
remote_logs_loading: HashSet<String>,
/// Sends completed session loads from background workers to the main loop.
session_load_sender: Sender<(u64, Vec<CopilotSession>)>,
/// Receives completed session loads without blocking startup or rendering.
session_load_receiver: Receiver<(u64, Vec<CopilotSession>)>,
/// Whether a session list load is currently in flight.
sessions_loading: bool,
/// Monotonically increasing token used to ignore stale background loads.
session_load_generation: u64,
}
impl App {
pub fn new(copilot_dir: PathBuf, launch_dir: PathBuf) -> Self {
let (remote_log_sender, remote_log_receiver) = mpsc::channel();
let (session_load_sender, session_load_receiver) = mpsc::channel();
App {
sessions: Vec::new(),
flat_list: Vec::new(),
cursor: 0,
selected_session: None,
active_panel: Panel::Sessions,
copilot_dir,
launch_dir,
mode: Mode::Normal,
input_buffer: String::new(),
session_filter: SessionFilter::All,
directory_filter: String::new(),
directory_filter_history: Vec::new(),
directory_suggestion_cursor: None,
initial_directory_filter_applied: false,
detail_scroll: 0,
help_scroll: 0,
should_quit: false,
status_message: None,
pending_action: PendingAction::None,
new_session_reload_baseline: None,
notified_waiting_sessions: HashSet::new(),
status_cache: SessionStatusCache::default(),
embedded_terminal: None,
terminal_fullscreen: false,
remote_log_sender,
remote_log_receiver,
remote_logs_loading: HashSet::new(),
session_load_sender,
session_load_receiver,
sessions_loading: false,
session_load_generation: 0,
}
}
pub fn reload(&mut self) {
self.session_load_generation = self.session_load_generation.wrapping_add(1);
self.sessions_loading = true;
let generation = self.session_load_generation;
let copilot_dir = self.copilot_dir.clone();
let sender = self.session_load_sender.clone();
std::thread::spawn(move || {
let sessions = load_sessions(&copilot_dir);
let _ = sender.send((generation, sessions));
});
}
pub fn poll_session_loads(&mut self) {
while let Ok((generation, sessions)) = self.session_load_receiver.try_recv() {
if generation == self.session_load_generation {
self.sessions_loading = false;
self.replace_sessions(sessions);
}
}
}
pub fn is_loading_sessions(&self) -> bool {
self.sessions_loading
}
pub fn terminal_title(&self) -> String {
self.current_session_title()
.and_then(|title| sanitize_terminal_title_part(&title))
.unwrap_or_else(|| APP_TERMINAL_TITLE.to_string())
}
pub fn refresh_statuses(&mut self) -> bool {
refresh_session_statuses_with_cache(
&self.copilot_dir,
&mut self.sessions,
&mut self.status_cache,
);
self.take_waiting_notification()
}
pub fn status_poll_interval(&self) -> std::time::Duration {
if self
.sessions
.iter()
.any(|session| session.status != SessionStatus::Idle)
{
std::time::Duration::from_secs(1)
} else {
std::time::Duration::from_secs(5)
}
}
fn replace_sessions(&mut self, mut sessions: Vec<CopilotSession>) {
let cursor_id = self
.session_at_cursor()
.map(|i| self.sessions[i].id.clone())
.or_else(|| self.selected_session.map(|i| self.sessions[i].id.clone()));
for session in &mut sessions {
if session.source == SessionSource::Remote {
session.remote_log = self
.sessions
.iter()
.find(|existing| existing.id == session.id)
.and_then(|existing| existing.remote_log.clone());
}
}
self.sessions = sessions;
let session_ids: HashSet<&str> = self
.sessions
.iter()
.map(|session| session.id.as_str())
.collect();
self.notified_waiting_sessions
.retain(|id| session_ids.contains(id.as_str()));
self.status_cache.retain_sessions(&session_ids);
self.apply_initial_directory_filter();
self.flat_list =
build_flat_list(&self.sessions, self.session_filter, &self.directory_filter);
if let Some(id) = cursor_id {
if let Some(pos) = self
.flat_list
.iter()
.position(|i| self.sessions[*i].id == id)
{
self.cursor = pos;
}
}
if self.cursor >= self.flat_list.len() {
self.cursor = self.flat_list.len().saturating_sub(1);
}
self.update_selected_from_cursor();
self.detail_scroll = 0;
if self.active_panel == Panel::Detail {
self.load_selected_remote_preview();
}
}
pub fn capture_new_session_reload_baseline(&mut self) {
self.new_session_reload_baseline = Some(
self.sessions
.iter()
.map(|session| session.id.clone())
.collect(),
);
}
pub fn clear_new_session_reload_watch(&mut self) {
self.new_session_reload_baseline = None;
}
pub fn has_new_session_reload_watch(&self) -> bool {
self.new_session_reload_baseline.is_some()
}
pub fn reload_if_new_session_created(&mut self) -> Option<String> {
let baseline = self.new_session_reload_baseline.as_ref()?;
let sessions = load_sessions(&self.copilot_dir);
let new_session_id = sessions
.iter()
.find(|session| !baseline.contains(&session.id))
.map(|session| session.id.clone())?;
self.session_load_generation = self.session_load_generation.wrapping_add(1);
self.new_session_reload_baseline = None;
self.replace_sessions(sessions);
self.sessions_loading = false;
Some(new_session_id)
}
// ── Navigation ────────────────────────────────────────────────────────────
pub fn move_up(&mut self) {
if self.cursor == 0 {
return;
}
self.cursor -= 1;
self.update_selected_from_cursor();
}
pub fn move_down(&mut self) {
if self.cursor + 1 >= self.flat_list.len() {
return;
}
self.cursor += 1;
self.update_selected_from_cursor();
}
/// Select / activate the item currently under the cursor.
pub fn select_current(&mut self) {
if let Some(idx) = self.session_at_cursor() {
self.selected_session = Some(idx);
self.active_panel = Panel::Detail;
self.detail_scroll = 0;
self.load_selected_remote_preview();
}
}
pub fn focus_sessions(&mut self) {
self.active_panel = Panel::Sessions;
}
pub fn scroll_detail_up(&mut self) {
self.detail_scroll = self.detail_scroll.saturating_sub(1);
}
pub fn scroll_detail_down(&mut self) {
self.detail_scroll += 1;
}
pub fn scroll_detail_page_up(&mut self) {
self.detail_scroll = self.detail_scroll.saturating_sub(DETAIL_PAGE_SCROLL_AMOUNT);
}
pub fn scroll_detail_page_down(&mut self) {
self.detail_scroll += DETAIL_PAGE_SCROLL_AMOUNT;
}
pub fn open_help(&mut self) {
self.mode = Mode::Help;
self.help_scroll = 0;
}
pub fn close_help(&mut self) {
self.mode = Mode::Normal;
self.help_scroll = 0;
}
pub fn scroll_help_up(&mut self) {
self.help_scroll = self.help_scroll.saturating_sub(1);
}
pub fn scroll_help_down(&mut self) {
self.help_scroll += 1;
}
pub fn scroll_help_page_up(&mut self) {
self.help_scroll = self.help_scroll.saturating_sub(DETAIL_PAGE_SCROLL_AMOUNT);
}
pub fn scroll_help_page_down(&mut self) {
self.help_scroll += DETAIL_PAGE_SCROLL_AMOUNT;
}
pub fn next_session_filter(&mut self) {
self.session_filter = self.session_filter.next();
self.apply_session_filters();
}
pub fn previous_session_filter(&mut self) {
self.session_filter = self.session_filter.previous();
self.apply_session_filters();
}
pub fn begin_directory_filter(&mut self) {
self.mode = Mode::DirectoryFilter;
self.input_buffer = self.directory_filter.clone();
self.sync_directory_suggestion_cursor();
}
pub fn confirm_directory_filter(&mut self) {
let directory_filter = self.input_buffer.trim().to_string();
self.remember_directory_filter(&directory_filter);
self.directory_filter = directory_filter;
self.input_buffer.clear();
self.directory_suggestion_cursor = None;
self.mode = Mode::Normal;
self.apply_session_filters();
}
pub fn clear_directory_filter(&mut self) {
self.directory_filter.clear();
self.apply_session_filters();
}
pub fn session_filter_counts(&self) -> SessionFilterCounts {
count_session_filters(&self.sessions, &self.directory_filter)
}
// ── Session actions ───────────────────────────────────────────────────────
/// Open the prompt to launch a new copilot session.
pub fn begin_new_session(&mut self) {
self.mode = Mode::NewSessionDir;
self.input_buffer = self.default_new_session_dir();
self.sync_directory_suggestion_cursor();
}
/// Confirm the new session directory prompt and queue a launch.
pub fn confirm_new_session(&mut self) {
let raw = self.input_buffer.trim().to_string();
let dir = if raw.is_empty() {
self.launch_dir.clone()
} else if let Some(rest) = raw.strip_prefix("~/") {
dirs::home_dir()
.map(|h| h.join(rest))
.unwrap_or_else(|| PathBuf::from(&raw))
} else if raw == "~" {
dirs::home_dir().unwrap_or_else(|| PathBuf::from(&raw))
} else {
PathBuf::from(&raw)
};
self.input_buffer.clear();
self.directory_suggestion_cursor = None;
self.mode = Mode::LaunchingNewSession;
self.active_panel = Panel::Detail;
self.status_message = Some("Creating new Copilot session…".into());
self.pending_action = PendingAction::LaunchNewNative { dir };
}
pub fn launching_new_session_dir(&self) -> Option<&Path> {
match &self.pending_action {
PendingAction::LaunchNewNative { dir } if self.mode == Mode::LaunchingNewSession => {
Some(dir.as_path())
}
_ => None,
}
}
pub fn cancel_input(&mut self) {
self.mode = Mode::Normal;
self.input_buffer.clear();
self.directory_suggestion_cursor = None;
}
pub fn select_next_directory_suggestion(&mut self) {
self.select_directory_suggestion(false);
}
pub fn select_previous_directory_suggestion(&mut self) {
self.select_directory_suggestion(true);
}
pub fn sync_directory_suggestion_cursor(&mut self) {
let current = self.input_buffer.trim();
self.directory_suggestion_cursor = self
.directory_suggestions()
.iter()
.position(|suggestion| suggestion == current);
}
fn select_directory_suggestion(&mut self, reverse: bool) {
let suggestions = self.directory_suggestions();
if suggestions.is_empty() {
return;
}
let current_index = self
.directory_suggestion_cursor
.filter(|index| *index < suggestions.len())
.or_else(|| {
let current = self.input_buffer.trim();
suggestions
.iter()
.position(|suggestion| suggestion == current)
});
let next_index = match current_index {
Some(0) if reverse => suggestions.len() - 1,
Some(index) if reverse => index - 1,
Some(index) => (index + 1) % suggestions.len(),
None if reverse => suggestions.len() - 1,
None => 0,
};
self.input_buffer = suggestions[next_index].clone();
self.directory_suggestion_cursor = Some(next_index);
}
/// Queue opening an embedded terminal for the session under the cursor.
pub fn open_session_native(&mut self) {
if let Some(idx) = self.session_at_cursor() {
if self.sessions[idx].source == SessionSource::Remote {
self.selected_session = Some(idx);
self.active_panel = Panel::Detail;
self.load_selected_remote_preview();
match self.sessions[idx].remote_url.clone() {
Some(url) => self.pending_action = PendingAction::OpenRemoteTask { url },
None => self.status_message = Some("Remote task URL not available".into()),
}
return;
}
let id = self.sessions[idx].id.clone();
let cwd = self.sessions[idx].cwd.clone();
self.selected_session = Some(idx);
self.active_panel = Panel::Detail;
self.pending_action = PendingAction::OpenNative { id, cwd };
}
}
/// Queue opening an embedded terminal preview for the session under the cursor.
pub fn open_session_embedded(&mut self) {
if let Some(idx) = self.session_at_cursor() {
if self.sessions[idx].source == SessionSource::Remote {
self.selected_session = Some(idx);
self.active_panel = Panel::Detail;
self.load_selected_remote_preview();
return;
}
let id = self.sessions[idx].id.clone();
let cwd = self.sessions[idx].cwd.clone();
self.selected_session = Some(idx);
self.active_panel = Panel::Detail;
self.pending_action = PendingAction::OpenEmbedded { id, cwd };
}
}
/// Starts loading the selected remote task log in the background if needed.
fn load_selected_remote_preview(&mut self) {
let Some(idx) = self.selected_session else {
return;
};
if self.sessions[idx].source != SessionSource::Remote
|| self.sessions[idx].remote_log.is_some()
|| self.remote_logs_loading.contains(&self.sessions[idx].id)
{
return;
}
let id = self.sessions[idx].id.clone();
self.remote_logs_loading.insert(id.clone());
let sender = self.remote_log_sender.clone();
std::thread::spawn(move || {
let log = load_remote_task_log(&id);
let _ = sender.send((id, log));
});
}
pub fn poll_remote_log_loads(&mut self) {
while let Ok((id, log)) = self.remote_log_receiver.try_recv() {
self.remote_logs_loading.remove(&id);
if let Some(session) = self.sessions.iter_mut().find(|session| session.id == id) {
session.remote_log = Some(log);
}
}
}
pub fn is_remote_log_loading(&self, id: &str) -> bool {
self.remote_logs_loading.contains(id)
}
/// Detach from the embedded terminal and return to normal mode.
pub fn detach_terminal(&mut self) {
self.embedded_terminal = None;
self.mode = Mode::Normal;
self.terminal_fullscreen = false;
self.status_message = Some("Detached; Copilot continues in tmux".into());
}
pub fn toggle_terminal_fullscreen(&mut self) {
self.terminal_fullscreen = !self.terminal_fullscreen;
}
// ── Helpers ───────────────────────────────────────────────────────────────
pub fn session_at_cursor(&self) -> Option<usize> {
self.flat_list.get(self.cursor).copied()
}
fn default_new_session_dir(&self) -> String {
let directory_filter = self.directory_filter.trim();
if !directory_filter.is_empty() {
return directory_filter.to_string();
}
self.session_at_cursor()
.or(self.selected_session)
.map(|idx| self.sessions[idx].cwd.to_string_lossy().to_string())
.unwrap_or_else(|| self.launch_dir.to_string_lossy().to_string())
}
fn remember_directory_filter(&mut self, directory_filter: &str) {
let directory_filter = directory_filter.trim();
if directory_filter.is_empty() {
return;
}
self.directory_filter_history
.retain(|entry| entry != directory_filter);
self.directory_filter_history
.insert(0, directory_filter.to_string());
self.directory_filter_history.truncate(20);
}
pub fn directory_suggestions(&self) -> Vec<String> {
let mut suggestions = Vec::new();
push_unique_directory_suggestion(&mut suggestions, self.directory_filter.trim());
for entry in &self.directory_filter_history {
push_unique_directory_suggestion(&mut suggestions, entry);
}
let mut local_sessions: Vec<_> = self
.sessions
.iter()
.filter(|session| session.source == SessionSource::Local)
.collect();
local_sessions.sort_by(|a, b| b.updated_at.cmp(&a.updated_at));
for session in local_sessions {
push_unique_directory_suggestion(&mut suggestions, &session.cwd.to_string_lossy());
}
suggestions
}
pub fn directory_suggestion_cursor(&self) -> Option<usize> {
self.directory_suggestion_cursor
.filter(|index| *index < self.directory_suggestions().len())
}
fn take_waiting_notification(&mut self) -> bool {
let waiting_sessions: HashSet<String> = self
.sessions
.iter()
.filter(|session| session.status == SessionStatus::Waiting)
.map(|session| session.id.clone())
.collect();
let should_notify = waiting_sessions
.iter()
.any(|id| !self.notified_waiting_sessions.contains(id));
self.notified_waiting_sessions = waiting_sessions;
should_notify
}
fn update_selected_from_cursor(&mut self) {
if let Some(idx) = self.session_at_cursor() {
self.selected_session = Some(idx);
self.detail_scroll = 0;
} else {
self.selected_session = None;
}
}
fn apply_session_filters(&mut self) {
let selected_id = self
.selected_session
.map(|idx| self.sessions[idx].id.clone());
self.flat_list =
build_flat_list(&self.sessions, self.session_filter, &self.directory_filter);
self.cursor = selected_id
.and_then(|id| {
self.flat_list
.iter()
.position(|idx| self.sessions[*idx].id == id)
})
.unwrap_or(0);
if self.cursor >= self.flat_list.len() {
self.cursor = self.flat_list.len().saturating_sub(1);
}
self.selected_session = self.session_at_cursor();
self.detail_scroll = 0;
}
fn apply_initial_directory_filter(&mut self) {
if self.initial_directory_filter_applied {
return;
}
self.initial_directory_filter_applied = true;
if !self.directory_filter.trim().is_empty() {
return;
}
let launch_dir = self.launch_dir.to_string_lossy().to_string();
if self
.sessions
.iter()
.any(|session| matches_directory_filter(session, &launch_dir))
{
self.directory_filter = launch_dir;
}
}
fn current_session_title(&self) -> Option<String> {
if let Some(terminal) = &self.embedded_terminal {
if let Some(title) = terminal
.terminal_title()
.filter(|title| !title.trim().is_empty())
{
return Some(title);
}
if let Some(session) = self
.sessions
.iter()
.find(|session| session.id == terminal.session_id)
{
return Some(session.display_name());
}
return Some(if terminal.session_id == "new" {
"New Copilot session".to_string()
} else {
terminal.session_id.clone()
});
}
self.selected_session
.map(|idx| self.sessions[idx].display_name())
}
}
fn sanitize_terminal_title_part(title: &str) -> Option<String> {
let sanitized: String = title
.chars()
.map(|c| if c.is_control() { ' ' } else { c })
.collect();
let sanitized = sanitized.split_whitespace().collect::<Vec<_>>().join(" ");
(!sanitized.is_empty()).then(|| sanitized.to_string())
}
// ── Build flat list ───────────────────────────────────────────────────────────
fn build_flat_list(
sessions: &[CopilotSession],
session_filter: SessionFilter,
directory_filter: &str,
) -> Vec<usize> {
let mut flat = Vec::new();
for (idx, session) in sessions.iter().enumerate() {
if matches_directory_filter(session, directory_filter)
&& matches_session_filter(session, session_filter)
{
flat.push(idx);
}
}
flat
}
fn count_session_filters(
sessions: &[CopilotSession],
directory_filter: &str,
) -> SessionFilterCounts {
sessions
.iter()
.filter(|session| matches_directory_filter(session, directory_filter))
.fold(SessionFilterCounts::default(), |mut counts, session| {
counts.all += 1;
match &session.status {
SessionStatus::Running => counts.running += 1,
SessionStatus::Waiting => counts.pending += 1,
_ => {}
}
if is_remote_session(session) {
counts.remote += 1;
}
counts
})
}
fn matches_session_filter(session: &CopilotSession, session_filter: SessionFilter) -> bool {
match session_filter {
SessionFilter::All => true,
SessionFilter::Running => session.status == SessionStatus::Running,
SessionFilter::Pending => session.status == SessionStatus::Waiting,
SessionFilter::Remote => is_remote_session(session),
}
}
fn matches_directory_filter(session: &CopilotSession, directory_filter: &str) -> bool {
let filters = directory_filter_candidates(directory_filter);
filters.is_empty()
|| filters
.iter()
.any(|filter| session_matches_directory_filter(session, filter))
}
fn session_matches_directory_filter(session: &CopilotSession, filter: &str) -> bool {
path_matches_filter(&session.cwd, filter)
|| session
.git_root
.as_ref()
.is_some_and(|git_root| path_matches_filter(git_root, filter))
|| session
.repository
.as_ref()
.is_some_and(|repository| repository_matches_filter(repository, filter))
}
fn path_matches_filter(path: &Path, filter: &str) -> bool {
normalize_filter_text(&path.to_string_lossy()).contains(filter)
}
fn repository_matches_filter(repository: &str, filter: &str) -> bool {
let repository = normalize_filter_text(repository);
repository.contains(filter) || filter_contains_repository_path(filter, &repository)
}
fn filter_contains_repository_path(filter: &str, repository: &str) -> bool {
if !repository.contains('/') {
return false;
}
let repository_path = format!("/{repository}");
filter.ends_with(&repository_path) || filter.contains(&format!("{repository_path}/"))
}
fn directory_filter_candidates(directory_filter: &str) -> Vec<String> {
let filter = directory_filter.trim();
if filter.is_empty() {
return Vec::new();
}
let mut candidates = Vec::new();
let normalized = normalize_filter_text(filter);
if !normalized.is_empty() {
candidates.push(normalized);
}
if let Some(expanded) = expand_home_directory_filter(filter) {
let expanded = normalize_filter_text(&expanded.to_string_lossy());
if !expanded.is_empty() && !candidates.contains(&expanded) {
candidates.push(expanded);
}
}
candidates
}
fn expand_home_directory_filter(filter: &str) -> Option<PathBuf> {
if filter == "~" {
return dirs::home_dir();
}
let rest = filter
.strip_prefix("~/")
.or_else(|| filter.strip_prefix("~\\"))?;
dirs::home_dir().map(|home| home.join(rest))
}
fn normalize_filter_text(value: &str) -> String {
value
.replace('\\', "/")
.trim_end_matches('/')
.to_ascii_lowercase()
}
fn is_remote_session(session: &CopilotSession) -> bool {
session.source == SessionSource::Remote
}
fn push_unique_directory_suggestion(suggestions: &mut Vec<String>, value: &str) {
let value = value.trim();
if !value.is_empty() && !suggestions.iter().any(|suggestion| suggestion == value) {
suggestions.push(value.to_string());
}
}
#[cfg(test)]
mod tests {
use super::*;
use chrono::Utc;
fn app_with_sessions(sessions: Vec<CopilotSession>) -> App {
let (remote_log_sender, remote_log_receiver) = mpsc::channel();
let (session_load_sender, session_load_receiver) = mpsc::channel();
let flat_list = build_flat_list(&sessions, SessionFilter::All, "");
let selected_session = flat_list.first().copied();
App {
flat_list,
cursor: 0,
selected_session,
sessions,
active_panel: Panel::Sessions,
copilot_dir: PathBuf::from("/tmp/copilot"),
launch_dir: PathBuf::from("/tmp"),
mode: Mode::Normal,
input_buffer: String::new(),
session_filter: SessionFilter::All,
directory_filter: String::new(),
directory_filter_history: Vec::new(),
directory_suggestion_cursor: None,
initial_directory_filter_applied: false,
detail_scroll: 0,
help_scroll: 0,
should_quit: false,
status_message: None,
pending_action: PendingAction::None,
new_session_reload_baseline: None,
notified_waiting_sessions: HashSet::new(),
status_cache: SessionStatusCache::default(),
embedded_terminal: None,
terminal_fullscreen: false,
remote_log_sender,
remote_log_receiver,
remote_logs_loading: HashSet::new(),
session_load_sender,
session_load_receiver,
sessions_loading: false,
session_load_generation: 0,
}
}
fn session(id: &str, source: SessionSource) -> CopilotSession {
session_with_details(id, source, "/tmp", SessionStatus::Idle, None, None)
}
fn session_with_summary(id: &str, summary: &str) -> CopilotSession {
let mut session = session(id, SessionSource::Local);
session.summary = Some(summary.to_string());
session
}
fn session_with_details(
id: &str,
source: SessionSource,
cwd: &str,
status: SessionStatus,
git_root: Option<&str>,
repository: Option<&str>,
) -> CopilotSession {
CopilotSession {
id: id.to_string(),
source,
cwd: PathBuf::from(cwd),
git_root: git_root.map(PathBuf::from),
repository: repository.map(ToString::to_string),
branch: None,
summary: None,
last_agent_message: None,
user_named: false,
created_at: Utc::now(),
updated_at: Utc::now(),
status,
remote_state: None,
remote_url: None,
remote_user: None,
pull_request: None,
remote_log: None,
}
}
#[test]
fn terminal_title_defaults_to_app_title_without_selection() {
let app = app_with_sessions(vec![]);
assert_eq!(app.terminal_title(), "gh pilot");
}
#[test]
fn terminal_title_uses_selected_session_name() {
let app = app_with_sessions(vec![session_with_summary("session-1", "Fix title display")]);
assert_eq!(app.terminal_title(), "Fix title display");
}
#[test]
fn terminal_title_sanitizes_control_characters() {
let app = app_with_sessions(vec![session_with_summary(
"session-1",
"Fix \u{1b}]0;spoofed\u{7}title",
)]);