Guest User

Untitled

a guest
Oct 18th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. if std::process::Command::new("cls").status().unwrap().success() {
  2. println!("screen successfully cleared");
  3. }
  4.  
  5. [dependencies]
  6. winapi = "0.2.8"
  7. kernel32-sys = "0.2.1"
  8.  
  9. extern crate kernel32;
  10. extern crate winapi;
  11.  
  12. use winapi::HANDLE;
  13. use winapi::wincon::CONSOLE_SCREEN_BUFFER_INFO;
  14. use winapi::wincon::COORD;
  15. use winapi::wincon::SMALL_RECT;
  16. use winapi::WORD;
  17. use winapi::DWORD;
  18.  
  19. static mut CONSOLE_HANDLE: Option<HANDLE> = None;
  20.  
  21.  
  22. fn get_output_handle() -> HANDLE {
  23. unsafe {
  24. if let Some(handle) = CONSOLE_HANDLE {
  25. return handle;
  26. } else {
  27. let handle = kernel32::GetStdHandle(winapi::STD_OUTPUT_HANDLE);
  28. CONSOLE_HANDLE = Some(handle);
  29. return handle;
  30. }
  31. }
  32. }
  33.  
  34. fn get_buffer_info() -> winapi::CONSOLE_SCREEN_BUFFER_INFO {
  35. let handle = get_output_handle();
  36. if handle == winapi::INVALID_HANDLE_VALUE {
  37. panic!("NoConsole")
  38. }
  39. let mut buffer = CONSOLE_SCREEN_BUFFER_INFO {
  40. dwSize: COORD { X: 0, Y: 0 },
  41. dwCursorPosition: COORD { X: 0, Y: 0 },
  42. wAttributes: 0 as WORD,
  43. srWindow: SMALL_RECT {
  44. Left: 0,
  45. Top: 0,
  46. Right: 0,
  47. Bottom: 0,
  48. },
  49. dwMaximumWindowSize: COORD { X: 0, Y: 0 },
  50. };
  51. unsafe {
  52. kernel32::GetConsoleScreenBufferInfo(handle, &mut buffer);
  53. }
  54. buffer
  55. }
  56.  
  57. fn clear() {
  58. let handle = get_output_handle();
  59. if handle == winapi::INVALID_HANDLE_VALUE {
  60. panic!("NoConsole")
  61. }
  62.  
  63. let screen_buffer = get_buffer_info();
  64. let console_size: DWORD = screen_buffer.dwSize.X as u32 * screen_buffer.dwSize.Y as u32;
  65. let coord_screen = COORD { X: 0, Y: 0 };
  66.  
  67. let mut amount_chart_written: DWORD = 0;
  68. unsafe {
  69. kernel32::FillConsoleOutputCharacterW(
  70. handle,
  71. 32 as winapi::WCHAR,
  72. console_size,
  73. coord_screen,
  74. &mut amount_chart_written,
  75. );
  76. }
  77. set_cursor_possition(0, 0);
  78. }
  79.  
  80. fn set_cursor_possition(y: i16, x: i16) {
  81. let handle = get_output_handle();
  82. if handle == winapi::INVALID_HANDLE_VALUE {
  83. panic!("NoConsole")
  84. }
  85. unsafe {
  86. kernel32::SetConsoleCursorPosition(handle, COORD { X: x, Y: y });
  87. }
  88. }
  89.  
  90. fn main() {
  91. loop {
  92. let mut input = String::new();
  93. std::io::stdin()
  94. .read_line(&mut input)
  95. .expect("Failed to read line");
  96.  
  97. println!("You typed: {}", input);
  98.  
  99. if input.trim() == "clear" {
  100. clear();
  101. }
  102. }
  103. }
Add Comment
Please, Sign In to add comment