pub struct Utf8Char { /* private fields */ }
Expand description

Class to manage the terminal’s individual UTF-8 characters. Includes fg/bg color, attributes (BOLD, UNDERLINE..)

Implementations§

source§

impl<'a> Utf8Char

source

pub fn new(c: u8) -> Self

Construct a new Utf8Char, single-byte only. This is really only useful for testing. ‘c’ must be “printable” ASCII in the range (0x20 <= c <= 0x7e). Anything outside of that is silently ignored.

Allocated Utf8Char will never be deleted.

Examples found in repository?
examples/terminal.rs (line 508)
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
fn mb_test4_cb(_choice: &mut fltk::menu::Choice, term: &mut Terminal) {
    // Test the Utf8Char primitive
    let uc = Utf8Char::new(b'Q');
    let uc1 = uc.text_utf8();
    assert_eq!(&uc1, &[b'Q']);
    assert_eq!(&uc.attrib(), &Attrib::Normal);
    assert_eq!(
        &uc.charflags(),
        &(CharFlags::FG_XTERM | CharFlags::BG_XTERM)
    );
    assert_eq!(&uc.fgcolor(), &Color::XtermWhite);
    assert_eq!(&uc.bgcolor(), &Color::TransparentBg);

    let ring_rows = term.ring_rows();

    // println!();
    // dbg!(term.disp_srow(), term.disp_erow(), term.disp_rows(), term.ring_cols(), term.ring_srow(), term.ring_erow() );
    // dbg!(term.hist_srow(), term.hist_erow(), term.hist_rows(), ring_rows );
    // dbg!(term.offset(), term.hist_use_srow(), term.hist_use() );

    term.take_focus().unwrap();
    term.clear_history();
    assert_eq!(term.history_use(), 0);

    // Subtract row numbers, modulo `rows`
    fn row_diff(rows: i32, a: i32, b: i32) -> i32 {
        match a - b {
            n if n < 0 => n + rows,
            n => n
        }
    }
    // disp_srow is always 1 greater than hist_erow, modulo (ring_rows+1)
    assert_eq!(row_diff(ring_rows, term.disp_srow(), term.hist_erow()), 1);
    assert!(term.disp_srow() >= 0);
    assert!(term.disp_erow() >= 0);
    assert!(term.hist_srow() >= 0);
    assert!(term.hist_erow() >= 0);
    assert!(term.offset() >= 0);
    assert!(term.disp_srow() <= ring_rows);
    assert!(term.disp_erow() <= ring_rows);
    assert!(term.hist_srow() <= ring_rows);
    assert!(term.hist_erow() <= ring_rows);
    assert!(term.offset() <= ring_rows);

    assert_eq!(term.ring_srow(), 0);
    assert_eq!(term.ring_erow(), ring_rows - 1);
    assert_eq!(
        row_diff(ring_rows, term.disp_erow(), term.disp_srow()) + 1,
        term.disp_rows()
    );
    assert_eq!(
        row_diff(ring_rows, term.hist_erow(), term.hist_srow()) + 1,
        term.hist_rows()
    );

    assert_eq!(term.ring_erow(), term.ring_rows() - 1);
    assert_eq!(term.ring_srow(), 0);

    // Check the different cols methods, which should all return the same answer
    assert!(term.disp_cols() > 10);
    assert_eq!(term.disp_cols(), term.ring_cols());
    assert_eq!(term.disp_cols(), term.hist_cols());

    // Redundant protected vs public methods:
    assert_eq!(term.disp_cols(), term.display_columns());
    assert_eq!(term.disp_rows(), term.display_rows());

    /// Local function to read back all rows from the display into a long string.
    /// Does not include scrollback history.
    /// Trims trailing blanks on each line
    fn read_disp(term: &Terminal) -> String {
        let rows = term.display_rows();
        let mut text: Vec<u8> = Vec::with_capacity((rows * 64) as usize);
        for row in 0..rows {
            let r = term.u8c_disp_row(row).trim();
            // Iterate through a row, accumulating [u8]
            for c in r.iter() {
                // Note: Sometimes utf-8 length is > 1
                text.extend_from_slice(c.text_utf8());
            }
            text.extend_from_slice(b"\n");
        }
        // Return the result as a string
        std::str::from_utf8(&text).unwrap().to_string()
    }

    term.clear();
    term.append("Top line  ↑ (up-arrow)");
    term.set_text_attrib(Attrib::Underline);
    term.append("  ");
    term.set_text_attrib(Attrib::Normal);
    term.append("  \n");
    let mut text_out = read_disp(term);
    // Trim trailing empty lines
    text_out = text_out.trim_end_matches(&"\n\n").to_string();
    // The two plain blanks at the end will be trimmed, the two underlined blanks will be retained.
    assert_eq!(text_out, "Top line  ↑ (up-arrow)  \n");
    let r = term.u8c_disp_row(0);
    assert_eq!(r.col(0).text_utf8(), b"T");
    assert_eq!(r.col(10).text_utf8(), b"\xe2\x86\x91");     // UTF-8 up-arrow
    assert_eq!(r.col(24).text_utf8(), b" ");                // First blank after test text, NOT trimmed
    let r = term.u8c_disp_row(1);
    assert_eq!(r.col(0).text_utf8(), b" ");                 // Second row starts with blanks
    assert_eq!(r.col(1).text_utf8(), b" ");                 // Second row is full of blanks


    // Clear the screen again, then append test text, then read it back and compare
    let test_text =
"The wind was a torrent of darkness among the gusty trees.
The moon was a ghostly galleon tossed upon cloudy seas.
The road was a ribbon of moonlight over the purple moor,
And the highwayman came riding—
            Riding—riding—
The highwayman came riding, up to the old inn-door.";

term.clear_history();
    term.clear();
    let bg_save = term.text_bg_color();
    let fg_save = term.text_fg_color();
    term.set_text_bg_color(Color::DarkBlue);    // Set spooky colors
    term.set_text_fg_color(Color::from_rgb(0x40, 0x40, 0xff));
    term.append(test_text);
    term.set_text_bg_color(bg_save);
    term.set_text_fg_color(fg_save);

    let mut text_out = read_disp(term);
    // Trim trailing empty lines
    text_out = text_out.trim_end_matches(&"\n\n").to_string();
    assert_eq!(test_text, text_out);

    assert_eq!(row_diff(ring_rows, term.disp_srow(), term.hist_erow()), 1);

    assert_eq!(term.ring_srow(), 0);
    assert_eq!(term.ring_erow(), ring_rows - 1);
    assert_eq!(
        row_diff(ring_rows, term.disp_erow(), term.disp_srow()) + 1,
        term.disp_rows()
    );
    assert_eq!(
        row_diff(ring_rows, term.hist_erow(), term.hist_srow()) + 1,
        term.hist_rows()
    );

    term.append(&format!(
        "\n\nScreen has {} rows of {} columns.\n",
        term.disp_rows(),
        term.disp_cols()
    ));
    term.set_text_attrib(Attrib::Italic);
    term.append("Done!");
    term.set_text_attrib(Attrib::Normal);
}
source

pub fn attrib(&self) -> Attrib

Return the attributes for this character.

Examples found in repository?
examples/terminal.rs (line 511)
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
fn mb_test4_cb(_choice: &mut fltk::menu::Choice, term: &mut Terminal) {
    // Test the Utf8Char primitive
    let uc = Utf8Char::new(b'Q');
    let uc1 = uc.text_utf8();
    assert_eq!(&uc1, &[b'Q']);
    assert_eq!(&uc.attrib(), &Attrib::Normal);
    assert_eq!(
        &uc.charflags(),
        &(CharFlags::FG_XTERM | CharFlags::BG_XTERM)
    );
    assert_eq!(&uc.fgcolor(), &Color::XtermWhite);
    assert_eq!(&uc.bgcolor(), &Color::TransparentBg);

    let ring_rows = term.ring_rows();

    // println!();
    // dbg!(term.disp_srow(), term.disp_erow(), term.disp_rows(), term.ring_cols(), term.ring_srow(), term.ring_erow() );
    // dbg!(term.hist_srow(), term.hist_erow(), term.hist_rows(), ring_rows );
    // dbg!(term.offset(), term.hist_use_srow(), term.hist_use() );

    term.take_focus().unwrap();
    term.clear_history();
    assert_eq!(term.history_use(), 0);

    // Subtract row numbers, modulo `rows`
    fn row_diff(rows: i32, a: i32, b: i32) -> i32 {
        match a - b {
            n if n < 0 => n + rows,
            n => n
        }
    }
    // disp_srow is always 1 greater than hist_erow, modulo (ring_rows+1)
    assert_eq!(row_diff(ring_rows, term.disp_srow(), term.hist_erow()), 1);
    assert!(term.disp_srow() >= 0);
    assert!(term.disp_erow() >= 0);
    assert!(term.hist_srow() >= 0);
    assert!(term.hist_erow() >= 0);
    assert!(term.offset() >= 0);
    assert!(term.disp_srow() <= ring_rows);
    assert!(term.disp_erow() <= ring_rows);
    assert!(term.hist_srow() <= ring_rows);
    assert!(term.hist_erow() <= ring_rows);
    assert!(term.offset() <= ring_rows);

    assert_eq!(term.ring_srow(), 0);
    assert_eq!(term.ring_erow(), ring_rows - 1);
    assert_eq!(
        row_diff(ring_rows, term.disp_erow(), term.disp_srow()) + 1,
        term.disp_rows()
    );
    assert_eq!(
        row_diff(ring_rows, term.hist_erow(), term.hist_srow()) + 1,
        term.hist_rows()
    );

    assert_eq!(term.ring_erow(), term.ring_rows() - 1);
    assert_eq!(term.ring_srow(), 0);

    // Check the different cols methods, which should all return the same answer
    assert!(term.disp_cols() > 10);
    assert_eq!(term.disp_cols(), term.ring_cols());
    assert_eq!(term.disp_cols(), term.hist_cols());

    // Redundant protected vs public methods:
    assert_eq!(term.disp_cols(), term.display_columns());
    assert_eq!(term.disp_rows(), term.display_rows());

    /// Local function to read back all rows from the display into a long string.
    /// Does not include scrollback history.
    /// Trims trailing blanks on each line
    fn read_disp(term: &Terminal) -> String {
        let rows = term.display_rows();
        let mut text: Vec<u8> = Vec::with_capacity((rows * 64) as usize);
        for row in 0..rows {
            let r = term.u8c_disp_row(row).trim();
            // Iterate through a row, accumulating [u8]
            for c in r.iter() {
                // Note: Sometimes utf-8 length is > 1
                text.extend_from_slice(c.text_utf8());
            }
            text.extend_from_slice(b"\n");
        }
        // Return the result as a string
        std::str::from_utf8(&text).unwrap().to_string()
    }

    term.clear();
    term.append("Top line  ↑ (up-arrow)");
    term.set_text_attrib(Attrib::Underline);
    term.append("  ");
    term.set_text_attrib(Attrib::Normal);
    term.append("  \n");
    let mut text_out = read_disp(term);
    // Trim trailing empty lines
    text_out = text_out.trim_end_matches(&"\n\n").to_string();
    // The two plain blanks at the end will be trimmed, the two underlined blanks will be retained.
    assert_eq!(text_out, "Top line  ↑ (up-arrow)  \n");
    let r = term.u8c_disp_row(0);
    assert_eq!(r.col(0).text_utf8(), b"T");
    assert_eq!(r.col(10).text_utf8(), b"\xe2\x86\x91");     // UTF-8 up-arrow
    assert_eq!(r.col(24).text_utf8(), b" ");                // First blank after test text, NOT trimmed
    let r = term.u8c_disp_row(1);
    assert_eq!(r.col(0).text_utf8(), b" ");                 // Second row starts with blanks
    assert_eq!(r.col(1).text_utf8(), b" ");                 // Second row is full of blanks


    // Clear the screen again, then append test text, then read it back and compare
    let test_text =
"The wind was a torrent of darkness among the gusty trees.
The moon was a ghostly galleon tossed upon cloudy seas.
The road was a ribbon of moonlight over the purple moor,
And the highwayman came riding—
            Riding—riding—
The highwayman came riding, up to the old inn-door.";

term.clear_history();
    term.clear();
    let bg_save = term.text_bg_color();
    let fg_save = term.text_fg_color();
    term.set_text_bg_color(Color::DarkBlue);    // Set spooky colors
    term.set_text_fg_color(Color::from_rgb(0x40, 0x40, 0xff));
    term.append(test_text);
    term.set_text_bg_color(bg_save);
    term.set_text_fg_color(fg_save);

    let mut text_out = read_disp(term);
    // Trim trailing empty lines
    text_out = text_out.trim_end_matches(&"\n\n").to_string();
    assert_eq!(test_text, text_out);

    assert_eq!(row_diff(ring_rows, term.disp_srow(), term.hist_erow()), 1);

    assert_eq!(term.ring_srow(), 0);
    assert_eq!(term.ring_erow(), ring_rows - 1);
    assert_eq!(
        row_diff(ring_rows, term.disp_erow(), term.disp_srow()) + 1,
        term.disp_rows()
    );
    assert_eq!(
        row_diff(ring_rows, term.hist_erow(), term.hist_srow()) + 1,
        term.hist_rows()
    );

    term.append(&format!(
        "\n\nScreen has {} rows of {} columns.\n",
        term.disp_rows(),
        term.disp_cols()
    ));
    term.set_text_attrib(Attrib::Italic);
    term.append("Done!");
    term.set_text_attrib(Attrib::Normal);
}
source

