Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read_char() breaks println! in other threads #136

Closed
bugeats opened this issue Sep 26, 2022 · 5 comments · Fixed by #165
Closed

read_char() breaks println! in other threads #136

bugeats opened this issue Sep 26, 2022 · 5 comments · Fixed by #165

Comments

@bugeats
Copy link

bugeats commented Sep 26, 2022

use console::Term;

fn main() {
    let thread1 = std::thread::spawn(|| loop {
        println!("line");
        std::thread::sleep(std::time::Duration::from_millis(250));
    });

    let stdout = Term::buffered_stdout();
    stdout.read_char().unwrap();

    thread1.join().unwrap();
}

OUTPUT:

line
    line
        line
            line

EXPECTED:

line
line
line
line

I don't know if I'm doing something wrong, but listening for chars on one thread is breaking (carriage returns?) output on other threads.

@bugeats bugeats changed the title read_char() breaks println! read_char() breaks println! in other threads Sep 26, 2022
@mitsuhiko
Copy link
Collaborator

I believe that this cannot be fixed because read_char moves the terminal into raw mode.

@redsuperbat
Copy link

I have the same issue.
Is there any workaround?

@goodartistscopy
Copy link
Contributor

goodartistscopy commented May 7, 2023

Indeed the terminal is set to raw mode in read_single_key(). But this may be overkill regarding what the function actually requires. In particular cfmakeraw() also disables all output processing from the terminal.

This single line fixes the behavior reported here:

    unsafe { libc::cfmakeraw(&mut termios) };
    termios.c_oflag = original.c_oflag;

As it only changes the output attributes, I think it won't interfere with the rest of the function, but I did not test it extensively. Also it may be more elegant to actually determine the precise termios flag set required, but I'm not knowledgeable enough on terminal control attributes to really figure that out.

@talwat
Copy link

talwat commented May 8, 2023

I have the same issue. Is there any workaround?

Sort of, I have had success by inserting a carriage return before every single printed newline. So basically instead of /n use /r/n.

@JasonWei512
Copy link

I have the same issue.
Is there any workaround?

I wrote a console wrapper to solve all the raw mode issues:
https://github.com/JasonWei512/code-radio-cli/blob/cc68773b3123fb580c21dbf92ff1a1565607a7a6/src/terminal.rs

JasonWei512 added a commit to JasonWei512/code-radio-cli that referenced this issue May 22, 2023
In console 0.15.7, read_char() won't break println!() in other threads:
- console-rs/console#165
- console-rs/console#136
"terminal::writeline!()" was a workaround for this problem.
So it's no longer needed.
JasonWei512 added a commit to JasonWei512/code-radio-cli that referenced this issue Jun 5, 2023
* Remove "terminal::writeline!()" macro

In console 0.15.7, read_char() won't break println!() in other threads:
- console-rs/console#165
- console-rs/console#136
"terminal::writeline!()" was a workaround for this problem.
So it's no longer needed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants