From 2b1d545a9904a1b9d64a1acfa33e8c6831c768bd Mon Sep 17 00:00:00 2001
From: gwenn <gtreguier@gmail.com>
Date: Sat, 30 Jul 2016 08:35:57 +0200
Subject: [PATCH] Move clear_screen in tty module.

---
 src/lib.rs         | 23 +----------------------
 src/tty/unix.rs    |  9 ++++++++-
 src/tty/windows.rs | 17 ++++++++++++++++-
 3 files changed, 25 insertions(+), 24 deletions(-)

diff --git a/src/lib.rs b/src/lib.rs
index 8a8ed339..d9dd423f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -237,27 +237,6 @@ fn write_and_flush(w: &mut Write, buf: &[u8]) -> Result<()> {
     Ok(())
 }
 
-/// Clear the screen. Used to handle ctrl+l
-#[cfg(unix)]
-fn clear_screen(s: &mut State) -> Result<()> {
-    write_and_flush(s.out, b"\x1b[H\x1b[2J")
-}
-#[cfg(windows)]
-fn clear_screen(s: &mut State) -> Result<()> {
-    let handle = s.output_handle;
-    let mut info = unsafe { mem::zeroed() };
-    check!(kernel32::GetConsoleScreenBufferInfo(handle, &mut info));
-    let coord = winapi::COORD { X: 0, Y: 0 };
-    check!(kernel32::SetConsoleCursorPosition(handle, coord));
-    let mut _count = 0;
-    check!(kernel32::FillConsoleOutputCharacterA(handle,
-                                                 ' ' as winapi::CHAR,
-                                                 (info.dwSize.X * info.dwSize.Y) as winapi::DWORD,
-                                                 coord,
-                                                 &mut _count));
-    Ok(())
-}
-
 /// Beep, used for completion when there is nothing to complete or when all
 /// the choices were already shown.
 fn beep() -> Result<()> {
@@ -788,7 +767,7 @@ fn readline_edit(prompt: &str,
             }
             KeyPress::Ctrl('L') => {
                 // Clear the screen leaving the current line at the top of the screen.
-                try!(clear_screen(&mut s));
+                try!(tty::clear_screen(&mut s.out, s.output_handle));
                 try!(s.refresh_line())
             }
             KeyPress::Ctrl('N') |
diff --git a/src/tty/unix.rs b/src/tty/unix.rs
index aadcefea..95662d76 100644
--- a/src/tty/unix.rs
+++ b/src/tty/unix.rs
@@ -1,5 +1,5 @@
 use std;
-use std::io::Read;
+use std::io::{Read, Write};
 use libc;
 use nix;
 use nix::sys::termios;
@@ -105,6 +105,13 @@ pub fn stdout_handle() -> Result<Handle> {
     Ok(())
 }
 
+/// Clear the screen. Used to handle ctrl+l
+pub fn clear_screen(w: &mut Write, _: Handle) -> Result<()> {
+    try!(w.write_all(b"\x1b[H\x1b[2J"));
+    try!(w.flush());
+    Ok(())
+}
+
 /// Console input reader
 pub struct RawReader<R> {
     chars: char_iter::Chars<R>,
diff --git a/src/tty/windows.rs b/src/tty/windows.rs
index fef3199d..65a4769a 100644
--- a/src/tty/windows.rs
+++ b/src/tty/windows.rs
@@ -102,6 +102,21 @@ pub fn stdout_handle() -> Result<Handle> {
     Ok(handle)
 }
 
+/// Clear the screen. Used to handle ctrl+l
+pub fn clear_screen(_: &mut Write, handle: Handle) -> Result<()> {
+    let mut info = unsafe { mem::zeroed() };
+    check!(kernel32::GetConsoleScreenBufferInfo(handle, &mut info));
+    let coord = winapi::COORD { X: 0, Y: 0 };
+    check!(kernel32::SetConsoleCursorPosition(handle, coord));
+    let mut _count = 0;
+    check!(kernel32::FillConsoleOutputCharacterA(handle,
+                                                 ' ' as winapi::CHAR,
+                                                 (info.dwSize.X * info.dwSize.Y) as winapi::DWORD,
+                                                 coord,
+                                                 &mut _count));
+    Ok(())
+}
+
 /// Console input reader
 pub struct RawReader<R> {
     handle: winapi::HANDLE,
@@ -121,7 +136,7 @@ impl<R: Read> RawReader<R> {
 
     pub fn next_key(&mut self, _: bool) -> Result<KeyPress> {
         use std::char::decode_utf16;
-        //use winapi::{LEFT_ALT_PRESSED, LEFT_CTRL_PRESSED, RIGHT_ALT_PRESSED, RIGHT_CTRL_PRESSED};
+        // use winapi::{LEFT_ALT_PRESSED, LEFT_CTRL_PRESSED, RIGHT_ALT_PRESSED, RIGHT_CTRL_PRESSED};
         use winapi::{LEFT_ALT_PRESSED, RIGHT_ALT_PRESSED};
 
         let mut rec: winapi::INPUT_RECORD = unsafe { mem::zeroed() };
-- 
GitLab