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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
#[cfg(test)] mod tests { #[test] fn it_works() { assert_eq!(2 + 2, 4); } } /// # Examples /// ``` /// use term_basics_linux::tbl; /// for sty in tbl::TextStyle::iterator(){ /// for bg in tbl::UserColour::iterator(){ /// for fg in tbl::UserColour::iterator(){ /// tbl::println_cols_style("cool and good", fg, bg, sty); /// } /// } /// } /// ``` /// ``` /// let name = tbl::prompt("type your name: "); /// tbl::print("Your name: "); /// tbl::println(name); /// ``` pub mod tbl{ use std::io; use std::io::Write; //flush stdout use std::io::Read; use termios::{Termios, TCSANOW, ECHO, ICANON, tcsetattr}; use std::cmp::{ max, min }; use num_derive::ToPrimitive; use num_traits::ToPrimitive; use std::slice::Iter; /// Colours available. The user has defined the exact values of /// these colours for there TTY or emulator. #[derive(PartialEq,Eq,Clone,ToPrimitive)] pub enum UserColour { Std = 99, Black = 0, Red = 1, Green = 2, Yellow = 3, Blue = 4, Magenta = 5, Cyan = 6, Grey = 7, } /// Iterate over all colours in the enum /// /// # Example /// /// ``` /// use term_basics_linux::tbl; /// for col in tbl::UserColour::iterator(){ /// //use col /// } /// ``` impl UserColour { pub fn iterator() -> Iter<'static, Self> { static ARR: [UserColour; 9] = [ UserColour::Std, UserColour::Black, UserColour::Red, UserColour::Green, UserColour::Yellow, UserColour::Blue, UserColour::Magenta, UserColour::Cyan, UserColour::Grey]; ARR.into_iter() } } /// All styles that do not alter fg or bg colours. #[derive(PartialEq,Eq,Clone,ToPrimitive)] pub enum TextStyle { Std = 0, Bold = 1, Faint = 2, Italic = 3, Underlined = 4, Blink = 5, Hidden = 8, Crossed = 9, } /// Iterate over all styles. /// /// # Example /// /// ``` /// use term_basics_linux::tbl; /// for sty in tbl::TextStyle::iterator(){ /// //use sty /// } /// ``` impl TextStyle { pub fn iterator() -> Iter<'static, Self> { static ARR: [TextStyle; 8] = [ TextStyle::Std, TextStyle::Bold, TextStyle::Faint, TextStyle::Italic, TextStyle::Underlined, TextStyle::Blink, TextStyle::Hidden, TextStyle::Crossed]; ARR.into_iter() } } /// To specify if you set the foreground or background colour. #[derive(PartialEq,Eq)] pub enum FGBG { FG, BG } /// Sets the colour of the text printed after this call. /// It will print linux colour escape characters to std out. /// /// # Examples /// /// ``` /// use term_basics_linux::tbl; /// for i in tbl::UserColour::iterator(){ /// tbl::set_colour(i.clone(), tbl::FGBG::FG); /// println!("haha yes"); /// } /// ``` /// ``` /// use term_basics_linux::tbl; /// for i in tbl::UserColour::iterator(){ /// tbl::set_colour(i.clone(), tbl::FGBG::BG); /// println!("haha yes"); /// } /// ``` pub fn set_colour(col: UserColour, fgbg: FGBG){ if col == UserColour::Std{ print!("\x1B[00m"); } let _id = ToPrimitive::to_u8(&col); let mut id = 0; if _id.is_some() { id = _id.unwrap(); } let mut colorcode = String::from("\x1B["); if fgbg == FGBG::FG { colorcode.push('3'); }else{ colorcode.push('4'); } colorcode.push_str(&format!("{}", id)); colorcode.push_str("m"); print!("{}", colorcode); } /// Sets both foreground and background colours /// It will print linux colour escape characters to std out. /// /// # Example /// /// ``` /// use term_basics_linux::tbl; /// for fg in tbl::UserColour::iterator(){ /// for bg in tbl::UserColour::iterator(){ /// tbl::set_colours(fg.clone(), bg.clone()); /// println!("haha yes"); /// } /// } /// ``` pub fn set_colours(fg: UserColour, bg: UserColour){ set_colour(fg, FGBG::FG); set_colour(bg, FGBG::BG); } /// Sets the style of the text printed after this call. /// It will print linux colour escape characters to std out. /// /// # Example /// /// ``` /// use term_basics_linux::tbl; /// for i in tbl::TextStyle::iterator(){ /// tbl::set_style(i.clone()); /// println!("haha yes"); /// } /// ``` pub fn set_style(sty: TextStyle){ let _id = ToPrimitive::to_u8(&sty); let mut id = 0; if _id.is_some() { id = _id.unwrap(); } let mut colorcode = String::from("\x1B[0"); colorcode.push_str(&format!("{}", id)); colorcode.push_str("m"); print!("{}", colorcode); } /// Print to stdout, it is just print!("{}", msg); /// Here to stay consistent /// /// # Example /// /// ``` /// use term_basics_linux::tbl; /// tbl::print("cool and good"); /// ``` pub fn print<T: std::fmt::Display>(msg: T){ print!("{}", msg); } /// Print to stdout, it is just println!("{}", msg); /// Here to stay consistent /// /// # Example /// /// ``` /// use term_basics_linux::tbl; /// tbl::println("cool and good"); /// ``` pub fn println<T: std::fmt::Display>(msg: T){ println!("{}", msg); } /// Print to stdout with a text colour. /// /// # Example /// /// ``` /// use term_basics_linux::tbl; /// tbl::print_col("orang no!", tbl::UserColour::Yellow); /// ``` pub fn print_col<T: std::fmt::Display>(msg: T, col: UserColour){ set_colour(col, FGBG::FG); print!("{}", msg); } /// Print to stdout with a text colour. /// /// # Example /// /// ``` /// use term_basics_linux::tbl; /// tbl::println_col("orang no!", tbl::UserColour::Yellow); /// ``` pub fn println_col<T: std::fmt::Display>(msg: T, col: UserColour){ set_colour(col, FGBG::FG); println!("{}", msg); } /// Print to stdout with text and background colours. /// /// # Example /// /// ``` /// use term_basics_linux::tbl; /// tbl::print_cols("No vegetal!", tbl::UserColour::Green, tbl::UserColour::Black); /// ``` pub fn print_cols<T: std::fmt::Display>(msg: T, fg: UserColour, bg: UserColour){ set_colours(fg, bg); print!("{}", msg); } /// Print to stdout with text and background colours. /// /// # Example /// /// ``` /// use term_basics_linux::tbl; /// tbl::println_cols("No vegetal!", tbl::UserColour::Green, tbl::UserColour::Black); /// ``` pub fn println_cols<T: std::fmt::Display>(msg: T, fg: UserColour, bg: UserColour){ set_colours(fg, bg); println!("{}", msg); } /// Print to stdout with text style. /// /// # Example /// /// ``` /// use term_basics_linux::tbl; /// tbl::print_style("I am bold.", tbl::TextStyle::Bold); /// ``` pub fn print_style<T: std::fmt::Display>(msg: T, sty: TextStyle){ set_style(sty); print!("{}", msg); } /// Print to stdout with text style. /// /// # Example /// /// ``` /// use term_basics_linux::tbl; /// tbl::println_style("I am bold.", tbl::TextStyle::Bold); /// ``` pub fn println_style<T: std::fmt::Display>(msg: T, sty: TextStyle){ set_style(sty); println!("{}", msg); } /// Print to stdout with text and background colours and style. /// /// # Example /// /// ``` /// use term_basics_linux::tbl; /// tbl::print_cols_style("No vegetal!", tbl::UserColour::Green, tbl::UserColour::Black, tbl::TextStyle::Bold); /// ``` pub fn print_cols_style<T: std::fmt::Display>(msg: T, fg: UserColour, bg: UserColour, sty: TextStyle){ set_colours(fg, bg); set_style(sty); print!("{}", msg); } /// Print to stdout with text and background colours and style. /// /// # Example /// /// ``` /// use term_basics_linux::tbl; /// tbl::println_cols_style("No vegetal!", tbl::UserColour::Green, tbl::UserColour::Black, tbl::TextStyle::Bold); /// ``` pub fn println_cols_style<T: std::fmt::Display>(msg: T, fg: UserColour, bg: UserColour, sty: TextStyle){ set_colours(fg, bg); set_style(sty); println!("{}", msg); } /// Returns the character as u8 typed by the user. /// It will return immediately after being typed, without the user pressing 'enter'. /// /// # Example /// /// ``` /// use term_basics_linux::tbl; /// //print user input until spacebar is pressed /// loop{ /// let x = tbl::getch(); /// if x == 32 { break; } /// print!("{}", x as char); /// } /// ``` pub fn getch() -> u8{ //https://stackoverflow.com/questions/26321592/how-can-i-read-one-character-from-stdin-without-having-to-hit-enter let stdin = 0; let termios = Termios::from_fd(stdin).unwrap(); let mut new_termios = termios.clone(); new_termios.c_lflag &= !(ICANON | ECHO); tcsetattr(stdin, TCSANOW, &mut new_termios).unwrap(); let stdout = io::stdout(); let mut reader = io::stdin(); let mut buffer = [0;1]; stdout.lock().flush().unwrap(); reader.read_exact(&mut buffer).unwrap(); tcsetattr(stdin, TCSANOW, & termios).unwrap(); return buffer[0]; } /// Prints the result of getch as u8, infinite loop. Can be used for testing. /// /// # Example /// /// ``` /// use term_basics_linux::tbl; /// let user_input = tbl::test_chars(); /// ``` pub fn test_chars(){ loop { print!("{:?}\n", getch()); } } /// Lets the user type text. It returns the string after the user presses 'enter'. /// It supports moving the cursor with the arrow keys, /// going to the begin and end of the line using 'home' and 'end' /// and deleting characters with backspace and the delete key. /// /// # Example /// /// ``` /// use term_basics_linux::tbl; /// let user_input = tbl::input_field(); /// ``` pub fn input_field() -> String{ fn charvec_to_string(vec: &Vec<char>) -> String{ let mut string = String::new(); for &ch in vec { string.push(ch); } return string; } fn typed_char(ch: u8, buff: &mut Vec<char>, gstate: &mut u8, pos: &mut usize){ let ch = ch as char; buff.insert(*pos, ch); if *pos != buff.len() - 1{ for i in *pos..buff.len(){ print!("{}", buff[i]); } for _ in *pos..buff.len()-1{ print!("{}", 8 as char); } }else{ print!("{}", ch); } *gstate = 0; *pos += 1; } let mut res = Vec::new(); let mut gstate: u8 = 0; let mut hoen_state: u8 = 0; let mut pos = 0; //set_colour(MsgType::Normal); loop { match getch(){ 10 => { print!("\n"); break; } //enter 127 => { //backspace if res.len() <= 0 { continue; } res.pop(); for _ in 0..res.len() + 1 { print!("{}", 8 as char); } let mut printres = res.clone(); printres.push(' '); print(charvec_to_string(&printres)); print!("{}", 8 as char); //print!("\x1B[1D"); //also works gstate = 0; pos = res.len(); } 27 => { //first char in arrow code and home/end code and other char combos gstate = 1; hoen_state = 1; } 91 => { //2nd char in arrow code and home/end code and other char combos if gstate == 1 { gstate = 2; } if hoen_state == 1 { hoen_state = 2; } } 65 => { //up arrow if gstate == 2 {} else { typed_char(65, &mut res, &mut gstate, &mut pos); } } 66 => { //down arrow if gstate == 2 {} else { typed_char(66, &mut res, &mut gstate, &mut pos); } } 72 => { //home key if hoen_state != 2 { continue; } for _ in 0..pos { print!("{}", 8 as char); } pos = 0; hoen_state = 0; } 52 => { //end key 3e char if hoen_state == 2 { hoen_state = 3; } } 126 => { //end key if hoen_state != 3 { continue; } for _ in pos..res.len() { print!("\x1B[1C"); } pos = res.len(); hoen_state = 0; } 80 => { //delete key if gstate != 2 { continue; } if pos == res.len() { continue; } res.remove(pos); let len = res.len() - pos; for i in pos..res.len(){ print!("{}", res[i]); } print!(" "); for _ in 0..len + 1{ print!("{}", 8 as char); } } 67 => { //right arrow if gstate == 2 { if pos < res.len() { print!("\x1B[1C"); } gstate = 0; pos = min(pos + 1, res.len()); } else { typed_char(67, &mut res, &mut gstate, &mut pos); } } 68 => { //left arrow if gstate == 2 { if pos > 0 { print!("{}", 8 as char); } gstate = 0; pos = max(pos as i32 - 1, 0 as i32) as usize; } else { typed_char(68, &mut res, &mut gstate, &mut pos); } } x => { typed_char(x, &mut res, &mut gstate, &mut pos); } } } return charvec_to_string(&res); } /// Prints a message to the user. /// The user can type its input next to the message on the same line. /// It will return the user input after the user pressed enter. /// It uses term_basics_linux::tbl::input_field and supports the same operation. /// /// # Example /// /// ``` /// use term_basics_linux::tbl; /// let name = tbl::prompt("type your name: "); /// tbl::print("Your name: "); /// tbl::println(name); /// ``` pub fn prompt(msg : &str) -> String{ print(msg); std::io::stdout().flush().expect("Error: stdout flush failed."); return input_field(); } /*pub fn read_bool(msg: &str, inputs: &mut Option<VecDeque<astr::Astr>>) -> bool{ let line; if inputs.is_none(){line = prompt(&msg);} else{ let res = inputs.as_mut().unwrap().pop_front(); if res.is_none(){line = prompt(&msg);} else {line = res.unwrap().tostring();} } match line.as_ref(){ "y" => true, "ye" => true, "yes" => true, "ok" => true, "+" => true, "t" => true, "tr" => true, "tru" => true, "true" => true, _ => false, } }*/ }