rust-cli / rust-cli/rexpect

Resuming read after timeout

Open
#125 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
391
Forks
69
Avg merge
1d 34m
Merged PRs (30d)
2

Description

I have a

pub session: Option<Arc<Mutex<PtySession>>>,

I want to get the existing output of a process after 100ms. This seemed to work at first:

        let output = {
            let session_mutex = self.session.as_mut().ok_or(anyhow!("No session"))?;
            let mut session = session_mutex.lock()?;
            match session.exp_eof() {
                Ok(output) => output,
                Err(e) => {
                    match e {
                        rexpect::error::Error::Timeout { got, .. } => got
                            .replace("`\\n`\n", "\n")
                            .replace("`\\r`", "\r")
                            .replace("`^`", "\u{1b}"),
                        _ => return Err(e.into()),
                    }
                }
            }
        };

but then I realized that session.exp_eof does not drain the buffer if a timeout is reached, meaning that if I try to read the next chunk, the entire output up to this point is going to be returned yet again. This seems to work:

    fn drain_read_buffer(&mut self) -> ZammResult<String> {
        let mut output = String::new();
        if let Some(session) = self.session.as_mut() {
            let reader = &mut session.lock()?.reader;
            while let Some(chunk) = reader.try_read() {
                output.push(chunk);
            }
        }
        Ok(output)
    }

    ...

        let mut output = String::new();
        loop {
            sleep(Duration::from_millis(100));
            let new_output = self.drain_read_buffer()?;
            if new_output.is_empty() {
                break;
            }
            output.push_str(&new_output);
        }

But I'm wondering, is there a better way to implement "read whatever is available after 100ms" than this?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by examining rexpect::exp_eof and the reader.try_read path used by drain_read_buffer, focusing on what remains buffered after a timeout. Define what "read whatever is available after 100ms" should return and how subsequent reads should avoid returning earlier output again; validate the behavior with a PTY process that produces output incrementally.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
cli, operating-systems
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.