Put sleeping 500ms into pty_with_nushell()

This commit is contained in:
YizhePKU 2024-07-08 00:41:35 +08:00
parent ca781ef29d
commit cafbace836
2 changed files with 15 additions and 15 deletions

View File

@ -15,11 +15,11 @@
//! `--no-config-file`, otherwise Nushell will ask if you want to create one //! `--no-config-file`, otherwise Nushell will ask if you want to create one
//! with default, and that messes up the input. //! with default, and that messes up the input.
//! //!
//! Step 3: Wait for Nushell to initialize (sleeping for 500ms should do). On //! Step 3: Wait for Nushell to initialize (sleeping for 500ms should do).
//! Linux, trying to write to the PTY before Nushell finishes initialization //! `pty_with_nushell()` also does that for you. On Linux, trying to write to
//! appears to succeed, but the data will be lost. I'm not sure if this is a bug //! the PTY before Nushell finishes initialization appears to succeed, but the
//! or just weird behavior of Linux PTY. It's not necessary on Windows, but it //! data will be lost. I'm not sure if this is a bug or just weird behavior of
//! won't hurt either. //! Linux PTY. It's not necessary on Windows, but it won't hurt either.
//! //!
//! Step 4: Write data to the PTY. Any data you sent will appear to Nushell as //! Step 4: Write data to the PTY. Any data you sent will appear to Nushell as
//! if they were typed in a terminal. ANSI escape codes are used for special //! if they were typed in a terminal. ANSI escape codes are used for special
@ -73,8 +73,9 @@ pub fn default_terminal() -> (Term<EventProxy>, mpsc::Receiver<Event>) {
(Term::new(config, &size, EventProxy(tx)), rx) (Term::new(config, &size, EventProxy(tx)), rx)
} }
/// Creates a PTY and connect the slave end to a Nushell process. If `pwd` is /// Creates a PTY and connect the slave end to a Nushell process, then wait for
/// None, the Nushell process will inherit PWD from the current process. /// Nushell to initialize. If `pwd` is None, the Nushell process will inherit
/// PWD from the current process.
pub fn pty_with_nushell(args: Vec<&str>, pwd: Option<PathBuf>) -> Pty { pub fn pty_with_nushell(args: Vec<&str>, pwd: Option<PathBuf>) -> Pty {
let executable = crate::fs::executable_path().to_string_lossy().to_string(); let executable = crate::fs::executable_path().to_string_lossy().to_string();
let options = Options { let options = Options {
@ -92,7 +93,12 @@ pub fn pty_with_nushell(args: Vec<&str>, pwd: Option<PathBuf>) -> Pty {
cell_width: 0, cell_width: 0,
cell_height: 0, cell_height: 0,
}; };
tty::new(&options, window_size, 0).expect("creating a PTY succeeds") let pty = tty::new(&options, window_size, 0).expect("creating a PTY should succeed");
// Wait for Nushell to initialize.
std::thread::sleep(Duration::from_millis(500));
pty
} }
/// Reads from `pty` until no more data is available. Will periodically call /// Reads from `pty` until no more data is available. Will periodically call

View File

@ -3,7 +3,7 @@ use nu_test_support::terminal::{
default_terminal, extract_cursor, extract_text, pty_with_nushell, pty_write_handler, default_terminal, extract_cursor, extract_text, pty_with_nushell, pty_write_handler,
read_to_end, read_to_end,
}; };
use std::{io::Write, time::Duration}; use std::io::Write;
#[test] #[test]
fn auto_cd_works() { fn auto_cd_works() {
@ -15,9 +15,6 @@ fn auto_cd_works() {
let mut pty = pty_with_nushell(vec!["--no-config-file"], Some(cwd.path().to_path_buf())); let mut pty = pty_with_nushell(vec!["--no-config-file"], Some(cwd.path().to_path_buf()));
let (mut term, mut events) = default_terminal(); let (mut term, mut events) = default_terminal();
// Wait for Nushell to initialize.
std::thread::sleep(Duration::from_millis(500));
#[cfg(windows)] #[cfg(windows)]
pty.writer().write_all(b".\\foo\r").unwrap(); pty.writer().write_all(b".\\foo\r").unwrap();
#[cfg(unix)] #[cfg(unix)]
@ -62,9 +59,6 @@ fn command_hints_are_pwd_aware() {
); );
let (mut term, mut events) = default_terminal(); let (mut term, mut events) = default_terminal();
// Wait for Nushell to initialize.
std::thread::sleep(Duration::from_millis(500));
pty.writer().write_all(b"cd foo\r").unwrap(); pty.writer().write_all(b"cd foo\r").unwrap();
pty.writer().write_all(b"print 'FOO'\r").unwrap(); pty.writer().write_all(b"print 'FOO'\r").unwrap();
pty.writer().write_all(b"cd ../bar\r").unwrap(); pty.writer().write_all(b"cd ../bar\r").unwrap();