From 95427077d8a577eb5f829369849405f16e6bc3d2 Mon Sep 17 00:00:00 2001 From: dvkt Date: Tue, 31 Dec 2019 00:36:37 -0800 Subject: [PATCH] start keeping stdout around --- src/menu.rs | 2 +- src/text.rs | 2 +- src/ui.rs | 80 ++++++++++++++++++++++++++++++++++++++--------------- 3 files changed, 59 insertions(+), 25 deletions(-) diff --git a/src/menu.rs b/src/menu.rs index 75f0890..e937781 100644 --- a/src/menu.rs +++ b/src/menu.rs @@ -171,7 +171,7 @@ impl Menu { typ if !typ.is_supported() => push!("107;91", name), _ => push!("0", name), } - out.push('\n'); + out.push_str("\r\n"); } if self.searching { out.push_str(&self.render_input()); diff --git a/src/text.rs b/src/text.rs index 56a3012..042d074 100644 --- a/src/text.rs +++ b/src/text.rs @@ -122,7 +122,7 @@ impl View for Text { out.push_str(&indent); } out.push_str(line); - out.push('\n'); + out.push_str("\r\n"); } out } diff --git a/src/ui.rs b/src/ui.rs index cb6e652..2ef45a0 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -12,14 +12,20 @@ use crate::{ utils, }; use std::{ - io::{stdin, stdout, Result, Write}, - process, - process::Stdio, + cell::RefCell, + io::{self, stdin, stdout, Result, Write}, + process::{self, Stdio}, sync::mpsc, thread, time::Duration, }; -use termion::{color, input::TermRead, raw::IntoRawMode, terminal_size}; +use termion::{ + color, + input::{MouseTerminal, TermRead}, + raw::{IntoRawMode, RawTerminal}, + screen::AlternateScreen, + terminal_size, +}; pub type Key = termion::event::Key; pub type Page = Box; @@ -34,6 +40,7 @@ pub struct UI { running: bool, // main ui loop running? pub size: (usize, usize), // cols, rows status: String, // status message, if any + out: RefCell>>>, } impl UI { @@ -45,6 +52,9 @@ impl UI { running: true, size: (0, 0), status: String::new(), + out: RefCell::new(MouseTerminal::from(AlternateScreen::from( + stdout().into_raw_mode().unwrap(), + ))), } } @@ -59,23 +69,24 @@ impl UI { pub fn draw(&mut self) { if self.dirty { - print!( + let screen = self.render(); + let status = self.render_status().unwrap_or_else(|| "".into()); + let mut out = self.out.borrow_mut(); + write!( + out, "{}{}{}{}{}", termion::clear::All, termion::cursor::Goto(1, 1), termion::cursor::Hide, - self.render(), - self.render_status().unwrap_or_else(|| "".into()), + screen, + status, ); - + out.flush(); self.dirty = false; } } pub fn update(&mut self) { - let mut stdout = stdout().into_raw_mode().unwrap(); - stdout.flush().unwrap(); - let action = self.process_page_input(); if let Err(e) = self.process_action(action) { self.set_status(format!("{}{}", color::Fg(color::LightRed), e)); @@ -267,7 +278,9 @@ impl UI { fn confirm(&self, question: &str) -> bool { let rows = self.rows(); - print!( + let mut out = self.out.borrow_mut(); + write!( + out, "{}{}{}{} [Y/n]: {}", color::Fg(color::Reset), termion::cursor::Goto(1, rows), @@ -275,7 +288,7 @@ impl UI { question, termion::cursor::Show, ); - stdout().flush(); + out.flush(); if let Some(Ok(key)) = stdin().keys().next() { match key { @@ -292,7 +305,9 @@ impl UI { fn prompt(&self, prompt: &str) -> Option { let rows = self.rows(); - print!( + let mut out = self.out.borrow_mut(); + write!( + out, "{}{}{}{}{}", color::Fg(color::Reset), termion::cursor::Goto(1, rows), @@ -300,22 +315,32 @@ impl UI { prompt, termion::cursor::Show, ); - stdout().flush(); + out.flush(); let mut input = String::new(); for k in stdin().keys() { if let Ok(key) = k { match key { Key::Char('\n') => { - print!("{}{}", termion::clear::CurrentLine, termion::cursor::Hide); - stdout().flush(); + write!( + out, + "{}{}", + termion::clear::CurrentLine, + termion::cursor::Hide + ); + out.flush(); return Some(input); } Key::Char(c) => input.push(c), Key::Esc | Key::Ctrl('c') => { if input.is_empty() { - print!("{}{}", termion::clear::CurrentLine, termion::cursor::Hide); - stdout().flush(); + write!( + out, + "{}{}", + termion::clear::CurrentLine, + termion::cursor::Hide + ); + out.flush(); return None; } else { input.clear(); @@ -330,14 +355,15 @@ impl UI { break; } - print!( + write!( + out, "{}{}{}{}", termion::cursor::Goto(1, rows), termion::clear::CurrentLine, prompt, input, ); - stdout().flush(); + out.flush(); } if !input.is_empty() { @@ -461,8 +487,16 @@ impl Default for UI { impl Drop for UI { fn drop(&mut self) { - print!("\x1b[?25h"); // show cursor - stdout().flush(); + let mut out = self.out.borrow_mut(); + write!( + out, + "{}{}{}{}", + color::Fg(color::Reset), + color::Bg(color::Reset), + termion::cursor::Show, + termion::clear::All, + ); + out.flush(); } }