title: How do I create a new line when using termion in raw mode?
body: <p>I am using the <a href="https://docs.rs/termion/1.5.1/termion/" rel="nofollow noreferrer">termion crate</a> to capture user input keys with <code>termion::raw::IntoRawMode</code> like a event listener. I am unable to print a new line in the console because stdout is in "raw mode" and <code>\n</code> is not recognized as a new line. I don't know how to disable raw mode when <code>draw_user</code> method is called and then return to raw mode in order to listen to key events again.</p>
  
  <p>The second line starts with white spaces before, but I don't know why:</p>
  
  <p><a href="https://i.stack.imgur.com/ASnOp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ASnOp.png" alt="enter image description here"></a></p>
  
  <p>Here is my code:</p>
  
  <pre><code>extern crate termion;
  
  use termion::event::Key;
  use termion::input::TermRead;
  use termion::raw::IntoRawMode;
  use std::io::{stdin, stdout, Write};
  
  fn main() {
      let mut x: u16 = 1;
      let mut y: u16 = 1;
      // Get the standard input stream.
      let stdin = stdin();
      // Get the standard output stream and go to raw mode.
      let mut stdout = stdout().into_raw_mode().unwrap();
      write!(
          stdout,
          "{}{}{}",
          // Clear the screen.
          termion::clear::All,
          // Goto (1,1).
          termion::cursor::Goto(1, 1),
          // Hide the cursor.
          termion::cursor::Hide
      ).unwrap();
      // Flush stdout (i.e. make the output appear).
      stdout.flush().unwrap();
  
      for c in stdin.keys() {
          // Clear the current line.
          write!(
              stdout,
              "{}{}{}",
              termion::cursor::Goto(1, 1),
              termion::clear::CurrentLine,
              termion::clear::BeforeCursor
          ).unwrap();
  
          // Print the key we type...
          match c.unwrap() {
              // Exit.
              Key::Char('q') =&gt; break,
              Key::Ctrl('c') =&gt; break,
              Key::Left =&gt; {
                  x -= 1;
                  draw_user(&amp;mut x, &amp;mut y);
              }
              Key::Right =&gt; {
                  x += 1;
                  draw_user(&amp;mut x, &amp;mut y);
              }
              Key::Up =&gt; {
                  y -= 1;
                  draw_user(&amp;mut x, &amp;mut y);
              }
              Key::Down =&gt; {
                  y += 1;
                  draw_user(&amp;mut x, &amp;mut y);
              }
              _ =&gt; println!(""),
          }
          stdout.flush().unwrap();
      }
      // Show the cursor again before we exit.
      write!(
          stdout,
          "{}{}",
          termion::cursor::Show,
          termion::clear::AfterCursor
      ).unwrap();
      // Flush again.
  }
  
  fn draw_user(x: &amp;mut u16, y: &amp;mut u16) {
      let termsize = termion::terminal_size().ok();
      let termwidth = termsize.map(|(w, _)| w - 10).unwrap();
      let termheight = termsize.map(|(_, h)| h).unwrap();
      if *x &lt; 1 {
          *x = 1;
      }
      if *x &gt; termwidth {
          *x = termwidth;
      }
      if *y &lt; 1 {
          *y = 1;
      }
      if *y &gt; termheight {
          *y = termheight;
      }
      //println!("x:{}, y:{}", *x, *y);
      for h in 1..termheight + 1 {
          for w in 1..termwidth + 1 {
              //print!("w:{}",w);
              if h == *y &amp;&amp; w == *x {
                  print!("☺");
              } else {
                  print!("*");
              }
          }
          println!("");
      }
  }
  </code></pre>