-
-
Notifications
You must be signed in to change notification settings - Fork 651
/
screen_tests.rs
3629 lines (3438 loc) · 137 KB
/
screen_tests.rs
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 super::{screen_thread_main, CopyOptions, Screen, ScreenInstruction};
use crate::panes::PaneId;
use crate::{
channels::SenderWithContext,
os_input_output::{AsyncReader, Pid, ServerOsApi},
route::route_action,
thread_bus::Bus,
ClientId, ServerInstruction, SessionMetaData, ThreadSenders,
};
use insta::assert_snapshot;
use std::path::PathBuf;
use zellij_utils::cli::CliAction;
use zellij_utils::data::{Event, Resize, Style};
use zellij_utils::errors::{prelude::*, ErrorContext};
use zellij_utils::input::actions::Action;
use zellij_utils::input::command::{RunCommand, TerminalAction};
use zellij_utils::input::config::Config;
use zellij_utils::input::layout::{
FloatingPaneLayout, Layout, PluginAlias, PluginUserConfiguration, Run, RunPlugin,
RunPluginLocation, RunPluginOrAlias, SplitDirection, SplitSize, TiledPaneLayout,
};
use zellij_utils::input::options::Options;
use zellij_utils::ipc::IpcReceiverWithContext;
use zellij_utils::pane_size::{Size, SizeInPixels};
use crate::background_jobs::BackgroundJob;
use crate::pty_writer::PtyWriteInstruction;
use std::env::set_var;
use std::os::unix::io::RawFd;
use std::sync::{Arc, Mutex};
use crate::{
plugins::PluginInstruction,
pty::{ClientTabIndexOrPaneId, PtyInstruction},
};
use zellij_utils::ipc::PixelDimensions;
use zellij_utils::{
channels::{self, ChannelWithContext, Receiver},
data::{Direction, FloatingPaneCoordinates, InputMode, ModeInfo, Palette, PluginCapabilities},
interprocess::local_socket::LocalSocketStream,
ipc::{ClientAttributes, ClientToServerMsg, ServerToClientMsg},
};
use crate::panes::grid::Grid;
use crate::panes::link_handler::LinkHandler;
use crate::panes::sixel::SixelImageStore;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use zellij_utils::vte;
fn take_snapshot_and_cursor_coordinates(
ansi_instructions: &str,
grid: &mut Grid,
) -> (Option<(usize, usize)>, String) {
let mut vte_parser = vte::Parser::new();
for &byte in ansi_instructions.as_bytes() {
vte_parser.advance(grid, byte);
}
(grid.cursor_coordinates(), format!("{:?}", grid))
}
fn take_snapshots_and_cursor_coordinates_from_render_events<'a>(
all_events: impl Iterator<Item = &'a ServerInstruction>,
screen_size: Size,
) -> Vec<(Option<(usize, usize)>, String)> {
let sixel_image_store = Rc::new(RefCell::new(SixelImageStore::default()));
let terminal_emulator_color_codes = Rc::new(RefCell::new(HashMap::new()));
let character_cell_size = Rc::new(RefCell::new(Some(SizeInPixels {
width: 8,
height: 21,
})));
let debug = false;
let arrow_fonts = true;
let styled_underlines = true;
let explicitly_disable_kitty_keyboard_protocol = false;
let mut grid = Grid::new(
screen_size.rows,
screen_size.cols,
Rc::new(RefCell::new(Palette::default())),
terminal_emulator_color_codes,
Rc::new(RefCell::new(LinkHandler::new())),
character_cell_size,
sixel_image_store,
Style::default(),
debug,
arrow_fonts,
styled_underlines,
explicitly_disable_kitty_keyboard_protocol,
);
let snapshots: Vec<(Option<(usize, usize)>, String)> = all_events
.filter_map(|server_instruction| {
match server_instruction {
ServerInstruction::Render(output) => {
if let Some(output) = output {
// note this only takes a snapshot of the first client!
let raw_snapshot = output.get(&1).unwrap();
let snapshot =
take_snapshot_and_cursor_coordinates(raw_snapshot, &mut grid);
Some(snapshot)
} else {
None
}
},
_ => None,
}
})
.collect();
snapshots
}
fn send_cli_action_to_server(
session_metadata: &SessionMetaData,
cli_action: CliAction,
client_id: ClientId,
) {
let get_current_dir = || PathBuf::from(".");
let actions = Action::actions_from_cli(cli_action, Box::new(get_current_dir), None).unwrap();
let senders = session_metadata.senders.clone();
let capabilities = PluginCapabilities::default();
let client_attributes = ClientAttributes::default();
let default_shell = None;
let default_layout = Box::new(Layout::default());
let default_mode = session_metadata
.session_configuration
.get_client_configuration(&client_id)
.options
.default_mode
.unwrap_or(InputMode::Normal);
let client_keybinds = session_metadata
.session_configuration
.get_client_keybinds(&client_id)
.clone();
for action in actions {
route_action(
action,
client_id,
None,
senders.clone(),
capabilities,
client_attributes.clone(),
default_shell.clone(),
default_layout.clone(),
None,
client_keybinds.clone(),
default_mode,
)
.unwrap();
}
}
#[derive(Clone, Default)]
struct FakeInputOutput {
fake_filesystem: Arc<Mutex<HashMap<String, String>>>,
server_to_client_messages: Arc<Mutex<HashMap<ClientId, Vec<ServerToClientMsg>>>>,
}
impl ServerOsApi for FakeInputOutput {
fn set_terminal_size_using_terminal_id(
&self,
_terminal_id: u32,
_cols: u16,
_rows: u16,
_width_in_pixels: Option<u16>,
_height_in_pixels: Option<u16>,
) -> Result<()> {
// noop
Ok(())
}
fn spawn_terminal(
&self,
_file_to_open: TerminalAction,
_quit_db: Box<dyn Fn(PaneId, Option<i32>, RunCommand) + Send>,
_default_editor: Option<PathBuf>,
) -> Result<(u32, RawFd, RawFd)> {
unimplemented!()
}
fn read_from_tty_stdout(&self, _fd: RawFd, _buf: &mut [u8]) -> Result<usize> {
unimplemented!()
}
fn async_file_reader(&self, _fd: RawFd) -> Box<dyn AsyncReader> {
unimplemented!()
}
fn write_to_tty_stdin(&self, _id: u32, _buf: &[u8]) -> Result<usize> {
unimplemented!()
}
fn tcdrain(&self, _id: u32) -> Result<()> {
unimplemented!()
}
fn kill(&self, _pid: Pid) -> Result<()> {
unimplemented!()
}
fn force_kill(&self, _pid: Pid) -> Result<()> {
unimplemented!()
}
fn box_clone(&self) -> Box<dyn ServerOsApi> {
Box::new((*self).clone())
}
fn send_to_client(&self, client_id: ClientId, msg: ServerToClientMsg) -> Result<()> {
self.server_to_client_messages
.lock()
.unwrap()
.entry(client_id)
.or_insert_with(Vec::new)
.push(msg);
Ok(())
}
fn new_client(
&mut self,
_client_id: ClientId,
_stream: LocalSocketStream,
) -> Result<IpcReceiverWithContext<ClientToServerMsg>> {
unimplemented!()
}
fn remove_client(&mut self, _client_id: ClientId) -> Result<()> {
unimplemented!()
}
fn load_palette(&self) -> Palette {
unimplemented!()
}
fn get_cwd(&self, _pid: Pid) -> Option<PathBuf> {
unimplemented!()
}
fn write_to_file(&mut self, contents: String, filename: Option<String>) -> Result<()> {
if let Some(filename) = filename {
self.fake_filesystem
.lock()
.unwrap()
.insert(filename, contents);
}
Ok(())
}
fn re_run_command_in_terminal(
&self,
_terminal_id: u32,
_run_command: RunCommand,
_quit_cb: Box<dyn Fn(PaneId, Option<i32>, RunCommand) + Send>, // u32 is the exit status
) -> Result<(RawFd, RawFd)> {
unimplemented!()
}
fn clear_terminal_id(&self, _terminal_id: u32) -> Result<()> {
unimplemented!()
}
}
fn create_new_screen(size: Size) -> Screen {
let mut bus: Bus<ScreenInstruction> = Bus::empty();
let fake_os_input = FakeInputOutput::default();
bus.os_input = Some(Box::new(fake_os_input));
let client_attributes = ClientAttributes {
size,
..Default::default()
};
let max_panes = None;
let mut mode_info = ModeInfo::default();
mode_info.session_name = Some("zellij-test".into());
let draw_pane_frames = false;
let auto_layout = true;
let session_is_mirrored = true;
let copy_options = CopyOptions::default();
let default_layout = Box::new(Layout::default());
let default_layout_name = None;
let default_shell = None;
let session_serialization = true;
let serialize_pane_viewport = false;
let scrollback_lines_to_serialize = None;
let layout_dir = None;
let debug = false;
let styled_underlines = true;
let arrow_fonts = true;
let explicitly_disable_kitty_keyboard_protocol = false;
let screen = Screen::new(
bus,
&client_attributes,
max_panes,
mode_info,
draw_pane_frames,
auto_layout,
session_is_mirrored,
copy_options,
debug,
default_layout,
default_layout_name,
default_shell,
session_serialization,
serialize_pane_viewport,
scrollback_lines_to_serialize,
styled_underlines,
arrow_fonts,
layout_dir,
explicitly_disable_kitty_keyboard_protocol,
);
screen
}
struct MockScreen {
pub main_client_id: u16,
pub pty_receiver: Option<Receiver<(PtyInstruction, ErrorContext)>>,
pub pty_writer_receiver: Option<Receiver<(PtyWriteInstruction, ErrorContext)>>,
pub background_jobs_receiver: Option<Receiver<(BackgroundJob, ErrorContext)>>,
pub screen_receiver: Option<Receiver<(ScreenInstruction, ErrorContext)>>,
pub server_receiver: Option<Receiver<(ServerInstruction, ErrorContext)>>,
pub plugin_receiver: Option<Receiver<(PluginInstruction, ErrorContext)>>,
pub to_screen: SenderWithContext<ScreenInstruction>,
pub to_pty: SenderWithContext<PtyInstruction>,
pub to_plugin: SenderWithContext<PluginInstruction>,
pub to_server: SenderWithContext<ServerInstruction>,
pub to_pty_writer: SenderWithContext<PtyWriteInstruction>,
pub to_background_jobs: SenderWithContext<BackgroundJob>,
pub os_input: FakeInputOutput,
pub client_attributes: ClientAttributes,
pub config_options: Options,
pub session_metadata: SessionMetaData,
pub config: Config,
last_opened_tab_index: Option<usize>,
}
impl MockScreen {
pub fn run(
&mut self,
initial_layout: Option<TiledPaneLayout>,
initial_floating_panes_layout: Vec<FloatingPaneLayout>,
) -> std::thread::JoinHandle<()> {
let config = self.config.clone();
let client_attributes = self.client_attributes.clone();
let screen_bus = Bus::new(
vec![self.screen_receiver.take().unwrap()],
None,
Some(&self.to_pty.clone()),
Some(&self.to_plugin.clone()),
Some(&self.to_server.clone()),
Some(&self.to_pty_writer.clone()),
Some(&self.to_background_jobs.clone()),
Some(Box::new(self.os_input.clone())),
)
.should_silently_fail();
let debug = false;
let screen_thread = std::thread::Builder::new()
.name("screen_thread".to_string())
.spawn(move || {
set_var("ZELLIJ_SESSION_NAME", "zellij-test");
screen_thread_main(
screen_bus,
None,
client_attributes,
config,
debug,
Box::new(Layout::default()),
)
.expect("TEST")
})
.unwrap();
let pane_layout = initial_layout.unwrap_or_default();
let pane_count = pane_layout.extract_run_instructions().len();
let floating_pane_count = initial_floating_panes_layout.len();
let mut pane_ids = vec![];
let mut floating_pane_ids = vec![];
let mut plugin_ids = HashMap::new();
plugin_ids.insert(
RunPluginOrAlias::from_url("file:/path/to/fake/plugin", &None, None, None).unwrap(),
vec![1],
);
for i in 0..pane_count {
pane_ids.push((i as u32, None));
}
for i in 0..floating_pane_count {
floating_pane_ids.push((i as u32, None));
}
let default_shell = None;
let tab_name = None;
let tab_index = self.last_opened_tab_index.map(|l| l + 1).unwrap_or(0);
let should_change_focus_to_new_tab = true;
let _ = self.to_screen.send(ScreenInstruction::NewTab(
None,
default_shell,
Some(pane_layout.clone()),
initial_floating_panes_layout.clone(),
tab_name,
(vec![], vec![]), // swap layouts
should_change_focus_to_new_tab,
self.main_client_id,
));
let _ = self.to_screen.send(ScreenInstruction::ApplyLayout(
pane_layout,
initial_floating_panes_layout,
pane_ids,
floating_pane_ids,
plugin_ids,
tab_index,
true,
self.main_client_id,
));
self.last_opened_tab_index = Some(tab_index);
screen_thread
}
// same as the above function, but starts a plugin with a plugin alias
pub fn run_with_alias(
&mut self,
initial_layout: Option<TiledPaneLayout>,
initial_floating_panes_layout: Vec<FloatingPaneLayout>,
) -> std::thread::JoinHandle<()> {
let config = self.config.clone();
let client_attributes = self.client_attributes.clone();
let screen_bus = Bus::new(
vec![self.screen_receiver.take().unwrap()],
None,
Some(&self.to_pty.clone()),
Some(&self.to_plugin.clone()),
Some(&self.to_server.clone()),
Some(&self.to_pty_writer.clone()),
Some(&self.to_background_jobs.clone()),
Some(Box::new(self.os_input.clone())),
)
.should_silently_fail();
let debug = false;
let screen_thread = std::thread::Builder::new()
.name("screen_thread".to_string())
.spawn(move || {
set_var("ZELLIJ_SESSION_NAME", "zellij-test");
screen_thread_main(
screen_bus,
None,
client_attributes,
config,
debug,
Box::new(Layout::default()),
)
.expect("TEST")
})
.unwrap();
let pane_layout = initial_layout.unwrap_or_default();
let pane_count = pane_layout.extract_run_instructions().len();
let floating_pane_count = initial_floating_panes_layout.len();
let mut pane_ids = vec![];
let mut floating_pane_ids = vec![];
let mut plugin_ids = HashMap::new();
plugin_ids.insert(
RunPluginOrAlias::Alias(PluginAlias {
name: "fixture_plugin_for_tests".to_owned(),
configuration: Some(Default::default()),
run_plugin: Some(RunPlugin {
location: RunPluginLocation::parse("file:/path/to/fake/plugin", None).unwrap(),
configuration: PluginUserConfiguration::default(),
..Default::default()
}),
..Default::default()
}),
vec![1],
);
for i in 0..pane_count {
pane_ids.push((i as u32, None));
}
for i in 0..floating_pane_count {
floating_pane_ids.push((i as u32, None));
}
let default_shell = None;
let tab_name = None;
let tab_index = self.last_opened_tab_index.map(|l| l + 1).unwrap_or(0);
let should_change_focus_to_new_tab = true;
let _ = self.to_screen.send(ScreenInstruction::NewTab(
None,
default_shell,
Some(pane_layout.clone()),
initial_floating_panes_layout.clone(),
tab_name,
(vec![], vec![]), // swap layouts
should_change_focus_to_new_tab,
self.main_client_id,
));
let _ = self.to_screen.send(ScreenInstruction::ApplyLayout(
pane_layout,
initial_floating_panes_layout,
pane_ids,
floating_pane_ids,
plugin_ids,
tab_index,
true,
self.main_client_id,
));
self.last_opened_tab_index = Some(tab_index);
screen_thread
}
pub fn new_tab(&mut self, tab_layout: TiledPaneLayout) {
let pane_count = tab_layout.extract_run_instructions().len();
let mut pane_ids = vec![];
let plugin_ids = HashMap::new();
let default_shell = None;
let tab_name = None;
let tab_index = self.last_opened_tab_index.map(|l| l + 1).unwrap_or(0);
for i in 0..pane_count {
pane_ids.push((i as u32, None));
}
let should_change_focus_to_new_tab = true;
let _ = self.to_screen.send(ScreenInstruction::NewTab(
None,
default_shell,
Some(tab_layout.clone()),
vec![], // floating_panes_layout
tab_name,
(vec![], vec![]), // swap layouts
should_change_focus_to_new_tab,
self.main_client_id,
));
let _ = self.to_screen.send(ScreenInstruction::ApplyLayout(
tab_layout,
vec![], // floating_panes_layout
pane_ids,
vec![], // floating panes ids
plugin_ids,
0,
true,
self.main_client_id,
));
self.last_opened_tab_index = Some(tab_index);
}
pub fn teardown(&mut self, threads: Vec<std::thread::JoinHandle<()>>) {
let _ = self.to_pty.send(PtyInstruction::Exit);
let _ = self.to_pty_writer.send(PtyWriteInstruction::Exit);
let _ = self.to_screen.send(ScreenInstruction::Exit);
let _ = self.to_server.send(ServerInstruction::KillSession);
let _ = self.to_plugin.send(PluginInstruction::Exit);
for thread in threads {
let _ = thread.join();
}
}
pub fn clone_session_metadata(&self) -> SessionMetaData {
// hack that only clones the clonable parts of SessionMetaData
let layout = Box::new(Layout::default()); // this is not actually correct!!
SessionMetaData {
senders: self.session_metadata.senders.clone(),
capabilities: self.session_metadata.capabilities.clone(),
client_attributes: self.session_metadata.client_attributes.clone(),
default_shell: self.session_metadata.default_shell.clone(),
screen_thread: None,
pty_thread: None,
plugin_thread: None,
pty_writer_thread: None,
background_jobs_thread: None,
session_configuration: self.session_metadata.session_configuration.clone(),
layout,
current_input_modes: self.session_metadata.current_input_modes.clone(),
}
}
}
impl MockScreen {
pub fn new(size: Size) -> Self {
let (to_server, server_receiver): ChannelWithContext<ServerInstruction> =
channels::bounded(50);
let to_server = SenderWithContext::new(to_server);
let (to_screen, screen_receiver): ChannelWithContext<ScreenInstruction> =
channels::unbounded();
let to_screen = SenderWithContext::new(to_screen);
let (to_plugin, plugin_receiver): ChannelWithContext<PluginInstruction> =
channels::unbounded();
let to_plugin = SenderWithContext::new(to_plugin);
let (to_pty, pty_receiver): ChannelWithContext<PtyInstruction> = channels::unbounded();
let to_pty = SenderWithContext::new(to_pty);
let (to_pty_writer, pty_writer_receiver): ChannelWithContext<PtyWriteInstruction> =
channels::unbounded();
let to_pty_writer = SenderWithContext::new(to_pty_writer);
let (to_background_jobs, background_jobs_receiver): ChannelWithContext<BackgroundJob> =
channels::unbounded();
let to_background_jobs = SenderWithContext::new(to_background_jobs);
let client_attributes = ClientAttributes {
size,
..Default::default()
};
let capabilities = PluginCapabilities {
arrow_fonts: Default::default(),
};
let layout = Box::new(Layout::default()); // this is not actually correct!!
let session_metadata = SessionMetaData {
senders: ThreadSenders {
to_screen: Some(to_screen.clone()),
to_pty: Some(to_pty.clone()),
to_plugin: Some(to_plugin.clone()),
to_pty_writer: Some(to_pty_writer.clone()),
to_background_jobs: Some(to_background_jobs.clone()),
to_server: Some(to_server.clone()),
should_silently_fail: true,
},
capabilities,
default_shell: None,
client_attributes: client_attributes.clone(),
screen_thread: None,
pty_thread: None,
plugin_thread: None,
pty_writer_thread: None,
background_jobs_thread: None,
layout,
session_configuration: Default::default(),
current_input_modes: HashMap::new(),
};
let os_input = FakeInputOutput::default();
let config_options = Options::default();
let main_client_id = 1;
MockScreen {
main_client_id,
pty_receiver: Some(pty_receiver),
pty_writer_receiver: Some(pty_writer_receiver),
background_jobs_receiver: Some(background_jobs_receiver),
screen_receiver: Some(screen_receiver),
server_receiver: Some(server_receiver),
plugin_receiver: Some(plugin_receiver),
to_screen,
to_pty,
to_plugin,
to_server,
to_pty_writer,
to_background_jobs,
os_input,
client_attributes,
config_options,
session_metadata,
last_opened_tab_index: None,
config: Config::default(),
}
}
}
macro_rules! log_actions_in_thread {
( $arc_mutex_log:expr, $exit_event:path, $receiver:expr ) => {
std::thread::Builder::new()
.name("pty_writer_thread".to_string())
.spawn({
let log = $arc_mutex_log.clone();
move || loop {
let (event, _err_ctx) = $receiver
.recv()
.expect("failed to receive event on channel");
match event {
$exit_event => {
log.lock().unwrap().push(event);
break;
},
_ => {
log.lock().unwrap().push(event);
},
}
}
})
.unwrap()
};
}
fn new_tab(screen: &mut Screen, pid: u32, tab_index: usize) {
let client_id = 1;
let new_terminal_ids = vec![(pid, None)];
let new_plugin_ids = HashMap::new();
screen
.new_tab(tab_index, (vec![], vec![]), None, Some(client_id))
.expect("TEST");
screen
.apply_layout(
TiledPaneLayout::default(),
vec![], // floating panes layout
new_terminal_ids,
vec![], // new floating terminal ids
new_plugin_ids,
tab_index,
true,
client_id,
)
.expect("TEST");
}
#[test]
fn open_new_tab() {
let size = Size {
cols: 121,
rows: 20,
};
let mut screen = create_new_screen(size);
new_tab(&mut screen, 1, 0);
new_tab(&mut screen, 2, 1);
assert_eq!(screen.tabs.len(), 2, "Screen now has two tabs");
assert_eq!(
screen.get_active_tab(1).unwrap().position,
1,
"Active tab switched to new tab"
);
}
#[test]
pub fn switch_to_prev_tab() {
let size = Size {
cols: 121,
rows: 20,
};
let mut screen = create_new_screen(size);
new_tab(&mut screen, 1, 1);
new_tab(&mut screen, 2, 2);
screen.switch_tab_prev(None, true, 1).expect("TEST");
assert_eq!(
screen.get_active_tab(1).unwrap().position,
0,
"Active tab switched to previous tab"
);
}
#[test]
pub fn switch_to_next_tab() {
let size = Size {
cols: 121,
rows: 20,
};
let mut screen = create_new_screen(size);
new_tab(&mut screen, 1, 1);
new_tab(&mut screen, 2, 2);
screen.switch_tab_prev(None, true, 1).expect("TEST");
screen.switch_tab_next(None, true, 1).expect("TEST");
assert_eq!(
screen.get_active_tab(1).unwrap().position,
1,
"Active tab switched to next tab"
);
}
#[test]
pub fn switch_to_tab_name() {
let size = Size {
cols: 121,
rows: 20,
};
let mut screen = create_new_screen(size);
new_tab(&mut screen, 1, 1);
new_tab(&mut screen, 2, 2);
assert_eq!(
screen
.switch_active_tab_name("Tab #1".to_string(), 1)
.expect("TEST"),
false,
"Active tab switched to tab by name"
);
assert_eq!(
screen
.switch_active_tab_name("Tab #2".to_string(), 1)
.expect("TEST"),
true,
"Active tab switched to tab by name"
);
assert_eq!(
screen
.switch_active_tab_name("Tab #3".to_string(), 1)
.expect("TEST"),
true,
"Active tab switched to tab by name"
);
}
#[test]
pub fn close_tab() {
let size = Size {
cols: 121,
rows: 20,
};
let mut screen = create_new_screen(size);
new_tab(&mut screen, 1, 1);
new_tab(&mut screen, 2, 2);
screen.close_tab(1).expect("TEST");
assert_eq!(screen.tabs.len(), 1, "Only one tab left");
assert_eq!(
screen.get_active_tab(1).unwrap().position,
0,
"Active tab switched to previous tab"
);
}
#[test]
pub fn close_the_middle_tab() {
let size = Size {
cols: 121,
rows: 20,
};
let mut screen = create_new_screen(size);
new_tab(&mut screen, 1, 1);
new_tab(&mut screen, 2, 2);
new_tab(&mut screen, 3, 3);
screen.switch_tab_prev(None, true, 1).expect("TEST");
screen.close_tab(1).expect("TEST");
assert_eq!(screen.tabs.len(), 2, "Two tabs left");
assert_eq!(
screen.get_active_tab(1).unwrap().position,
1,
"Active tab switched to previous tab"
);
}
#[test]
fn move_focus_left_at_left_screen_edge_changes_tab() {
let size = Size {
cols: 121,
rows: 20,
};
let mut screen = create_new_screen(size);
new_tab(&mut screen, 1, 1);
new_tab(&mut screen, 2, 2);
new_tab(&mut screen, 3, 3);
screen.switch_tab_prev(None, true, 1).expect("TEST");
screen.move_focus_left_or_previous_tab(1).expect("TEST");
assert_eq!(
screen.get_active_tab(1).unwrap().position,
0,
"Active tab switched to previous"
);
}
#[test]
fn basic_move_of_active_tab_to_left() {
let mut screen = create_fixed_size_screen();
new_tab(&mut screen, 1, 0);
new_tab(&mut screen, 2, 1);
assert_eq!(screen.get_active_tab(1).unwrap().position, 1);
screen.move_active_tab_to_left(1).expect("TEST");
assert_eq!(
screen.get_active_tab(1).unwrap().position,
0,
"Active tab moved to left"
);
}
fn create_fixed_size_screen() -> Screen {
create_new_screen(Size {
cols: 121,
rows: 20,
})
}
#[test]
fn move_of_active_tab_to_left_when_there_is_only_one_tab() {
let mut screen = create_fixed_size_screen();
new_tab(&mut screen, 1, 0);
assert_eq!(screen.get_active_tab(1).unwrap().position, 0);
screen.move_active_tab_to_left(1).expect("TEST");
assert_eq!(
screen.get_active_tab(1).unwrap().position,
0,
"Active tab moved to left"
);
}
#[test]
fn move_of_active_tab_to_left_multiple_times() {
let mut screen = create_fixed_size_screen();
new_tab(&mut screen, 1, 0);
new_tab(&mut screen, 2, 1);
new_tab(&mut screen, 3, 2);
assert_eq!(screen.get_active_tab(1).unwrap().position, 2);
screen.move_active_tab_to_left(1).expect("TEST");
screen.move_active_tab_to_left(1).expect("TEST");
assert_eq!(
screen.get_active_tab(1).unwrap().position,
0,
"Active tab moved to left twice"
);
}
#[test]
fn wrapping_move_of_active_tab_to_left() {
let mut screen = create_fixed_size_screen();
new_tab(&mut screen, 1, 0);
new_tab(&mut screen, 2, 1);
new_tab(&mut screen, 3, 2);
screen.move_focus_left_or_previous_tab(1).expect("TEST");
screen.move_focus_left_or_previous_tab(1).expect("TEST");
assert_eq!(screen.get_active_tab(1).unwrap().position, 0);
screen.move_active_tab_to_left(1).expect("TEST");
assert_eq!(
screen.get_active_tab(1).unwrap().position,
2,
"Active tab moved to left until wrapped around"
);
}
#[test]
fn basic_move_of_active_tab_to_right() {
let mut screen = create_fixed_size_screen();
new_tab(&mut screen, 1, 0);
new_tab(&mut screen, 2, 1);
screen.move_focus_left_or_previous_tab(1).expect("TEST");
assert_eq!(screen.get_active_tab(1).unwrap().position, 0);
screen.move_active_tab_to_right(1).expect("TEST");
assert_eq!(
screen.get_active_tab(1).unwrap().position,
1,
"Active tab moved to right"
);
}
#[test]
fn move_of_active_tab_to_right_when_there_is_only_one_tab() {
let mut screen = create_fixed_size_screen();
new_tab(&mut screen, 1, 0);
assert_eq!(screen.get_active_tab(1).unwrap().position, 0);
screen.move_active_tab_to_right(1).expect("TEST");
assert_eq!(
screen.get_active_tab(1).unwrap().position,
0,
"Active tab moved to left"
);
}
#[test]
fn move_of_active_tab_to_right_multiple_times() {
let mut screen = create_fixed_size_screen();
new_tab(&mut screen, 1, 0);
new_tab(&mut screen, 2, 1);
new_tab(&mut screen, 3, 2);
screen.move_focus_left_or_previous_tab(1).expect("TEST");
screen.move_focus_left_or_previous_tab(1).expect("TEST");
assert_eq!(screen.get_active_tab(1).unwrap().position, 0);
screen.move_active_tab_to_right(1).expect("TEST");
screen.move_active_tab_to_right(1).expect("TEST");
assert_eq!(
screen.get_active_tab(1).unwrap().position,
2,
"Active tab moved to right twice"
);
}
#[test]
fn wrapping_move_of_active_tab_to_right() {
let mut screen = create_fixed_size_screen();
new_tab(&mut screen, 1, 0);
new_tab(&mut screen, 2, 1);
new_tab(&mut screen, 3, 2);
assert_eq!(screen.get_active_tab(1).unwrap().position, 2);
screen.move_active_tab_to_right(1).expect("TEST");
assert_eq!(
screen.get_active_tab(1).unwrap().position,
0,
"Active tab moved to right until wrapped around"
);
}
#[test]
fn move_focus_right_at_right_screen_edge_changes_tab() {
let size = Size {
cols: 121,
rows: 20,
};
let mut screen = create_new_screen(size);
new_tab(&mut screen, 1, 1);
new_tab(&mut screen, 2, 2);
new_tab(&mut screen, 3, 3);
screen.switch_tab_prev(None, true, 1).expect("TEST");
screen.move_focus_right_or_next_tab(1).expect("TEST");
assert_eq!(
screen.get_active_tab(1).unwrap().position,
2,
"Active tab switched to next"
);
}
#[test]
pub fn toggle_to_previous_tab_simple() {
let position_and_size = Size {
cols: 121,