AntonGepting / tmux-interface-rs

Rust language library for communication with TMUX via CLI
MIT License
52 stars 6 forks source link

tmux output displayed #14

Closed Ganneff closed 1 year ago

Ganneff commented 1 year ago

Hi

at least HasSession has tmux spit out an error to the user - should be suppressed IMO.

Example:

        Tmux::with_command(HasSession::new().target_session(&sesname))
            .status()
            .unwrap()
            .success()

If sesname contains a non-existing session, I do get the correct false back, but also can't find session: nonexisting on console, which is what tmux replies. That line shouldn't appear, IMO.

Greetings Ganneff

AntonGepting commented 1 year ago

Hi,

thank you for reporting this issue.

It looks like the reason for this is that the stderr handle is inherited from the parent process by default in case of using .status() method.

As a fast temporary solution I can suggest you:

  1. manually use method .into_command() of Tmux structure to convert tmux_interface::Tmux into std::process::Command
  2. manually set stderr handle as null with .stderr() method of std::process::Command

Example:

#[test]
fn issue14() {
  use std::process::Stdio;
  use tmux_interface::{HasSession, Tmux};

  // error reproduction
  let output = Tmux::with_command(HasSession::new().target_session("my_session"))
    .status()
    .unwrap()
    .success();

  dbg!(output);

  // temporary solution
  let output = Tmux::with_command(HasSession::new().target_session("my_session"))
    .into_command()
    .stderr(Stdio::null())
    .status()
    .unwrap()
    .success();

  dbg!(output);
}

For future releases most likely some of the following variants will be analyzed and implemented:

  1. using null for stderr by default
  2. using wrapper for .stderr(), .stdout(), .stdin() methods for commands in tmux_interface library to let the user control the behavior
  3. both
  4. or maybe some other mechanics
Ganneff commented 1 year ago

Thanks, Option 2 is most flexible, but 1 is entirely OK too. Actually, an idea for 4: Collect stderr, and only print it when status != success or when user asks for it.

AntonGepting commented 1 year ago

I have analyzed advantages and disadvantages of options from my and your previous posts, and decided to implement following:

so simple example is now looking like:

#[test]
fn issue14() {
    use tmux_interface::{HasSession, StdIO, Tmux};

    let output = Tmux::with_command(HasSession::new().target_session("my_session"))
        .stderr(Some(StdIO::Null))
        .status()
        .unwrap()
        .success();

    dbg!(output);
}

If you are comfortable with proposed changes, this feature will be released in 0.3.1

Ganneff commented 1 year ago

This sounds good, yes.

AntonGepting commented 1 year ago

thx,

merged in main, released v0.3.1, closed as solved.

Please feel free to reopen or create a new issue if you have other problems or suggestions.