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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//! `console_listener` includes the `Listener` struct, used to listen for a specified string to be typed to the console.

pub mod listener {
    use std::{
        any::Any,
        io,
        sync::{Arc, Mutex},
        thread,
    };

    /// The Listener struct holds a thread that listens for a specified string written to the console.
    /// 
    /// The Listener struct implements functions to check whether the string was typed, `heard_key`, and to wait for the string to get typed, `wait_for_key`.
    /// Create a new instance of Listener using `new` or `new_case_insensitive` and specify the string to listen for.
    pub struct Listener {
        heard_key: Arc<Mutex<bool>>,
        thread: thread::JoinHandle<bool>,
    }

    impl Listener {
        /// Creates a new Listener instance.
        ///
        /// The key is the string that the Listener listens for.
        ///
        /// # Panics
        ///
        /// The `new` function panics if the key is an empty string
        /// 
        /// # Examples
        /// 
        /// ```
        /// let listener = Listener::new("q");
        /// ```
        pub fn new(key: &str) -> Listener {
            assert!(!key.is_empty());

            Self::new_inner(key, false)
        }

        /// Creates a new case insensitive Listener instance.
        ///
        /// The key is the string that the Listener listens for.
        ///
        /// # Panics
        ///
        /// The `new_case_insensitive` function panics if the key is an empty string
        /// 
        /// # Examples
        /// 
        /// ```
        /// let listener = Listener::new_case_insensitive("q");
        /// ```
        pub fn new_case_insensitive(key: &str) -> Listener {
            assert!(!key.is_empty());

            Self::new_inner(key, true)
        }

        fn new_inner(key: &str, case_insensitive: bool) -> Listener {
            let heard_key = Arc::new(Mutex::new(false));
            let thread;
            {
                let key = String::from(key);
                let heard_key = Arc::clone(&heard_key);
                thread = thread::spawn(move || loop {
                    let mut input = String::with_capacity(key.len());
                    io::stdin()
                        .read_line(&mut input)
                        .expect("Error while reading input from console.");
                    
                    let input = input.trim_end();

                    if case_insensitive && input.eq_ignore_ascii_case(&key) || input == key {
                        let mut b = heard_key.lock().expect("Failed to lock heard_key.");
                        *b = true;

                        break true;
                    }
                });
            }

            Listener { heard_key, thread }
        }

        /// Returns true if the Listener has heard the key.
        /// 
        /// # Examples
        /// 
        /// ```
        /// let listener = Listener::new("q");
        /// 
        /// while !listener.heard_key() {
        ///     println!("Waiting for key.");
        /// }
        /// ```
        pub fn heard_key(&self) -> bool {
            match self.heard_key.lock() {
                Ok(b) => *b,
                Err(e) => *e.into_inner(),
            }
        }

        /// Blocks the current thread until the listening thread ends. This consumes the Listener.
        /// 
        /// Returns `Ok` containing a boolean indicating whether the key was heard.
        /// 
        /// # Errors
        /// 
        /// Returns `Err` if the listening thread panicked.
        /// 
        /// # Examples
        /// 
        /// ```
        /// let listener = Listener::new("q");
        /// 
        /// heard_key = listener.wait_for_key().unwrap();
        /// 
        /// assert!(heard_key);
        /// ```
        pub fn wait_for_key(self) -> Result<bool, Box<dyn Any + Send>> {
            self.thread.join()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{listener::*};
    use std::{thread, time::Duration};

    #[test]
    fn new_works() {
        let _listener = Listener::new("test");
    }

    #[test]
    #[should_panic]
    fn new_panics_on_empty_key() {
        let _listener = Listener::new("");
    }

    #[test]
    fn new_case_insensitive_works() {
        let _listener = Listener::new_case_insensitive("test");
    }

    #[test]
    #[should_panic]
    fn new_case_insensitive_panics_on_empty_key() {
        let _listener = Listener::new_case_insensitive("");
    }

    #[test]
    fn heard_key_returns_false() {
        let listener = Listener::new("test");

        thread::sleep(Duration::from_secs(5));

        assert!(!listener.heard_key());
    }
}