Skip to content

Cursor hides when pressing Ctrl + C during an active progress bar. #279

@ankushT369

Description

@ankushT369

Description:
While a progress bar from the console crate is running, pressing Ctrl + C interrupts the program but leaves the terminal cursor hidden. The cursor is not restored automatically, so the terminal stays in a broken state until manually reset.
This is the sample code i am testing

use console::Term;
use std::thread;
use std::time::Duration;

fn main() -> std::io::Result<()> {
    let term = Term::stderr(); 
    term.hide_cursor()?;

    let mut last_lines = 0usize;
    let mut a = 0;
    let mut b = 0;

    loop {
        let bar1 = format!("Download A: [{}{}] {}%", "#".repeat(a), " ".repeat(20 - a), a * 5);
        let bar2 = format!("Download B: [{}{}] {}%", "#".repeat(b), " ".repeat(20 - b), b * 5);

        let frame = vec![bar1, bar2];

        if last_lines > 0 {
            term.move_cursor_up(last_lines)?;
        }

        for line in &frame {
            term.clear_line()?;
            term.write_line(line)?;
        }

        term.flush()?;
        last_lines = frame.len();

        if a < 20 { a += 1; }
        if b < 20 { b += 1; }

        if a == 20 && b == 20 {
            break;
        }

        thread::sleep(Duration::from_millis(200));
    }

    term.show_cursor()?;
    Ok(())
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions