1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
//TODO

//! Tiny crates that stops the console window form closing when the program finishes.

/// Using the `enter_to_continue` module makes is the simplest way of using this crate, however, the only key you can use with it is the enter key.
pub mod enter_to_continue {
    use std::io;
    /// ### Message then close with enter.
    /// Prompts user with message `"Press enter to close."`, waits for the user to press enter then ends to program (closing the window).
    /// Add
    /// ```no_run
    /// extern crate dont_disappear;
    /// ```
    /// to the top of your file
    /// and

    /// ```no_run
    /// dont_disappear::enter_to_continue::default();
    /// ```
    /// to where your program ends
    pub fn default() {
        custom_msg("Press enter to close.");
    }
    /// ### Custom message then close with enter.
    /// Prompts user with a custom message, waits for the user to press enter then ends to program (closing the window).
    /// Add
    /// ```no_run
    /// extern crate dont_disappear;
    /// ```
    /// to the top of your file
    /// and

    /// ```no_run
    /// dont_disappear::enter_to_continue::custom_msg("Your custom message.");
    /// ```
    /// to where your program ends
    pub fn custom_msg(msg: &str) {
        let mut input = String::new();
        println!("{}", msg);
        io::stdin().read_line(&mut input).unwrap();
    }
}
/// The `any_key_to_continue` module responds to any key press, however, can return strange characters when Ctrl-c or Delete keys are used.
pub mod any_key_to_continue {
    extern crate crossterm;
    use self::crossterm::input;
    use self::crossterm::Screen;
    /// ### Message then close with any key.
    /// Prompts user with message `"Press any key to continue"`, waits for the user to press a key then ends to program (closing the window).
    /// Add
    /// ```rust
    /// extern crate dont_disappear;
    /// ```
    /// to the top of your file
    /// and

    /// ```no_run
    /// dont_disappear::any_key_to_continue::default();
    /// ```
    /// to where your program ends
    pub fn default() {
        custom_msg("Press any key to continue")
    }
    /// ### Custom message then close with any key.
    /// Prompts user with a custom message, waits for the user to press a key then ends to program (closing the window).
    /// Add
    /// ```rust
    /// extern crate dont_disappear;
    /// ```
    /// to the top of your file
    /// and

    /// ```no_run
    /// dont_disappear::any_key_to_continue::custom_msg("Your custom message.");
    /// ```
    /// to where your program ends
    pub fn custom_msg(msg: &str) {
        println!("{}", msg);
        let screen = Screen::default();
        let input = input(&screen);

        match input.read_char() {
            Ok(_s) => (),
            Err(_e) => (),
        }
    }
}

/// ### Press close only with window manager or Ctrl-c.
/// The program just stops and waits to be killed by pressing close only with window manager or Ctrl-c.
/// The thread is [parked](https://doc.rust-lang.org/nightly/std/thread/fn.park.html) so it does not use CPU.
/// Add
/// ```no_run
/// extern crate dont_disappear;
/// ```
/// to the top of your file
/// and
/// ```no_run
/// dont_disappear::press_close();
/// ```
/// to where your program ends
// possible
pub fn press_close() {
    loop {
        std::thread::park();
    }
}