pub fn bgcolor(&self) -> Color

Return the background color for this character.

Examples found in repository?
examples/terminal.rs (line 517)
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
fn mb_test4_cb(_choice: &mut fltk::menu::Choice, term: &mut Terminal) {
    // Test the Utf8Char primitive
    let uc = Utf8Char::new(b'Q');
    let uc1 = uc.text_utf8();
    assert_eq!(&uc1, &[b'Q']);
    assert_eq!(&uc.attrib(), &Attrib::Normal);
    assert_eq!(
        &uc.charflags(),
        &(CharFlags::FG_XTERM | CharFlags::BG_XTERM)
    );
    assert_eq!(&uc.fgcolor(), &Color::XtermWhite);
    assert_eq!(&uc.bgcolor(), &Color::TransparentBg);

    let ring_rows = term.ring_rows();

    // println!();
    // dbg!(term.disp_srow(), term.disp_erow(), term.disp_rows(), term.ring_cols(), term.ring_srow(), term.ring_erow() );
    // dbg!(term.hist_srow(), term.hist_erow(), term.hist_rows(), ring_rows );
    // dbg!(term.offset(), term.hist_use_srow(), term.hist_use() );

    term.take_focus().unwrap();
    term.clear_history();
    assert_eq!(term.history_use(), 0);

    // Subtract row numbers, modulo `rows`
    fn row_diff(rows: i32, a: i32, b: i32) -> i32 {
        match a - b {
            n if n < 0 => n + rows,
            n => n
        }
    }
    // disp_srow is always 1 greater than hist_erow, modulo (ring_rows+1)
    assert_eq!(row_diff(ring_rows, term.disp_srow(), term.hist_erow()), 1);
    assert!(term.disp_srow() >= 0);
    assert!(term.disp_erow() >= 0);
    assert!(term.hist_srow() >= 0);
    assert!(term.hist_erow() >= 0);
    assert!(term.offset() >= 0);
    assert!(term.disp_srow() <= ring_rows);
    assert!(term.disp_erow() <= ring_rows);
    assert!(term.hist_srow() <= ring_rows);
    assert!(term.hist_erow() <= ring_rows);
    assert!(term.offset() <= ring_rows);

    assert_eq!(term.ring_srow(), 0);
    assert_eq!(term.ring_erow(), ring_rows - 1);
    assert_eq!(
        row_diff(ring_rows, term.disp_erow(), term.disp_srow()) + 1,
        term.disp_rows()
    );
    assert_eq!(
        row_diff(ring_rows, term.hist_erow(), term.hist_srow()) + 1,
        term.hist_rows()
    );

    assert_eq!(term.ring_erow(), term.ring_rows() - 1);
    assert_eq!(term.ring_srow(), 0);

    // Check the different cols methods, which should all return the same answer
    assert!(term.disp_cols() > 10);
    assert_eq!(term.disp_cols(), term.ring_cols());
    assert_eq!(term.disp_cols(), term.hist_cols());

    // Redundant protected vs public methods:
    assert_eq!(term.disp_cols(), term.display_columns());
    assert_eq!(term.disp_rows(), term.display_rows());

    /// Local function to read back all rows from the display into a long string.
    /// Does not include scrollback history.
    /// Trims trailing blanks on each line
    fn read_disp(term: &Terminal) -> String {
        let rows = term.display_rows();
        let mut text: Vec<u8> = Vec::with_capacity((rows * 64) as usize);
        for row in 0..rows {
            let r = term.u8c_disp_row(row).trim();
            // Iterate through a row, accumulating [u8]
            for c in r.iter() {
                // Note: Sometimes utf-8 length is > 1
                text.extend_from_slice(c.text_utf8());
            }
            text.extend_from_slice(b"\n");
        }
        // Return the result as a string
        std::str::from_utf8(&text).unwrap().to_string()
    }

    term.clear();
    term.append("Top line  ↑ (up-arrow)");
    term.set_text_attrib(Attrib::Underline);
    term.append("  ");
    term.set_text_attrib(Attrib::Normal);
    term.append("  \n");
    let mut text_out = read_disp(term);
    // Trim trailing empty lines
    text_out = text_out.trim_end_matches(&"\n\n").to_string();
    // The two plain blanks at the end will be trimmed, the two underlined blanks will be retained.
    assert_eq!(text_out, "Top line  ↑ (up-arrow)  \n");
    let r = term.u8c_disp_row(0);
    assert_eq!(r.col(0).text_utf8(), b"T");
    assert_eq!(r.col(10).text_utf8(), b"\xe2\x86\x91");     // UTF-8 up-arrow
    assert_eq!(r.col(24).text_utf8(), b" ");                // First blank after test text, NOT trimmed
    let r = term.u8c_disp_row(1);
    assert_eq!(r.col(0).text_utf8(), b" ");                 // Second row starts with blanks
    assert_eq!(r.col(1).text_utf8(), b" ");                 // Second row is full of blanks


    // Clear the screen again, then append test text, then read it back and compare
    let test_text =
"The wind was a torrent of darkness among the gusty trees.
The moon was a ghostly galleon tossed upon cloudy seas.
The road was a ribbon of moonlight over the purple moor,
And the highwayman came riding—
            Riding—riding—
The highwayman came riding, up to the old inn-door.";

term.clear_history();
    term.clear();
    let bg_save = term.text_bg_color();
    let fg_save = term.text_fg_color();
    term.set_text_bg_color(Color::DarkBlue);    // Set spooky colors
    term.set_text_fg_color(Color::from_rgb(0x40, 0x40, 0xff));
    term.append(test_text);
    term.set_text_bg_color(bg_save);
    term.set_text_fg_color(fg_save);

    let mut text_out = read_disp(term);
    // Trim trailing empty lines
    text_out = text_out.trim_end_matches(&"\n\n").to_string();
    assert_eq!(test_text, text_out);

    assert_eq!(row_diff(ring_rows, term.disp_srow(), term.hist_erow()), 1);

    assert_eq!(term.ring_srow(), 0);
    assert_eq!(term.ring_erow(), ring_rows - 1);
    assert_eq!(
        row_diff(ring_rows, term.disp_erow(), term.disp_srow()) + 1,
        term.disp_rows()
    );
    assert_eq!(
        row_diff(ring_rows, term.hist_erow(), term.hist_srow()) + 1,
        term.hist_rows()
    );

    term.append(&format!(
        "\n\nScreen has {} rows of {} columns.\n",
        term.disp_rows(),
        term.disp_cols()
    ));
    term.set_text_attrib(Attrib::Italic);
    term.append("Done!");
    term.set_text_attrib(Attrib::Normal);
}
source

