2018-08-22 02:05:53 +10:00
|
|
|
extern crate crossterm;
|
|
|
|
|
2018-08-24 06:16:31 +10:00
|
|
|
use crossterm::{Screen, Crossterm, screen};
|
2018-08-22 02:05:53 +10:00
|
|
|
use crossterm::terminal::{terminal,Terminal, ClearType};
|
2018-08-24 06:16:31 +10:00
|
|
|
use crossterm::cursor::{TerminalCursor, cursor};
|
|
|
|
use crossterm::input::input;
|
2018-08-22 02:05:53 +10:00
|
|
|
use std::sync::{Arc,Mutex};
|
|
|
|
use std::io::Read;
|
|
|
|
use std::{thread,time};
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
use crossterm::color;
|
|
|
|
|
|
|
|
let screen = Screen::new(true);
|
|
|
|
let crossterm = Crossterm::new(&screen);
|
|
|
|
let cursor = crossterm.cursor();
|
|
|
|
cursor.hide();
|
|
|
|
|
|
|
|
let mut input_buf = Arc::new(Mutex::new(String::new()));
|
|
|
|
|
2018-08-24 02:15:18 +10:00
|
|
|
let threads = log(input_buf.clone(),&screen);
|
2018-08-22 02:05:53 +10:00
|
|
|
|
2018-08-24 06:16:31 +10:00
|
|
|
|
|
|
|
let mut count = 0;
|
|
|
|
|
2018-08-24 02:15:18 +10:00
|
|
|
thread::spawn(move || {
|
2018-08-24 06:16:31 +10:00
|
|
|
let input = input(&screen);
|
2018-08-24 02:15:18 +10:00
|
|
|
let mut stdin = input.read_async().bytes();
|
2018-08-22 02:05:53 +10:00
|
|
|
|
2018-08-24 02:15:18 +10:00
|
|
|
loop
|
2018-08-22 02:05:53 +10:00
|
|
|
{
|
2018-08-24 02:15:18 +10:00
|
|
|
let a = stdin.next();
|
|
|
|
|
|
|
|
match a {
|
2018-08-24 06:16:31 +10:00
|
|
|
Some(Ok(13)) =>
|
2018-08-24 02:15:18 +10:00
|
|
|
{
|
|
|
|
input_buf.lock().unwrap().clear();
|
|
|
|
|
|
|
|
// need to start receiving again because if pressed enter then async reading will stop
|
2018-08-24 06:16:31 +10:00
|
|
|
// stdin = input.read_async().bytes();
|
2018-08-24 02:15:18 +10:00
|
|
|
}
|
2018-08-24 06:16:31 +10:00
|
|
|
Some(Ok(val)) =>
|
2018-08-24 02:15:18 +10:00
|
|
|
{
|
2018-08-24 06:16:31 +10:00
|
|
|
// println!("{:?}",a);
|
2018-08-24 02:15:18 +10:00
|
|
|
input_buf.lock().unwrap().push(a.unwrap().unwrap() as char);
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
thread::sleep(time::Duration::from_millis(100));
|
|
|
|
count += 1;
|
2018-08-22 02:05:53 +10:00
|
|
|
}
|
2018-08-24 06:16:31 +10:00
|
|
|
}).join();
|
2018-08-22 02:05:53 +10:00
|
|
|
|
|
|
|
|
|
|
|
for thread in threads
|
|
|
|
{
|
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor.show();
|
|
|
|
}
|
|
|
|
|
2018-08-24 02:15:18 +10:00
|
|
|
fn log(input_buf: Arc<Mutex<String>>, screen: &Screen) -> Vec<thread::JoinHandle<()>>
|
2018-08-22 02:05:53 +10:00
|
|
|
{
|
|
|
|
let mut threads = Vec::with_capacity(10);
|
|
|
|
|
2018-08-24 02:15:18 +10:00
|
|
|
let (_, term_height) = terminal(&screen).terminal_size();
|
|
|
|
|
|
|
|
for i in 0..1
|
2018-08-22 02:05:53 +10:00
|
|
|
{
|
|
|
|
let input_buffer = input_buf.clone();
|
2018-08-24 02:15:18 +10:00
|
|
|
let clone_stdout = screen.stdout.clone();
|
2018-08-22 02:05:53 +10:00
|
|
|
|
2018-08-25 02:35:13 +10:00
|
|
|
let crossterm = Crossterm::new(screen.stdout.clone());
|
2018-08-24 06:16:31 +10:00
|
|
|
|
2018-08-22 02:05:53 +10:00
|
|
|
let join = thread::spawn( move || {
|
|
|
|
let cursor = crossterm.cursor();
|
|
|
|
let terminal = crossterm.terminal();
|
|
|
|
|
|
|
|
for j in 0..1000
|
|
|
|
{
|
|
|
|
swap_write(format!("Some output: {} from thread: {}", j, i).as_ref(), &input_buffer.lock().unwrap(), &terminal, &cursor, term_height);
|
|
|
|
thread::sleep(time::Duration::from_millis(300));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
threads.push(join);
|
|
|
|
}
|
|
|
|
|
|
|
|
return threads;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn swap_write(msg: &str, input_buf: &String, terminal: &Terminal, cursor: &TerminalCursor, term_height: u16) {
|
|
|
|
cursor.goto(0, term_height);
|
|
|
|
terminal.clear(ClearType::CurrentLine);
|
|
|
|
terminal.write(format!("{}\r\n", msg));
|
|
|
|
terminal.write(format!(">{}", input_buf));
|
|
|
|
}
|