matchmaker-minus 5.7.201

An asynchronous data feedable terminal paging library for Rust
Documentation
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
// Test the implementation of std::fmt::Write on Pager
mod fmt_write {
    use crate::{Pager, minus_core::commands::Command};
    use std::fmt::Write;

    #[test]
    fn pager_writeln() {
        const TEST: &str = "This is a line";
        let mut pager = Pager::new();
        writeln!(pager, "{TEST}").unwrap();
        while let Ok(Command::AppendData(text)) = pager.rx.try_recv() {
            if text != "\n" {
                assert_eq!(text, TEST.to_string());
            }
        }
    }

    #[test]
    fn test_write() {
        const TEST: &str = "This is a line";
        let mut pager = Pager::new();
        write!(pager, "{TEST}").unwrap();
        while let Ok(Command::AppendData(text)) = pager.rx.try_recv() {
            assert_eq!(text, TEST.to_string());
        }
    }
}

mod pager_append_str {
    use crate::PagerState;

    #[test]
    fn sequential_append_str() {
        const TEXT1: &str = "This is a line.";
        const TEXT2: &str = " This is a follow up line";
        let mut ps = PagerState::new().unwrap();
        ps.append_str(TEXT1);
        ps.append_str(TEXT2);
        assert_eq!(ps.screen.formatted_lines, vec![format!("{TEXT1}{TEXT2}")]);
        assert_eq!(ps.screen.orig_text, TEXT1.to_string() + TEXT2);
    }

    #[test]
    fn append_sequential_lines() {
        const TEXT1: &str = "This is a line.";
        const TEXT2: &str = " This is a follow up line";
        let mut ps = PagerState::new().unwrap();
        ps.append_str(&(TEXT1.to_string() + "\n"));
        ps.append_str(&(TEXT2.to_string() + "\n"));

        assert_eq!(
            ps.screen.formatted_lines,
            vec![TEXT1.to_string(), TEXT2.to_string()]
        );
    }

    #[test]
    fn crlf_write() {
        const LINES: [&str; 4] = [
            "hello,\n",
            "this is ",
            "a test\r\n",
            "of weird line endings",
        ];

        let mut ps = PagerState::new().unwrap();

        for line in LINES {
            ps.append_str(line);
        }

        assert_eq!(
            ps.screen.formatted_lines,
            vec![
                "hello,".to_string(),
                "this is a test".to_string(),
                "of weird line endings".to_string()
            ]
        );
    }

    #[test]
    fn unusual_whitespace() {
        const LINES: [&str; 4] = [
            "This line has trailing whitespace      ",
            "     This has leading whitespace\n",
            "   This has whitespace on both sides   ",
            "Andthishasnone",
        ];

        let mut ps = PagerState::new().unwrap();

        for line in LINES {
            ps.append_str(line);
        }

        assert_eq!(
            ps.screen.formatted_lines,
            vec![
                "This line has trailing whitespace           This has leading whitespace",
                "   This has whitespace on both sides   Andthishasnone"
            ]
        );
    }

    #[test]
    fn appendstr_with_newlines() {
        const LINES: [&str; 3] = [
            "this is a normal line with no newline",
            "this is an appended line with a newline\n",
            "and this is a third line",
        ];

        let mut ps = PagerState::new().unwrap();
        // For the purpose of testing wrapping while appending strs
        ps.cols = 15;

        for line in LINES {
            ps.append_str(line);
        }

        assert_eq!(
            ps.screen.formatted_lines,
            vec![
                "this is a",
                "normal line",
                "with no",
                "newlinethis is",
                "an appended",
                "line with a",
                "newline",
                "and this is a",
                "third line"
            ]
        );
    }

    #[test]
    fn incremental_append() {
        const LINES: [&str; 4] = [
            "this is a line",
            " and this is another",
            " and this is yet another\n",
            "and this should be on a newline",
        ];

        let mut ps = PagerState::new().unwrap();

        ps.append_str(LINES[0]);

        assert_eq!(ps.screen.orig_text, LINES[0].to_owned());
        assert_eq!(ps.screen.formatted_lines, vec![LINES[0].to_owned()]);

        ps.append_str(LINES[1]);

        let line = LINES[..2].join("");
        assert_eq!(ps.screen.orig_text, line);
        assert_eq!(ps.screen.formatted_lines, vec![line]);

        ps.append_str(LINES[2]);

        let mut line = LINES[..3].join("");
        assert_eq!(ps.screen.orig_text, line);

        line.pop();
        assert_eq!(ps.screen.formatted_lines, vec![line]);

        ps.append_str(LINES[3]);

        let joined = LINES.join("");
        assert_eq!(ps.screen.orig_text, joined);
        assert_eq!(
            ps.screen.formatted_lines,
            joined
                .lines()
                .map(ToString::to_string)
                .collect::<Vec<String>>()
        );
    }

    #[test]
    fn multiple_newlines() {
        const TEST: &str = "This\n\n\nhas many\n newlines\n";

        let mut ps = PagerState::new().unwrap();

        ps.append_str(TEST);

        assert_eq!(ps.screen.orig_text, TEST.to_owned());
        assert_eq!(
            ps.screen.formatted_lines,
            TEST.lines()
                .map(ToString::to_string)
                .collect::<Vec<String>>()
        );

        ps.screen.orig_text = TEST.to_string();
        ps.reformat_display();

        assert_eq!(ps.screen.orig_text, TEST.to_owned());
        assert_eq!(
            ps.screen.formatted_lines,
            TEST.lines()
                .map(ToString::to_string)
                .collect::<Vec<String>>()
        );
    }

    #[test]
    fn append_floating_newline() {
        const TEST: &str = "This is a line with a bunch of\nin between\nbut not at the end";
        let mut ps = PagerState::new().unwrap();
        ps.append_str(TEST);
        assert_eq!(
            ps.screen.formatted_lines,
            vec![
                "This is a line with a bunch of".to_string(),
                "in between".to_string(),
                "but not at the end".to_owned()
            ]
        );
        assert_eq!(ps.screen.orig_text, TEST.to_string());
    }

    #[test]
    fn format_lines_reuses_existing_rows_when_shrinking() {
        let mut ps = PagerState::new().unwrap();
        ps.screen.formatted_lines = vec!["x".repeat(64), "y".repeat(64), "z".repeat(64)];
        let vec_ptr = ps.screen.formatted_lines.as_ptr();
        let row0_ptr = ps.screen.formatted_lines[0].as_ptr();
        let row1_ptr = ps.screen.formatted_lines[1].as_ptr();

        ps.screen.orig_text = "short\nlines".to_string();
        ps.reformat_display();

        assert_eq!(ps.screen.formatted_lines, vec!["short", "lines"]);
        assert_eq!(ps.screen.formatted_lines.as_ptr(), vec_ptr);
        assert_eq!(ps.screen.formatted_lines[0].as_ptr(), row0_ptr);
        assert_eq!(ps.screen.formatted_lines[1].as_ptr(), row1_ptr);
    }

    #[test]
    fn format_lines_reuses_existing_rows_when_growing_within_capacity() {
        let mut ps = PagerState::new().unwrap();
        let mut formatted_lines = Vec::with_capacity(4);
        formatted_lines.push("x".repeat(64));
        formatted_lines.push("y".repeat(64));
        ps.screen.formatted_lines = formatted_lines;

        let vec_ptr = ps.screen.formatted_lines.as_ptr();
        let row0_ptr = ps.screen.formatted_lines[0].as_ptr();
        let row1_ptr = ps.screen.formatted_lines[1].as_ptr();

        ps.screen.orig_text = "one\ntwo\nthree".to_string();
        ps.reformat_display();

        assert_eq!(ps.screen.formatted_lines, vec!["one", "two", "three"]);
        assert_eq!(ps.screen.formatted_lines.as_ptr(), vec_ptr);
        assert_eq!(ps.screen.formatted_lines[0].as_ptr(), row0_ptr);
        assert_eq!(ps.screen.formatted_lines[1].as_ptr(), row1_ptr);
    }
}