pub fn fgcolor(&self) -> Color

Return the foreground color for this character.

Examples found in repository?
examples/terminal.rs (line 516)
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
fn mb_test4_cb(_choice: &mut fltk::menu::Choice, term: &mut Terminal) {
    // Test the Utf8Char primitive
    let uc = Utf8Char::new(b'Q');
    let uc1 = uc.text_utf8();
    assert_eq!(&uc1, &[b'Q']);
    assert_eq!(&uc.attrib(), &Attrib::Normal);
    assert_eq!(
        &uc.charflags(),
        &(CharFlags::FG_XTERM | CharFlags::BG_XTERM)
    );
    assert_eq!(&uc.fgcolor(), &Color::XtermWhite);
    assert_eq!(&uc.bgcolor(), &Color::TransparentBg);

    let ring_rows = term.ring_rows();

    // println!();
    // dbg!(term.disp_srow(), term.disp_erow(), term.disp_rows(), term.ring_cols(), term.ring_srow(), term.ring_erow() );
    // dbg!(term.hist_srow(), term.hist_erow(), term.hist_rows(), ring_rows );
    // dbg!(term.offset(), term.hist_use_srow(), term.hist_use() );

    term.take_focus().unwrap();
    term.clear_history();
    assert_eq!(term.history_use(), 0);

    // Subtract row numbers, modulo `rows`
    fn row_diff(rows: i32, a: i32, b: i32) -> i32 {
        match a - b {
            n if n < 0 => n + rows,
            n => n
        }
    }
    // disp_srow is always 1 greater than hist_erow, modulo (ring_rows+1)
    assert_eq!(row_diff(ring_rows, term.disp_srow(), term.hist_erow()), 1);
    assert!(term.disp_srow() >= 0);
    assert!(term.disp_erow() >= 0);
    assert!(term.hist_srow() >= 0);
    assert!(term.hist_erow() >= 0);
    assert!(term.offset() >= 0);
    assert!(term.disp_srow() <= ring_rows);
    assert!(term.disp_erow() <= ring_rows);
    assert!(term.hist_srow() <= ring_rows);
    assert!(term.hist_erow() <= ring_rows);
    assert!(term.offset() <= ring_rows);

    assert_eq!(term.ring_srow(), 0);
    assert_eq!(term.ring_erow(), ring_rows - 1);
    assert_eq!(
        row_diff(ring_rows, term.disp_erow(), term.disp_srow()) + 1,
        term.disp_rows()
    );
    assert_eq!(
        row_diff(ring_rows, term.hist_erow(), term.hist_srow()) + 1,
        term.hist_rows()
    );

    assert_eq!(term.ring_erow(), term.ring_rows() - 1);
    assert_eq!(term.ring_srow(), 0);

    // Check the different cols methods, which should all return the same answer
    assert!(term.disp_cols() > 10);
    assert_eq!(term.disp_cols(), term.ring_cols());
    assert_eq!(term.disp_cols(), term.hist_cols());

    // Redundant protected vs public methods:
    assert_eq!(term.disp_cols(), term.display_columns());
    assert_eq!(term.disp_rows(), term.display_rows());

    /// Local function to read back all rows from the display into a long string.
    /// Does not include scrollback history.
    /// Trims trailing blanks on each line
    fn read_disp(term: &Terminal) -> String {
        let rows = term.display_rows();
        let mut text: Vec<u8> = Vec::with_capacity((rows * 64) as usize);
        for row in 0..rows {
            let r = term.u8c_disp_row(row).trim();
            // Iterate through a row, accumulating [u8]
            for c in r.iter() {
                // Note: Sometimes utf-8 length is > 1
                text.extend_from_slice(c.text_utf8());
            }
            text.extend_from_slice(b"\n");
        }
        // Return the result as a string
        std::str::from_utf8(&text).unwrap().to_string()
    }

    term.clear();
    term.append("Top line  ↑ (up-arrow)");
    term.set_text_attrib(Attrib::Underline);
    term.append("  ");
    term.set_text_attrib(Attrib::Normal);
    term.append("  \n");
    let mut text_out = read_disp(term);
    // Trim trailing empty lines
    text_out = text_out.trim_end_matches(&"\n\n").to_string();
    // The two plain blanks at the end will be trimmed, the two underlined blanks will be retained.
    assert_eq!(text_out, "Top line  ↑ (up-arrow)  \n");
    let r = term.u8c_disp_row(0);
    assert_eq!(r.col(0).text_utf8(), b"T");
    assert_eq!(r.col(10).text_utf8(), b"\xe2\x86\x91");     // UTF-8 up-arrow
    assert_eq!(r.col(24).text_utf8(), b" ");                // First blank after test text, NOT trimmed
    let r = term.u8c_disp_row(1);
    assert_eq!(r.col(0).text_utf8(), b" ");                 // Second row starts with blanks
    assert_eq!(r.col(1).text_utf8(), b" ");                 // Second row is full of blanks


    // Clear the screen again, then append test text, then read it back and compare
    let test_text =
"The wind was a torrent of darkness among the gusty trees.
The moon was a ghostly galleon tossed upon cloudy seas.
The road was a ribbon of moonlight over the purple moor,
And the highwayman came riding—
            Riding—riding—
The highwayman came riding, up to the old inn-door.";

term.clear_history();
    term.clear();
    let bg_save = term.text_bg_color();
    let fg_save = term.text_fg_color();
    term.set_text_bg_color(Color::DarkBlue);    // Set spooky colors
    term.set_text_fg_color(Color::from_rgb(0x40, 0x40, 0xff));
    term.append(test_text);
    term.set_text_bg_color(bg_save);
    term.set_text_fg_color(fg_save);

    let mut text_out = read_disp(term);
    // Trim trailing empty lines
    text_out = text_out.trim_end_matches(&"\n\n").to_string();
    assert_eq!(test_text, text_out);

    assert_eq!(row_diff(ring_rows, term.disp_srow(), term.hist_erow()), 1);

    assert_eq!(term.ring_srow(), 0);
    assert_eq!(term.ring_erow(), ring_rows - 1);
    assert_eq!(
        row_diff(ring_rows, term.disp_erow(), term.disp_srow()) + 1,
        term.disp_rows()
    );
    assert_eq!(
        row_diff(ring_rows, term.hist_erow(), term.hist_srow()) + 1,
        term.hist_rows()
    );

    term.append(&format!(
        "\n\nScreen has {} rows of {} columns.\n",
        term.disp_rows(),
        term.disp_cols()
    ));
    term.set_text_attrib(Attrib::Italic);
    term.append("Done!");
    term.set_text_attrib(Attrib::Normal);
}
source

