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
108
//! Tiny crates that stops the console window form closing when the program finishes.
use std::io;
use std::io::Write;

// Flush only when possible.
fn try_flush() {
    io::stdout().flush().unwrap_or(())
}

/// 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 crate::try_flush;
    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 exit.");
    }
    /// ### 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) {
        print!("{}", msg);
        try_flush();
        io::stdin().read_line(&mut String::new()).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 {
    use crate::try_flush;
    use crossterm::input;
    /// ### 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
    #[allow(unused_must_use)]
    pub fn custom_msg(msg: &str) {
        print!("{}", msg);
        try_flush();
        input().read_char();
    }
}

/// ### 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();
    }
}