minus 5.7.2

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
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
//! Provides the [`handle_event`] function

use std::convert::TryInto;
use std::io::Write;
use std::sync::{Arc, atomic::AtomicBool};

#[cfg(feature = "search")]
use parking_lot::{Condvar, Mutex};

use super::CommandQueue;
use super::commands::{Command, IoCommand};
use super::utils::display::{self, AppendStyle};
use crate::ExitStrategy;
#[cfg(feature = "search")]
use crate::search;
use crate::{PagerState, error::MinusError, hooks::Hook, input::InputEvent};

/// Respond based on the type of command
///
/// It will match the type of event received and based on that, it can take actions like:-
/// - Mutating fields of [`PagerState`]
/// - Handle cleanup and exits
/// - Call search related functions
#[cfg_attr(not(feature = "search"), allow(unused_mut))]
#[allow(clippy::too_many_lines)]
// TODO: Remove it in next major release
#[allow(deprecated)]
pub fn handle_event(
    ev: Command,
    p: &mut PagerState,
    command_queue: &mut CommandQueue,
    is_exited: &Arc<AtomicBool>,
) {
    match ev {
        Command::SetData(text) => {
            p.screen.orig_text = text;
            p.screen.line_count = p.screen.orig_text.lines().count();
            p.reformat_display();
            command_queue.push_back(Command::Io(IoCommand::RedrawDisplay));
        }
        Command::UserInput(InputEvent::Exit) => {
            p.run_hooks(Hook::PrePagerExit);
            p.exit();
            is_exited.store(true, std::sync::atomic::Ordering::SeqCst);
        }
        Command::UserInput(InputEvent::UpdateUpperMark(um)) => {
            command_queue.push_back(Command::Io(IoCommand::SetUpperMark(um)));
        }
        Command::UserInput(InputEvent::UpdateLeftMark(lm)) if !p.screen.line_wrapping => {
            let padding = if p.line_numbers.is_on() {
                crate::minus_core::utils::digits(p.screen.line_count())
                    + crate::LineNumbers::EXTRA_PADDING
                    + 2
            } else {
                0
            };
            let max_scrollable = p.screen.get_max_line_length().saturating_add(padding);
            if lm.saturating_add(p.cols) > max_scrollable && lm > p.left_mark {
                return;
            }
            p.left_mark = lm;
            command_queue.push_back(Command::Io(IoCommand::RedrawDisplay));
        }
        Command::UserInput(InputEvent::StartSelection { x, y }) => {
            #[cfg(feature = "search")]
            if p.search_state.search_term.is_some() {
                return;
            }

            if let Some(selection) = p.selection_from_coordinates(x, y) {
                p.selection_anchor = Some(selection);
                p.selection = Some(selection);
                command_queue.push_back(Command::Io(IoCommand::RedrawDisplay));
            }
        }
        Command::UserInput(InputEvent::UpdateSelection { x, y }) => {
            #[cfg(feature = "search")]
            if p.search_state.search_term.is_some() {
                return;
            }

            if p.selection_anchor.is_none() {
                return;
            }

            let writable_rows = p.rows.saturating_sub(1);
            if writable_rows == 0 {
                return;
            }

            let row_count = p.screen.formatted_lines_count();
            let max_upper_mark = row_count.saturating_sub(writable_rows);
            let mut should_redraw = false;
            let mut selection_y = usize::from(y);

            if y == 0 {
                let next_upper_mark = p.upper_mark.saturating_sub(1);
                if next_upper_mark != p.upper_mark {
                    p.upper_mark = next_upper_mark;
                    should_redraw = true;
                }
                selection_y = 0;
            } else if selection_y >= writable_rows {
                let next_upper_mark = p.upper_mark.saturating_add(1).min(max_upper_mark);
                if next_upper_mark != p.upper_mark {
                    p.upper_mark = next_upper_mark;
                    should_redraw = true;
                }
                selection_y = writable_rows.saturating_sub(1);
            }

            #[allow(clippy::cast_possible_truncation)]
            if let Some(selection) = p.selection_from_coordinates(x, selection_y as u16)
                && p.selection != Some(selection)
            {
                p.selection = Some(selection);
                should_redraw = true;
            }

            if should_redraw {
                command_queue.push_back(Command::Io(IoCommand::RedrawDisplay));
            }
        }
        Command::UserInput(InputEvent::ClearSelection) => {
            if p.selection.is_some() || p.selection_anchor.is_some() {
                p.clear_selection();
                command_queue.push_back(Command::Io(IoCommand::RedrawDisplay));
            }
        }

        #[cfg(feature = "clipboard")]
        Command::UserInput(InputEvent::CopySelection) => {
            if let Some(text) = p.extract_selection()
                && let Ok(mut clipboard) = arboard::Clipboard::new()
            {
                let _ = clipboard.set_text(text);
            }
            if p.selection.is_some() || p.selection_anchor.is_some() {
                p.clear_selection();
                command_queue.push_back(Command::Io(IoCommand::RedrawDisplay));
            }
        }
        Command::UserInput(InputEvent::RestorePrompt) => {
            // Set the message to None and new messages to false as all messages have been shown
            p.message = None;
            p.format_prompt();
            command_queue.push_back(Command::Io(IoCommand::RedrawPrompt));
        }
        Command::UserInput(InputEvent::UpdateTermArea(c, r)) => {
            p.rows = r;
            p.cols = c;
            p.reformat_display();
            // Readjust the text wrapping for the new number of columns
            command_queue.push_back(Command::Io(IoCommand::RedrawDisplay));
        }
        Command::UserInput(InputEvent::UpdateLineNumber(l)) => {
            p.line_numbers = l;
            p.reformat_display();
            command_queue.push_back(Command::Io(IoCommand::RedrawDisplay));
        }
        Command::UserInput(InputEvent::Number(n)) => {
            p.prefix_num.push(n);
            p.format_prompt();
            command_queue.push_back(Command::Io(IoCommand::RedrawPrompt));
        }
        #[cfg(feature = "search")]
        Command::UserInput(InputEvent::Search(m)) => {
            p.search_mode = m;
            p.search_state.search_mode = m;
            p.search_state.search_mark = 0;
            command_queue.push_back(Command::Io(IoCommand::FetchSearchQuery));
        }
        #[cfg(feature = "search")]
        Command::UserInput(InputEvent::NextMatch | InputEvent::MoveToNextMatch(1))
            if p.search_state.search_term.is_some() =>
        {
            // Move to next search match after the current upper_mark
            let position_of_next_match =
                search::next_nth_match(&p.search_state.search_idx, p.upper_mark, 1);
            if let Some(pnm) = position_of_next_match {
                p.search_state.search_mark = pnm;
                let upper_mark = *p
                    .search_state
                    .search_idx
                    .iter()
                    .nth(p.search_state.search_mark)
                    .unwrap();
                command_queue.push_back(Command::Io(IoCommand::SetUpperMark(upper_mark)));
                p.format_prompt();
                command_queue.push_back(Command::Io(IoCommand::RedrawPrompt));
            }
        }
        #[cfg(feature = "search")]
        Command::UserInput(InputEvent::PrevMatch | InputEvent::MoveToPrevMatch(1))
            if p.search_state.search_term.is_some() =>
        {
            // If no matches, return immediately
            if p.search_state.search_idx.is_empty() {
                return;
            }
            // Decrement the s_mark and get the preceding index
            p.search_state.search_mark = p.search_state.search_mark.saturating_sub(1);
            if let Some(y) = p
                .search_state
                .search_idx
                .iter()
                .nth(p.search_state.search_mark)
            {
                // If the index is less than or equal to the upper_mark, then set y to the new upper_mark
                if *y < p.upper_mark {
                    command_queue.push_back(Command::UserInput(InputEvent::UpdateUpperMark(*y)));
                    p.format_prompt();
                    command_queue.push_back(Command::Io(IoCommand::RedrawPrompt));
                }
            }
        }
        #[cfg(feature = "search")]
        Command::UserInput(InputEvent::MoveToNextMatch(n))
            if p.search_state.search_term.is_some() =>
        {
            // Move to next nth search match after the current upper_mark
            let position_of_next_match =
                search::next_nth_match(&p.search_state.search_idx, p.upper_mark, n);
            if let Some(pnm) = position_of_next_match {
                p.search_state.search_mark = pnm;
                let mut upper_mark = *p
                    .search_state
                    .search_idx
                    .iter()
                    .nth(p.search_state.search_mark)
                    .unwrap();

                // Ensure there is enough text available after location corresponding to
                // position_of_next_match so that we can display a pagefull of data. If not,
                // reduce it so that a pagefull of text can be accommodated.
                // NOTE: Add 1 to total number of lines to avoid off-by-one errors
                while p.upper_mark.saturating_add(p.rows)
                    > p.screen.formatted_lines_count().saturating_add(1)
                {
                    p.search_state.search_mark = p.search_state.search_mark.saturating_sub(1);
                    upper_mark = *p
                        .search_state
                        .search_idx
                        .iter()
                        .nth(p.search_state.search_mark)
                        .unwrap();
                }
                command_queue
                    .push_back(Command::UserInput(InputEvent::UpdateUpperMark(upper_mark)));
                p.format_prompt();
                command_queue.push_back(Command::Io(IoCommand::RedrawPrompt));
            }
        }
        #[cfg(feature = "search")]
        Command::UserInput(InputEvent::MoveToPrevMatch(n))
            if p.search_state.search_term.is_some() =>
        {
            // If no matches, return immediately
            if p.search_state.search_idx.is_empty() {
                return;
            }
            // Decrement the s_mark and get the preceding index
            p.search_state.search_mark = p.search_state.search_mark.saturating_sub(n);
            if let Some(y) = p
                .search_state
                .search_idx
                .iter()
                .nth(p.search_state.search_mark)
            {
                // If the index is less than or equal to the upper_mark, then set y to the new upper_mark
                if *y < p.upper_mark {
                    command_queue.push_back(Command::Io(IoCommand::SetUpperMark(*y)));
                    p.format_prompt();
                    command_queue.push_back(Command::Io(IoCommand::RedrawPrompt));
                }
            }
        }

        Command::UserInput(InputEvent::HorizontalScroll(val)) => {
            p.screen.line_wrapping = val;
            p.reformat_display();
            command_queue.push_back(Command::Io(IoCommand::RedrawDisplay));
        }

        Command::AppendData(text) => {
            let prev_unterminated = p.screen.unterminated;
            let prev_fmt_lines_count = p.screen.formatted_lines_count();
            let append_style = p.append_str(text.as_str());

            if append_style == AppendStyle::FullRedraw {
                command_queue.push_back(Command::Io(IoCommand::RedrawDisplay));
                return;
            }

            command_queue.push_back(Command::Io(IoCommand::DrawAppendedText(
                prev_unterminated,
                prev_fmt_lines_count,
                append_style,
            )));

            if p.follow_output {
                command_queue.push_back(Command::Io(IoCommand::SetUpperMark(
                    p.screen.formatted_lines_count(),
                )));
            }
        }

        Command::SetPrompt(ref text) | Command::SendMessage(ref text) => {
            if let Command::SetPrompt(_) = ev {
                p.prompt = text.clone();
            } else {
                p.message = Some(text.clone());
            }
            p.format_prompt();
            command_queue.push_back(Command::Io(IoCommand::RedrawPrompt));
        }
        Command::SetLineNumbers(ln) => {
            p.line_numbers = ln;
            p.reformat_display();
            command_queue.push_back(Command::Io(IoCommand::RedrawDisplay));
        }
        Command::SetExitStrategy(es) => {
            p.hooks.remove_callback(Hook::PostPagerExit, 1);
            if es == ExitStrategy::ProcessQuit {
                p.hooks.add_callback(
                    Hook::PostPagerExit,
                    1,
                    Box::new(|_| {
                        std::process::exit(1);
                    }),
                );
            } else {
                p.hooks
                    .add_callback(Hook::PostPagerExit, 1, Box::new(|_| {}));
            }
        }
        Command::LineWrapping(lw) => {
            p.screen.line_wrapping = lw;
            p.reformat_display();
        }
        #[cfg(feature = "static_output")]
        Command::SetRunNoOverflow(val) => p.run_no_overflow = val,
        #[cfg(feature = "search")]
        Command::IncrementalSearchCondition(cb) => p.search_state.incremental_search_condition = cb,
        Command::SetInputClassifier(clf) => p.input_classifier = clf,
        Command::AddExitCallback(cb) => p.exit_callbacks.push(cb),
        Command::AddHook(hook, id, cb) => p.hooks.add_callback(hook, id, cb),
        Command::RemoveHook(hook, id) => {
            p.hooks.remove_callback(hook, id);
        }
        Command::ShowPrompt(show) => p.show_prompt = show,
        Command::FollowOutput(follow_output)
        | Command::UserInput(InputEvent::FollowOutput(follow_output)) => {
            p.follow_output = follow_output;
            command_queue.push_back(Command::UserInput(InputEvent::UpdateUpperMark(
                p.screen.formatted_lines_count(),
            )));
            p.format_prompt();
            command_queue.push_back(Command::Io(IoCommand::RedrawPrompt));
        }
        Command::UserInput(_) => {}
        Command::Io(_) => unreachable!(),
    }
}

#[cfg_attr(
    not(feature = "search"),
    allow(unused_variables),
    allow(clippy::needless_pass_by_ref_mut)
)]
pub fn handle_io_command(
    internal_command: IoCommand,
    mut out: &mut impl Write,
    p: &mut PagerState,
    command_queue: &mut CommandQueue,
    #[cfg(feature = "search")] user_input_active: &Arc<(Mutex<bool>, Condvar)>,
) -> Result<(), MinusError> {
    if p.running.lock().is_uninitialized() {
        return Ok(());
    }
    match internal_command {
        IoCommand::RedrawPrompt => {
            display::write_prompt(out, &p.displayed_prompt, p.rows.try_into().unwrap())?;
        }
        IoCommand::RedrawDisplay => {
            display::draw_full(&mut out, p)?;
        }
        IoCommand::SetUpperMark(mut um) => {
            display::draw_for_change(out, p, &mut um)?;
            let line_count = p.screen.formatted_lines_count();
            if um >= line_count.saturating_sub(p.rows.saturating_sub(1)) && line_count > p.rows {
                p.run_hooks(Hook::EofReached);
            }
            p.upper_mark = um;
        }
        IoCommand::DrawAppendedText(prev_unterminated, prev_fmt_lines_count, append_style) => {
            let AppendStyle::PartialUpdate(bounds) = append_style else {
                unreachable!();
            };
            let fmt_lines = p.render_rows_for_display(bounds.0, bounds.1);
            display::draw_append_text(
                out,
                p.rows,
                prev_unterminated,
                prev_fmt_lines_count,
                &fmt_lines,
            )?;
        }
        #[cfg(feature = "search")]
        IoCommand::FetchSearchQuery => {
            // Pause the main user input thread, read search query and then restart the main input thread
            let (lock, cvar) = (&user_input_active.0, &user_input_active.1);
            let mut active = lock.lock();
            *active = false;
            drop(active);
            cvar.notify_one();
            let search_result = search::fetch_input(&mut out, p)?;
            let mut active = lock.lock();
            *active = true;
            drop(active);
            cvar.notify_one();

            // If we only have compiled regex cached, use that otherwise compile the original
            // string query if its not empty
            p.search_state.search_term = if search_result.compiled_regex.is_some() {
                search_result.compiled_regex
            } else if !search_result.string.is_empty() {
                let compiled_regex = regex::Regex::new(&search_result.string).ok();
                if compiled_regex.is_none() {
                    command_queue.push_back(Command::SendMessage(
                        "Invalid regular expression. Press Enter".to_string(),
                    ));
                    return Ok(());
                }
                compiled_regex
            } else {
                command_queue.push_back(Command::Io(IoCommand::RedrawDisplay));
                return Ok(());
            };

            p.reformat_display();
            p.upper_mark = *p
                .search_state
                .search_idx
                .iter()
                .nth(p.search_state.search_mark)
                .unwrap();
            command_queue.push_back(Command::Io(IoCommand::RedrawDisplay));
            command_queue.push_back(Command::Io(IoCommand::RedrawPrompt));
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::super::commands::{Command, IoCommand};
    use super::handle_event;
    use crate::{PagerState, input::InputEvent, minus_core::CommandQueue, state::Selection};
    use std::fmt::Write;
    use std::sync::{Arc, atomic::AtomicBool};

    const TEST_STR: &str = "This is some sample text";

    // Tests for event emitting functions of Pager
    #[test]
    #[cfg(any(feature = "dynamic_output", feature = "static_output"))]
    fn set_data() {
        let mut ps = PagerState::new().unwrap();
        let ev = Command::SetData(TEST_STR.to_string());
        let mut command_queue = CommandQueue::new_zero();

        handle_event(
            ev,
            &mut ps,
            &mut command_queue,
            &Arc::new(AtomicBool::new(false)),
        );

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

    #[test]
    fn append_str() {
        let mut ps = PagerState::new().unwrap();
        let ev1 = Command::AppendData(format!("{TEST_STR}\n"));
        let ev2 = Command::AppendData(TEST_STR.to_string());
        let mut command_queue = CommandQueue::new_zero();

        handle_event(
            ev1,
            &mut ps,
            &mut command_queue,
            &Arc::new(AtomicBool::new(false)),
        );
        handle_event(
            ev2,
            &mut ps,
            &mut command_queue,
            &Arc::new(AtomicBool::new(false)),
        );
        assert_eq!(
            ps.screen.formatted_lines,
            vec![TEST_STR.to_string(), TEST_STR.to_string()]
        );
    }

    #[test]
    #[cfg(any(feature = "dynamic_output", feature = "static_output"))]
    fn set_prompt() {
        let mut ps = PagerState::new().unwrap();
        let ev = Command::SetPrompt(TEST_STR.to_string());
        let mut command_queue = CommandQueue::new_zero();

        handle_event(
            ev,
            &mut ps,
            &mut command_queue,
            &Arc::new(AtomicBool::new(false)),
        );
        assert_eq!(ps.prompt, TEST_STR.to_string());
    }

    #[test]
    #[cfg(any(feature = "dynamic_output", feature = "static_output"))]
    fn send_message() {
        let mut ps = PagerState::new().unwrap();
        let ev = Command::SendMessage(TEST_STR.to_string());
        let mut command_queue = CommandQueue::new_zero();

        handle_event(
            ev,
            &mut ps,
            &mut command_queue,
            &Arc::new(AtomicBool::new(false)),
        );
        assert_eq!(ps.message.unwrap(), TEST_STR.to_string());
    }

    #[test]
    #[cfg(feature = "static_output")]
    fn set_run_no_overflow() {
        let mut ps = PagerState::new().unwrap();
        let ev = Command::SetRunNoOverflow(false);
        let mut command_queue = CommandQueue::new_zero();

        handle_event(
            ev,
            &mut ps,
            &mut command_queue,
            &Arc::new(AtomicBool::new(false)),
        );
        assert!(!ps.run_no_overflow);
    }

    #[test]
    fn add_exit_callback() {
        let mut ps = PagerState::new().unwrap();
        let ev = Command::AddExitCallback(Box::new(|| println!("Hello World")));
        let mut command_queue = CommandQueue::new_zero();

        handle_event(
            ev,
            &mut ps,
            &mut command_queue,
            &Arc::new(AtomicBool::new(false)),
        );
        assert_eq!(ps.exit_callbacks.len(), 1);
    }

    #[test]
    fn update_selection_scrolls_up_at_top_edge() {
        let mut ps = PagerState::new().unwrap();
        ps.rows = 5;
        ps.screen.orig_text = (0..10).fold(String::new(), |mut t, idx| {
            let _ = writeln!(t, "line {idx}");
            t
        });
        ps.reformat_display();
        ps.upper_mark = 3;
        ps.selection_anchor = Some(Selection {
            absolute_row: 3,
            col: 0,
        });
        ps.selection = ps.selection_anchor;
        let mut command_queue = CommandQueue::new_zero();

        handle_event(
            Command::UserInput(InputEvent::UpdateSelection { x: 0, y: 0 }),
            &mut ps,
            &mut command_queue,
            &Arc::new(AtomicBool::new(false)),
        );

        assert_eq!(ps.upper_mark, 2);
        assert_eq!(
            ps.selection,
            Some(Selection {
                absolute_row: 2,
                col: 0,
            })
        );
        assert_eq!(
            command_queue.pop_front(),
            Some(Command::Io(IoCommand::RedrawDisplay))
        );
    }

    #[test]
    fn update_selection_scrolls_down_at_bottom_edge() {
        let mut ps = PagerState::new().unwrap();
        ps.rows = 5;
        ps.screen.orig_text = (0..10).fold(String::new(), |mut t, idx| {
            let _ = writeln!(t, "line {idx}");
            t
        });
        ps.reformat_display();
        ps.upper_mark = 3;
        ps.selection_anchor = Some(Selection {
            absolute_row: 3,
            col: 0,
        });
        ps.selection = ps.selection_anchor;
        let mut command_queue = CommandQueue::new_zero();

        handle_event(
            Command::UserInput(InputEvent::UpdateSelection { x: 0, y: 4 }),
            &mut ps,
            &mut command_queue,
            &Arc::new(AtomicBool::new(false)),
        );

        assert_eq!(ps.upper_mark, 4);
        assert_eq!(
            ps.selection,
            Some(Selection {
                absolute_row: 7,
                col: 0,
            })
        );
        assert_eq!(
            command_queue.pop_front(),
            Some(Command::Io(IoCommand::RedrawDisplay))
        );
    }

    #[test]
    fn update_selection_clamps_scroll_at_bottom_bound() {
        let mut ps = PagerState::new().unwrap();
        ps.rows = 5;
        ps.screen.orig_text = (0..6).fold(String::new(), |mut t, idx| {
            let _ = writeln!(t, "line {idx}");
            t
        });
        ps.reformat_display();
        ps.upper_mark = 2;
        ps.selection_anchor = Some(Selection {
            absolute_row: 2,
            col: 0,
        });
        ps.selection = ps.selection_anchor;
        let mut command_queue = CommandQueue::new_zero();

        handle_event(
            Command::UserInput(InputEvent::UpdateSelection { x: 0, y: 10 }),
            &mut ps,
            &mut command_queue,
            &Arc::new(AtomicBool::new(false)),
        );

        assert_eq!(ps.upper_mark, 2);
        assert_eq!(
            ps.selection,
            Some(Selection {
                absolute_row: 5,
                col: 0,
            })
        );
        assert_eq!(
            command_queue.pop_front(),
            Some(Command::Io(IoCommand::RedrawDisplay))
        );
    }

    #[test]
    #[cfg(feature = "clipboard")]
    fn copy_selection_clears_selection_and_redraws() {
        let mut ps = PagerState::new().unwrap();
        ps.selection = Some(Selection {
            absolute_row: 0,
            col: 0,
        });
        let mut command_queue = CommandQueue::new_zero();

        handle_event(
            Command::UserInput(InputEvent::CopySelection),
            &mut ps,
            &mut command_queue,
            &Arc::new(AtomicBool::new(false)),
        );

        assert_eq!(ps.selection, None);
        assert_eq!(ps.selection_anchor, None);
        assert_eq!(
            command_queue.pop_front(),
            Some(Command::Io(IoCommand::RedrawDisplay))
        );
    }
}