From 348d43c92bc20bcb81ec8fd894c6c19f1d0658e0 Mon Sep 17 00:00:00 2001 From: dvkt Date: Fri, 22 Nov 2019 00:03:44 -0800 Subject: [PATCH] raw mode in progress --- src/main.rs | 46 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5a283d3..b2b499a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,18 @@ #![allow(unused_must_use)] -use std::io::{Read, Write}; +extern crate termion; + +use std::io::{stdin, stdout, Read, Write}; use std::net::TcpStream; +use termion::event::Key; +use termion::input::TermRead; +use termion::raw::IntoRawMode; + fn main() { - TcpStream::connect("gopher.black:70") + let stdin = stdin(); + let mut stdout = stdout().into_raw_mode().unwrap(); + TcpStream::connect("phkt.io:70") .and_then(|mut stream| { stream.write("\r\n".as_ref()).unwrap(); let mut buf = String::new(); @@ -23,7 +31,7 @@ fn main() { start = false } else if skip_to_end { if c == '\n' { - print!("{}", c); + println!(""); start = true; skip_to_end = false; } @@ -36,6 +44,38 @@ fn main() { } } } + + for c in stdin.keys() { + // Clear the current line. + write!( + stdout, + "{}{}", + termion::cursor::Goto(1, 1), + termion::clear::CurrentLine + ) + .unwrap(); + + // Print the key we type... + match c.unwrap() { + // Exit. + Key::Char('q') => break, + Key::Char(c) => println!("{}", c), + Key::Alt(c) => println!("Alt-{}", c), + Key::Ctrl('c') => { + return Ok(()); + } + Key::Ctrl(c) => println!("Ctrl-{}", c), + Key::Left => println!(""), + Key::Right => println!(""), + Key::Up => println!(""), + Key::Down => println!(""), + _ => println!("Other"), + } + + // Flush again. + stdout.flush().unwrap(); + } + Ok(()) }) .map_err(|err| {