// Test exit callbacks function
#[cfg(feature = "dynamic_output")]
#[test]
fn exit_callback() {
    use crate::PagerState;
    use std::sync::atomic::Ordering;
    use std::sync::{Arc, atomic::AtomicBool};

    let mut ps = PagerState::new().unwrap();
    let exited = Arc::new(AtomicBool::new(false));
    let exited_within_callback = exited.clone();
    ps.exit_callbacks.push(Box::new(move || {
        exited_within_callback.store(true, Ordering::Relaxed);
    }));
    ps.exit();

    assert!(exited.load(Ordering::Relaxed));
}

mod emit_events {
    // Check functions emit correct events on function calls
    use crate::{LineNumbers, Pager, minus_core::commands::Command};

    const TEST_STR: &str = "This is sample text";
    #[test]
    fn set_text() {
        let pager = Pager::new();
        pager.set_text(TEST_STR).unwrap();
        assert_eq!(
            Command::SetData(TEST_STR.to_string()),
            pager.rx.try_recv().unwrap()
        );
    }

    #[test]
    fn push_str() {
        let pager = Pager::new();
        pager.push_str(TEST_STR).unwrap();
        assert_eq!(
            Command::AppendData(TEST_STR.to_string()),
            pager.rx.try_recv().unwrap()
        );
    }

    #[test]
    fn set_prompt() {
        let pager = Pager::new();
        pager.set_prompt(TEST_STR).unwrap();
        assert_eq!(
            Command::SetPrompt(TEST_STR.to_string()),
            pager.rx.try_recv().unwrap()
        );
    }

    #[test]
    fn send_message() {
        let pager = Pager::new();
        pager.send_message(TEST_STR).unwrap();
        assert_eq!(
            Command::SendMessage(TEST_STR.to_string()),
            pager.rx.try_recv().unwrap()
        );
    }

    #[test]
    #[cfg(feature = "static_output")]
    fn set_run_no_overflow() {
        let pager = Pager::new();
        pager.set_run_no_overflow(false).unwrap();
        assert_eq!(
            Command::SetRunNoOverflow(false),
            pager.rx.try_recv().unwrap()
        );
    }

    #[test]
    fn set_line_numbers() {
        let pager = Pager::new();
        pager.set_line_numbers(LineNumbers::Enabled).unwrap();
        assert_eq!(
            Command::SetLineNumbers(LineNumbers::Enabled),
            pager.rx.try_recv().unwrap()
        );
    }

    #[test]
    fn set_output_sink() {
        let pager = Pager::new();
        pager.set_output_sink(std::io::stderr()).unwrap();
        assert_eq!(
            Command::SetOutputSink(Box::new(std::io::stderr())),
            pager.rx.try_recv().unwrap()
        );
    }
}

mod output_sink {
    use crate::{OutputSink, Pager, PagerState};
    use std::sync::{Arc, Mutex};

    #[derive(Clone, Default)]
    struct MockSink {
        buffer: Arc<Mutex<Vec<u8>>>,
        is_tty: bool,
    }

    impl std::io::Write for MockSink {
        fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
            self.buffer.lock().unwrap().extend_from_slice(buf);
            Ok(buf.len())
        }

        fn flush(&mut self) -> std::io::Result<()> {
            Ok(())
        }
    }

    impl OutputSink for MockSink {
        fn is_tty(&self) -> bool {
            self.is_tty
        }
    }

    #[test]
    fn test_custom_output_sink_in_pagerstate() {
        let sink = MockSink {
            buffer: Arc::new(Mutex::new(Vec::new())),
            is_tty: false,
        };

        let pager = Pager::new();
        pager.set_output_sink(sink).unwrap();

        let ps = PagerState::generate_initial_state(&pager.rx).unwrap();
        assert!(!ps.output_sink.lock().is_tty());
    }

    #[test]
    fn test_sink_implementations() {
        assert!(!OutputSink::is_tty(&Vec::<u8>::new()));
        assert!(!OutputSink::is_tty(&std::io::Cursor::new(Vec::<u8>::new())));
        assert!(!OutputSink::is_tty(&std::io::sink()));
    }
}