Skip to content

Commit

Permalink
Improve the reliability of write_all()
Browse files Browse the repository at this point in the history
  • Loading branch information
yizhepku committed Oct 21, 2024
1 parent c47fb1f commit d32c3b5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ unicode-segmentation = "1.9.0"
unicode-width = "0.1.9"

[dev-dependencies]
alacritty-test = { git = "https://github.com/YizhePKU/alacritty-test.git", rev = "d8d0196" }
alacritty-test = { git = "https://github.com/YizhePKU/alacritty-test.git", rev = "6c4df50" }
gethostname = "0.4.0"
pretty_assertions = "1.4.0"
rstest = { version = "0.18.0", default-features = false }
Expand Down
11 changes: 4 additions & 7 deletions examples/typing_latency.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
//! Measure the typing latency of Reedline with default configurations.
use alacritty_test::{pty_spawn, EventedReadWrite, Terminal};
use alacritty_test::{pty_spawn, PtyExt, Terminal};
use reedline::{DefaultPrompt, Reedline, Signal};
use std::{
io::Write,
time::{Duration, Instant},
};
use std::time::{Duration, Instant};

fn child() -> ! {
let mut line_editor = Reedline::create();
Expand Down Expand Up @@ -43,7 +40,7 @@ fn main() -> std::io::Result<()> {
let old_cursor = terminal.inner().grid().cursor.point;

// input a single keystroke
pty.writer().write_all(b"A").unwrap();
pty.write_all(b"A").unwrap();

let start_time = Instant::now();
loop {
Expand All @@ -63,7 +60,7 @@ fn main() -> std::io::Result<()> {
);

// delete the keystroke
pty.writer().write_all(b"\x7f\x7f\x7f\x7f").unwrap();
pty.write_all(b"\x7f\x7f\x7f\x7f").unwrap();
terminal.read_from_pty(&mut pty, Some(Duration::from_millis(50)))?;
}

Expand Down
52 changes: 26 additions & 26 deletions tests/basic.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use alacritty_test::{extract_text, pty_spawn, EventedReadWrite, Terminal};
use std::{io::Write, time::Duration};
use alacritty_test::{extract_text, pty_spawn, PtyExt, Terminal};
use std::time::Duration;

/// Test if Reedline prints the prompt at startup.
#[test]
Expand All @@ -24,7 +24,7 @@ fn echos_input() -> std::io::Result<()> {
let mut terminal = Terminal::new();
terminal.read_from_pty(&mut pty, Some(Duration::from_millis(50)))?;

pty.writer().write_all(b"Hello World!\r")?;
pty.write_all(b"Hello World!\r")?;
terminal.read_from_pty(&mut pty, Some(Duration::from_millis(50)))?;
let text = extract_text(terminal.inner());

Expand All @@ -41,9 +41,9 @@ fn backspace() -> std::io::Result<()> {
let mut terminal = Terminal::new();
terminal.read_from_pty(&mut pty, Some(Duration::from_millis(50)))?;

pty.writer().write_all(b"Hello World")?;
pty.writer().write_all(b"\x7f\x7f\x7f\x7f\x7f")?;
pty.writer().write_all(b"Bread!\r")?;
pty.write_all(b"Hello World")?;
pty.write_all(b"\x7f\x7f\x7f\x7f\x7f")?;
pty.write_all(b"Bread!\r")?;
terminal.read_from_pty(&mut pty, Some(Duration::from_millis(50)))?;

let text = extract_text(terminal.inner());
Expand All @@ -60,37 +60,37 @@ fn history() -> std::io::Result<()> {
let mut terminal = Terminal::new();
terminal.read_from_pty(&mut pty, Some(Duration::from_millis(50)))?;

pty.writer().write_all(b"Hello World!\r")?;
pty.write_all(b"Hello World!\r")?;
terminal.read_from_pty(&mut pty, Some(Duration::from_millis(50)))?;
pty.writer().write_all(b"Goodbye!\r")?;
pty.write_all(b"Goodbye!\r")?;
terminal.read_from_pty(&mut pty, Some(Duration::from_millis(50)))?;

// arrow up
pty.writer().write_all(b"\x1b[A")?;
pty.write_all(b"\x1b[A")?;
terminal.read_from_pty(&mut pty, Some(Duration::from_millis(50)))?;
let text = extract_text(terminal.inner());
assert_eq!(&text[4][13..21], "Goodbye!");

// press Enter to execute it
pty.writer().write_all(b"\r")?;
pty.write_all(b"\r")?;
terminal.read_from_pty(&mut pty, Some(Duration::from_millis(50)))?;
let text = extract_text(terminal.inner());
assert_eq!(&text[5][..22], "We processed: Goodbye!");

// arrow up twice
pty.writer().write_all(b"\x1b[A\x1b[A")?;
pty.write_all(b"\x1b[A\x1b[A")?;
terminal.read_from_pty(&mut pty, Some(Duration::from_millis(50)))?;
let text = extract_text(terminal.inner());
assert_eq!(&text[6][13..25], "Hello World!");

// arrow down twice
pty.writer().write_all(b"\x1b[B\x1b[B")?;
pty.write_all(b"\x1b[B\x1b[B")?;
terminal.read_from_pty(&mut pty, Some(Duration::from_millis(50)))?;
let text = extract_text(terminal.inner());
assert_eq!(&text[6][13..25], " ");

// type "Hel" then arrow up

Check warning on line 92 in tests/basic.rs

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"Hel" should be "Help" or "Hell" or "Heal".
pty.writer().write_all(b"Hel\x1b[A")?;
pty.write_all(b"Hel\x1b[A")?;

Check warning on line 93 in tests/basic.rs

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"Hel" should be "Help" or "Hell" or "Heal".
terminal.read_from_pty(&mut pty, Some(Duration::from_millis(50)))?;
let text = extract_text(terminal.inner());
assert_eq!(&text[6][13..25], "Hello World!");
Expand All @@ -107,16 +107,16 @@ fn word_movement() -> std::io::Result<()> {
let mut terminal = Terminal::new();
terminal.read_from_pty(&mut pty, Some(Duration::from_millis(50)))?;

pty.writer().write_all(b"foo bar baz")?;
pty.write_all(b"foo bar baz")?;

// Ctrl-left twice, Ctrl-right once, Ctrl-b twice, Ctrl-f once.
pty.writer().write_all(b"\x1b[1;5D\x1b[1;5D")?;
pty.writer().write_all(b"\x1b[1;5C")?;
pty.writer().write_all(b"\x02\x02")?;
pty.writer().write_all(b"\x06")?;
pty.write_all(b"\x1b[1;5D\x1b[1;5D")?;
pty.write_all(b"\x1b[1;5C")?;
pty.write_all(b"\x02\x02")?;
pty.write_all(b"\x06")?;

// Insert some more text, then press enter.
pty.writer().write_all(b"za\r")?;
pty.write_all(b"za\r")?;
terminal.read_from_pty(&mut pty, Some(Duration::from_millis(50)))?;

let text = extract_text(terminal.inner());
Expand All @@ -133,10 +133,10 @@ fn clear_screen() -> std::io::Result<()> {
let mut terminal = Terminal::new();
terminal.read_from_pty(&mut pty, Some(Duration::from_millis(50)))?;

pty.writer().write_all(b"Hello World!\r")?;
pty.write_all(b"Hello World!\r")?;
terminal.read_from_pty(&mut pty, Some(Duration::from_millis(50)))?;

pty.writer().write_all(b"Hello again!\x0c\r")?;
pty.write_all(b"Hello again!\x0c\r")?;
terminal.read_from_pty(&mut pty, Some(Duration::from_millis(50)))?;

let text = extract_text(terminal.inner());
Expand All @@ -152,28 +152,28 @@ fn emacs_keybinds() -> std::io::Result<()> {
let mut terminal = Terminal::new();
terminal.read_from_pty(&mut pty, Some(Duration::from_millis(50)))?;

pty.writer().write_all(b"Hello World!")?;
pty.write_all(b"Hello World!")?;

// undo with Ctrl-z
pty.writer().write_all(b"\x1a")?;
pty.write_all(b"\x1a")?;
terminal.read_from_pty(&mut pty, Some(Duration::from_millis(50)))?;
let text = extract_text(terminal.inner());
assert_eq!(&text[0][13..25], "Hello ");

// redo with Ctrl-g
pty.writer().write_all(b"\x07")?;
pty.write_all(b"\x07")?;
terminal.read_from_pty(&mut pty, Some(Duration::from_millis(50)))?;
let text = extract_text(terminal.inner());
assert_eq!(&text[0][13..25], "Hello World!");

// delete "World" with alt+left, alt+backspace
pty.writer().write_all(b"\x1b[1;3D\x1b\x7f")?;
pty.write_all(b"\x1b[1;3D\x1b\x7f")?;
terminal.read_from_pty(&mut pty, Some(Duration::from_millis(50)))?;
let text = extract_text(terminal.inner());
assert_eq!(&text[0][13..25], "Hello ! ");

// make "Hello" ALL CAPS with alt+b, alt+u
pty.writer().write_all(b"\x1bb\x1bu")?;
pty.write_all(b"\x1bb\x1bu")?;
terminal.read_from_pty(&mut pty, Some(Duration::from_millis(50)))?;
let text = extract_text(terminal.inner());
assert_eq!(&text[0][13..25], "HELLO ! ");
Expand Down

0 comments on commit d32c3b5

Please sign in to comment.