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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
//! Module containing various utility functions. use std::io::{BufRead, Write, Result as IoResult, Error, ErrorKind}; use std::iter; /// The User-Agent to use when authenticating to the GitHub API. pub static GITHUB_USER_AGENT: &'static str = concat!("dishub/", env!("CARGO_PKG_VERSION")); /// Uppercase the first character of the supplied string. /// /// Based on http://stackoverflow.com/a/38406885/2851815 /// /// # Examples /// /// ``` /// # use dishub::util::uppercase_first; /// assert_eq!(uppercase_first("abolish"), "Abolish".to_string()); /// ``` pub fn uppercase_first(s: &str) -> String { let mut c = s.chars(); match c.next() { None => String::new(), Some(f) => f.to_uppercase().collect::<String>() + c.as_str(), } } /// Create a string consisting of `n` repetitions of `what`. /// /// # Examples /// /// ``` /// # use dishub::util::mul_str; /// assert_eq!(mul_str("Го! ", 3), "Го! Го! Го! ".to_string()); /// ``` pub fn mul_str(what: &str, n: usize) -> String { iter::repeat(what).take(n).collect() } /// Ask the user to input a string of the exact length of `desired_len`, (re)prompting as necessary. /// /// # Examples /// /// Allow anything 10 charactes long: /// /// ``` /// # use std::io::Cursor; /// # use dishub::util::prompt_exact_len; /// assert_eq!(prompt_exact_len(&mut Cursor::new(b"0123456789"), /// &mut Vec::new(), /// "Allowed chars", /// |_| true, /// 10).unwrap(), /// "0123456789".to_string()); /// ``` /// /// Allow a 10-character-long `u64`: /// /// ``` /// # use std::io::Cursor; /// # use std::str::FromStr; /// # use dishub::util::prompt_exact_len; /// assert_eq!(prompt_exact_len(&mut Cursor::new(b"1234567890"), /// &mut Vec::new(), /// "Long number", /// |s| u64::from_str(s).is_ok(), /// 10).unwrap(), /// "1234567890".to_string()); /// assert!(prompt_exact_len(&mut Cursor::new(b"1234abcdef"), /// &mut Vec::new(), /// "Long number", /// |s| u64::from_str(s).is_ok(), /// 10).is_err()); /// ``` pub fn prompt_exact_len<R, W, F>(input: &mut R, output: &mut W, prompt_s: &str, verifier: F, desired_len: usize) -> IoResult<String> where R: BufRead, W: Write, F: Fn(&String) -> bool { let mut out = String::new(); while out.len() != desired_len { try!(prompt(input, output, prompt_s, &verifier, false, true, &mut out)); } Ok(out) } /// Ask the user to input a string of non-zero length, (re)prompting as necessary. /// /// # Examples /// /// Allow anything as long as it's *some*thing: /// /// ``` /// # use std::io::Cursor; /// # use dishub::util::prompt_nonzero_len; /// assert_eq!(prompt_nonzero_len(&mut Cursor::new(b"123456789"), /// &mut Vec::new(), /// "Allowed chars", /// |_| true).unwrap(), /// "123456789".to_string()); /// ``` /// /// Allow valid `u64`s: /// /// ``` /// # use std::io::Cursor; /// # use std::str::FromStr; /// # use dishub::util::prompt_nonzero_len; /// assert_eq!(prompt_nonzero_len(&mut Cursor::new(b"123456789"), /// &mut Vec::new(), /// "Number", /// |s| u64::from_str(s).is_ok()).unwrap(), /// "123456789".to_string()); /// assert!(prompt_nonzero_len(&mut Cursor::new(b"123abcdef"), /// &mut Vec::new(), /// "Number", /// |s| u64::from_str(s).is_ok()).is_err()); /// ``` pub fn prompt_nonzero_len<R, W, F>(input: &mut R, output: &mut W, prompt_s: &str, verifier: F) -> IoResult<String> where R: BufRead, W: Write, F: Fn(&String) -> bool { let mut out = String::new(); while out.is_empty() { try!(prompt(input, output, prompt_s, &verifier, false, true, &mut out)); } Ok(out) } /// Ask the user to input a string of any length after printing a prompt prompting. /// /// Will return `None` if the string is empty. /// /// # Examples /// /// Allow anything: /// /// ``` /// # use std::io::Cursor; /// # use dishub::util::prompt_any_len; /// assert_eq!(prompt_any_len(&mut Cursor::new(b"123456789"), /// &mut Vec::new(), /// "Allowed chars", /// |_| true).unwrap().unwrap(), /// "123456789".to_string()); /// assert_eq!(prompt_any_len(&mut Cursor::new(b""), /// &mut Vec::new(), /// "Allowed chars", /// |_| true).unwrap(), /// None); /// ``` /// /// Allow valid `u64`s: /// /// ``` /// # use std::io::Cursor; /// # use std::str::FromStr; /// # use dishub::util::prompt_any_len; /// assert_eq!(prompt_any_len(&mut Cursor::new(b"123456789"), /// &mut Vec::new(), /// "Number", /// |s| u64::from_str(s).is_ok()).unwrap().unwrap(), /// "123456789".to_string()); /// assert_eq!(prompt_any_len(&mut Cursor::new(b"123abcdef"), /// &mut Vec::new(), /// "Number", /// |s| u64::from_str(s).is_ok()).map_err(|_| ()), /// Ok(None)); /// ``` pub fn prompt_any_len<R, W, F>(input: &mut R, output: &mut W, prompt_s: &str, verifier: F) -> IoResult<Option<String>> where R: BufRead, W: Write, F: Fn(&String) -> bool { let mut out = String::new(); try!(prompt(input, output, prompt_s, &verifier, true, true, &mut out)); if out.is_empty() { Ok(None) } else { Ok(Some(out)) } } /// Ask the user to input a multiline string, (re)prompting as necessary. /// /// Each line is separated by a `\`, but can be escaped by `\\`, e.g. /// /// ```plaintext /// Prompt: Abolish\ /// the\ /// burgeoisie! /// ``` /// /// Will yield `"Abolish\nthe\nburgeoisie!"`, but /// /// ```plaintext /// Prompt: Capitalism\\ /// ``` /// /// Will yield `r"Capitalism\"`. /// /// # Examples /// /// Reading multiple lines: /// /// ``` /// # use std::io::Cursor; /// # use dishub::util::prompt_multiline; /// assert_eq!(prompt_multiline(&mut Cursor::new(b"Line 1\\\nLine 2\\\nLine 3"), /// &mut Vec::new(), /// "Lines", /// |_| true).unwrap(), /// "Line 1\nLine 2\nLine 3".to_string()); /// ``` /// /// Escaping newline: /// /// ``` /// # use std::io::Cursor; /// # use dishub::util::prompt_multiline; /// assert_eq!(prompt_multiline(&mut Cursor::new(b"Line 0\\\\\n"), /// &mut Vec::new(), /// "Escaped line", /// |_| true).unwrap(), /// "Line 0\\".to_string()); /// ``` /// /// Accepting only two-line strings: /// /// ``` /// # use std::io::Cursor; /// # use dishub::util::prompt_multiline; /// assert_eq!(prompt_multiline(&mut Cursor::new(b"Line 1\\\nLine 2\n"), /// &mut Vec::new(), /// "2 lines", /// |s| s.lines().count() == 2).unwrap(), /// "Line 1\nLine 2".to_string()); /// ``` pub fn prompt_multiline<R, W, F>(input: &mut R, output: &mut W, prompt_s: &str, verifier: F) -> IoResult<String> where R: BufRead, W: Write, F: Fn(&String) -> bool { let reprompt = mul_str(" ", prompt_s.len() + 2); let mut lbuf = String::new(); let mut buf = String::new(); while buf.is_empty() { buf = try!(prompt_nonzero_len(input, output, prompt_s, |_| true)); while buf.ends_with('\\') && !buf.ends_with(r"\\") { buf.pop(); buf.push('\n'); try!(prompt(input, output, &reprompt, &|_| true, false, false, &mut lbuf)); buf.push_str(&lbuf); } if buf.ends_with(r"\\") { buf.pop(); } if !verifier(&buf) { buf.clear(); } } Ok(buf) } fn prompt<R, W, F>(input: &mut R, output: &mut W, prompt_s: &str, verifier: &F, allow_empty: bool, colon: bool, out: &mut String) -> IoResult<()> where R: BufRead, W: Write, F: Fn(&String) -> bool { if colon { try!(write!(output, "{}: ", prompt_s)); } else { try!(write!(output, "{}", prompt_s)); } try!(output.flush()); out.clear(); if try!(input.read_line(out)) == 0 && !allow_empty { return Err(Error::new(ErrorKind::UnexpectedEof, "Input too short")); } *out = out.trim().to_string(); if !verifier(out) { out.clear(); } Ok(()) }