pub fn charflags(&self) -> CharFlags

Return the xterm CharFlags bits

Examples found in repository?
examples/terminal.rs (line 513)
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
fn mb_test4_cb(_choice: &mut fltk::menu::Choice, term: &mut Terminal) {
    // Test the Utf8Char primitive
    let uc = Utf8Char::new(b'Q');
    let uc1 = uc.text_utf8();
    assert_eq!(&uc1, &[b'Q']);
    assert_eq!(&uc.attrib(), &Attrib::Normal);
    assert_eq!(
        &uc.charflags(),
        &(CharFlags::FG_XTERM | CharFlags::BG_XTERM)
    );
    assert_eq!(&uc.fgcolor(), &Color::XtermWhite);
    assert_eq!(&uc.bgcolor(), &Color::TransparentBg);

    let ring_rows = term.ring_rows();

    // println!();
    // dbg!(term.disp_srow(), term.disp_erow(), term.disp_rows(), term.ring_cols(), term.ring_srow(), term.ring_erow() );
    // dbg!(term.hist_srow(), term.hist_erow(), term.hist_rows(), ring_rows );
    // dbg!(term.offset(), term.hist_use_srow(), term.hist_use() );

    term.take_focus().unwrap();
    term.clear_history();
    assert_eq!(term.history_use(), 0);

    // Subtract row numbers, modulo `rows`
    fn row_diff(rows: i32, a: i32, b: i32) -> i32 {
        match a - b {
            n if n < 0 => n + rows,
            n => n
        }
    }
    // disp_srow is always 1 greater than hist_erow, modulo (ring_rows+1)
    assert_eq!(row_diff(ring_rows, term.disp_srow(), term.hist_erow()), 1);
    assert!(term.disp_srow() >= 0);
    assert!(term.disp_erow() >= 0);
    assert!(term.hist_srow() >= 0);
    assert!(term.hist_erow() >= 0);
    assert!(term.offset() >= 0);
    assert!(term.disp_srow() <= ring_rows);
    assert!(term.disp_erow() <= ring_rows);
    assert!(term.hist_srow() <= ring_rows);
    assert!(term.hist_erow() <= ring_rows);
    assert!(term.offset() <= ring_rows);

    assert_eq!(term.ring_srow(), 0);
    assert_eq!(term.ring_erow(), ring_rows - 1);
    assert_eq!(
        row_diff(ring_rows, term.disp_erow(), term.disp_srow()) + 1,
        term.disp_rows()
    );
    assert_eq!(
        row_diff(ring_rows, term.hist_erow(), term.hist_srow()) + 1,
        term.hist_rows()
    );

    assert_eq!(term.ring_erow(), term.ring_rows() - 1);
    assert_eq!(term.ring_srow(), 0);

    // Check the different cols methods, which should all return the same answer
    assert!(term.disp_cols() > 10);
    assert_eq!(term.disp_cols(), term.ring_cols());
    assert_eq!(term.disp_cols(), term.hist_cols());

    // Redundant protected vs public methods:
    assert_eq!(term.disp_cols(), term.display_columns());
    assert_eq!(term.disp_rows(), term.display_rows());

    /// Local function to read back all rows from the display into a long string.
    /// Does not include scrollback history.
    /// Trims trailing blanks on each line
    fn read_disp(term: &Terminal) -> String {
        let rows = term.display_rows();
        let mut text: Vec<u8> = Vec::with_capacity((rows * 64) as usize);
        for row in 0..rows {
            let r = term.u8c_disp_row(row).trim();
            // Iterate through a row, accumulating [u8]
            for c in r.iter() {
                // Note: Sometimes utf-8 length is > 1
                text.extend_from_slice(c.text_utf8());
            }
            text.extend_from_slice(b"\n");
        }
        // Return the result as a string
        std::str::from_utf8(&text).unwrap().to_string()
    }

    term.clear();
    term.append("Top line  ↑ (up-arrow)");
    term.set_text_attrib(Attrib::Underline);
    term.append("  ");
    term.set_text_attrib(Attrib::Normal);
    term.append("  \n");
    let mut text_out = read_disp(term);
    // Trim trailing empty lines
    text_out = text_out.trim_end_matches(&"\n\n").to_string();
    // The two plain blanks at the end will be trimmed, the two underlined blanks will be retained.
    assert_eq!(text_out, "Top line  ↑ (up-arrow)  \n");
    let r = term.u8c_disp_row(0);
    assert_eq!(r.col(0).text_utf8(), b"T");
    assert_eq!(r.col(10).text_utf8(), b"\xe2\x86\x91");     // UTF-8 up-arrow
    assert_eq!(r.col(24).text_utf8(), b" ");                // First blank after test text, NOT trimmed
    let r = term.u8c_disp_row(1);
    assert_eq!(r.col(0).text_utf8(), b" ");                 // Second row starts with blanks
    assert_eq!(r.col(1).text_utf8(), b" ");                 // Second row is full of blanks


    // Clear the screen again, then append test text, then read it back and compare
    let test_text =
"The wind was a torrent of darkness among the gusty trees.
The moon was a ghostly galleon tossed upon cloudy seas.
The road was a ribbon of moonlight over the purple moor,
And the highwayman came riding—
            Riding—riding—
The highwayman came riding, up to the old inn-door.";

term.clear_history();
    term.clear();
    let bg_save = term.text_bg_color();
    let fg_save = term.text_fg_color();
    term.set_text_bg_color(Color::DarkBlue);    // Set spooky colors
    term.set_text_fg_color(Color::from_rgb(0x40, 0x40, 0xff));
    term.append(test_text);
    term.set_text_bg_color(bg_save);
    term.set_text_fg_color(fg_save);

    let mut text_out = read_disp(term);
    // Trim trailing empty lines
    text_out = text_out.trim_end_matches(&"\n\n").to_string();
    assert_eq!(test_text, text_out);

    assert_eq!(row_diff(ring_rows, term.disp_srow(), term.hist_erow()), 1);

    assert_eq!(term.ring_srow(), 0);
    assert_eq!(term.ring_erow(), ring_rows - 1);
    assert_eq!(
        row_diff(ring_rows, term.disp_erow(), term.disp_srow()) + 1,
        term.disp_rows()
    );
    assert_eq!(
        row_diff(ring_rows, term.hist_erow(), term.hist_srow()) + 1,
        term.hist_rows()
    );

    term.append(&format!(
        "\n\nScreen has {} rows of {} columns.\n",
        term.disp_rows(),
        term.disp_cols()
    ));
    term.set_text_attrib(Attrib::Italic);
    term.append("Done!");
    term.set_text_attrib(Attrib::Normal);
}
source

pub fn text_utf8(&self) -> &[u8]

Return the UTF-8 text string for this character.

Examples found in repository?
examples/terminal.rs (line 509)
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
fn mb_test4_cb(_choice: &mut fltk::menu::Choice, term: &mut Terminal) {
    // Test the Utf8Char primitive
    let uc = Utf8Char::new(b'Q');
    let uc1 = uc.text_utf8();
    assert_eq!(&uc1, &[b'Q']);
    assert_eq!(&uc.attrib(), &Attrib::Normal);
    assert_eq!(
        &uc.charflags(),
        &(CharFlags::FG_XTERM | CharFlags::BG_XTERM)
    );
    assert_eq!(&uc.fgcolor(), &Color::XtermWhite);
    assert_eq!(&uc.bgcolor(), &Color::TransparentBg);

    let ring_rows = term.ring_rows();

    // println!();
    // dbg!(term.disp_srow(), term.disp_erow(), term.disp_rows(), term.ring_cols(), term.ring_srow(), term.ring_erow() );
    // dbg!(term.hist_srow(), term.hist_erow(), term.hist_rows(), ring_rows );
    // dbg!(term.offset(), term.hist_use_srow(), term.hist_use() );

    term.take_focus().unwrap();
    term.clear_history();
    assert_eq!(term.history_use(), 0);

    // Subtract row numbers, modulo `rows`
    fn row_diff(rows: i32, a: i32, b: i32) -> i32 {
        match a - b {
            n if n < 0 => n + rows,
            n => n
        }
    }
    // disp_srow is always 1 greater than hist_erow, modulo (ring_rows+1)
    assert_eq!(row_diff(ring_rows, term.disp_srow(), term.hist_erow()), 1);
    assert!(term.disp_srow() >= 0);
    assert!(term.disp_erow() >= 0);
    assert!(term.hist_srow() >= 0);
    assert!(term.hist_erow() >= 0);
    assert!(term.offset() >= 0);
    assert!(term.disp_srow() <= ring_rows);
    assert!(term.disp_erow() <= ring_rows);
    assert!(term.hist_srow() <= ring_rows);
    assert!(term.hist_erow() <= ring_rows);
    assert!(term.offset() <= ring_rows);

    assert_eq!(term.ring_srow(), 0);
    assert_eq!(term.ring_erow(), ring_rows - 1);
    assert_eq!(
        row_diff(ring_rows, term.disp_erow(), term.disp_srow()) + 1,
        term.disp_rows()
    );
    assert_eq!(
        row_diff(ring_rows, term.hist_erow(), term.hist_srow()) + 1,
        term.hist_rows()
    );

    assert_eq!(term.ring_erow(), term.ring_rows() - 1);
    assert_eq!(term.ring_srow(), 0);

    // Check the different cols methods, which should all return the same answer
    assert!(term.disp_cols() > 10);
    assert_eq!(term.disp_cols(), term.ring_cols());
    assert_eq!(term.disp_cols(), term.hist_cols());

    // Redundant protected vs public methods:
    assert_eq!(term.disp_cols(), term.display_columns());
    assert_eq!(term.disp_rows(), term.display_rows());

    /// Local function to read back all rows from the display into a long string.
    /// Does not include scrollback history.
    /// Trims trailing blanks on each line
    fn read_disp(term: &Terminal) -> String {
        let rows = term.display_rows();
        let mut text: Vec<u8> = Vec::with_capacity((rows * 64) as usize);
        for row in 0..rows {
            let r = term.u8c_disp_row(row).trim();
            // Iterate through a row, accumulating [u8]
            for c in r.iter() {
                // Note: Sometimes utf-8 length is > 1
                text.extend_from_slice(c.text_utf8());
            }
            text.extend_from_slice(b"\n");
        }
        // Return the result as a string
        std::str::from_utf8(&text).unwrap().to_string()
    }

    term.clear();
    term.append("Top line  ↑ (up-arrow)");
    term.set_text_attrib(Attrib::Underline);
    term.append("  ");
    term.set_text_attrib(Attrib::Normal);
    term.append("  \n");
    let mut text_out = read_disp(term);
    // Trim trailing empty lines
    text_out = text_out.trim_end_matches(&"\n\n").to_string();
    // The two plain blanks at the end will be trimmed, the two underlined blanks will be retained.
    assert_eq!(text_out, "Top line  ↑ (up-arrow)  \n");
    let r = term.u8c_disp_row(0);
    assert_eq!(r.col(0).text_utf8(), b"T");
    assert_eq!(r.col(10).text_utf8(), b"\xe2\x86\x91");     // UTF-8 up-arrow
    assert_eq!(r.col(24).text_utf8(), b" ");                // First blank after test text, NOT trimmed
    let r = term.u8c_disp_row(1);
    assert_eq!(r.col(0).text_utf8(), b" ");                 // Second row starts with blanks
    assert_eq!(r.col(1).text_utf8(), b" ");                 // Second row is full of blanks


    // Clear the screen again, then append test text, then read it back and compare
    let test_text =
"The wind was a torrent of darkness among the gusty trees.
The moon was a ghostly galleon tossed upon cloudy seas.
The road was a ribbon of moonlight over the purple moor,
And the highwayman came riding—
            Riding—riding—
The highwayman came riding, up to the old inn-door.";

term.clear_history();
    term.clear();
    let bg_save = term.text_bg_color();
    let fg_save = term.text_fg_color();
    term.set_text_bg_color(Color::DarkBlue);    // Set spooky colors
    term.set_text_fg_color(Color::from_rgb(0x40, 0x40, 0xff));
    term.append(test_text);
    term.set_text_bg_color(bg_save);
    term.set_text_fg_color(fg_save);

    let mut text_out = read_disp(term);
    // Trim trailing empty lines
    text_out = text_out.trim_end_matches(&"\n\n").to_string();
    assert_eq!(test_text, text_out);

    assert_eq!(row_diff(ring_rows, term.disp_srow(), term.hist_erow()), 1);

    assert_eq!(term.ring_srow(), 0);
    assert_eq!(term.ring_erow(), ring_rows - 1);
    assert_eq!(
        row_diff(ring_rows, term.disp_erow(), term.disp_srow()) + 1,
        term.disp_rows()
    );
    assert_eq!(
        row_diff(ring_rows, term.hist_erow(), term.hist_srow()) + 1,
        term.hist_rows()
    );

    term.append(&format!(
        "\n\nScreen has {} rows of {} columns.\n",
        term.disp_rows(),
        term.disp_cols()
    ));
    term.set_text_attrib(Attrib::Italic);
    term.append("Done!");
    term.set_text_attrib(Attrib::Normal);
}
source

pub fn size() -> usize

Return the size of a Utf8Char object in the underlying C++ code

Trait Implementations§

source§

impl<'a> Debug for Utf8Char

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.