start wrapping termion

pull/14/head
chris west 4 years ago
parent eedf1dfa34
commit a4fd553431

@ -45,6 +45,7 @@ pub mod help;
pub mod history; pub mod history;
pub mod menu; pub mod menu;
pub mod phetchdir; pub mod phetchdir;
pub mod terminal;
pub mod text; pub mod text;
pub mod ui; pub mod ui;

@ -9,10 +9,10 @@
use crate::{ use crate::{
config::Config, config::Config,
gopher::{self, Type}, gopher::{self, Type},
terminal,
ui::{self, Action, Key, View, MAX_COLS, SCROLL_LINES}, ui::{self, Action, Key, View, MAX_COLS, SCROLL_LINES},
}; };
use std::fmt; use std::fmt;
use termion::{clear, cursor};
/// The Menu holds our Gopher Lines, a list of links, and maintains /// The Menu holds our Gopher Lines, a list of links, and maintains
/// both where the cursor is on screen and which lines need to be /// both where the cursor is on screen and which lines need to be
@ -333,12 +333,12 @@ impl Menu {
out.push_str(color!(Reset)); out.push_str(color!(Reset));
// clear rest of line // clear rest of line
out.push_str(clear::UntilNewline.as_ref()); out.push_str(terminal::ClearUntilNewline.as_ref());
out.push_str("\r\n"); out.push_str("\r\n");
} }
// clear remainder of screen // clear remainder of screen
out.push_str(clear::AfterCursor.as_ref()); out.push_str(terminal::ClearAfterCursor.as_ref());
out out
} }
@ -364,7 +364,7 @@ impl Menu {
return None; return None;
} }
let (x, y) = self.screen_coords(link)?; let (x, y) = self.screen_coords(link)?;
Some(format!("{} {}", cursor::Goto(x, y), cursor::Hide)) Some(format!("{} {}", terminal::Goto(x, y), terminal::HideCursor))
} }
/// Print this string to draw the cursor on screen. /// Print this string to draw the cursor on screen.
@ -376,8 +376,8 @@ impl Menu {
let (x, y) = self.screen_coords(self.link)?; let (x, y) = self.screen_coords(self.link)?;
Some(format!( Some(format!(
"{}\x1b[97;1m*\x1b[0m{}", "{}\x1b[97;1m*\x1b[0m{}",
cursor::Goto(x, y), terminal::Goto(x, y),
cursor::Hide terminal::HideCursor
)) ))
} }
@ -388,14 +388,14 @@ impl Menu {
/// User input field. /// User input field.
fn render_input(&self) -> String { fn render_input(&self) -> String {
format!("Find: {}{}", self.input, cursor::Show) format!("Find: {}{}", self.input, terminal::ShowCursor)
} }
fn redraw_input(&self) -> Action { fn redraw_input(&self) -> Action {
if self.searching { if self.searching {
Action::Status(self.render_input()) Action::Status(self.render_input())
} else { } else {
Action::Status(cursor::Hide.to_string()) Action::Status(terminal::HideCursor.to_string())
} }
} }

@ -0,0 +1,18 @@
//! The terminal module mostly provides terminal escape sequences for
//! things like clearing the screen or going into alternate mode.
//!
//! It wraps termion for now, but we may move away from termion in the
//! future and this will help.
use termion;
pub use termion::cursor::Goto;
pub use termion::cursor::Hide as HideCursor;
pub use termion::cursor::Show as ShowCursor;
pub use termion::screen::ToAlternateScreen;
pub use termion::screen::ToMainScreen;
pub use termion::clear::AfterCursor as ClearAfterCursor;
pub use termion::clear::CurrentLine as ClearCurrentLine;
pub use termion::clear::UntilNewline as ClearUntilNewline;

@ -4,10 +4,10 @@
use crate::{ use crate::{
config::Config, config::Config,
terminal,
ui::{self, Action, Key, View, MAX_COLS, SCROLL_LINES}, ui::{self, Action, Key, View, MAX_COLS, SCROLL_LINES},
}; };
use std::fmt; use std::fmt;
use termion::clear;
/// The Text View holds the raw Gopher response as well as information /// The Text View holds the raw Gopher response as well as information
/// about which lines should currently be displayed on screen. /// about which lines should currently be displayed on screen.
@ -156,13 +156,13 @@ impl View for Text {
out.push_str(&line); out.push_str(&line);
// clear rest of line // clear rest of line
out.push_str(&format!("{}", clear::UntilNewline)); out.push_str(&format!("{}", terminal::ClearUntilNewline));
out.push_str("\r\n"); out.push_str("\r\n");
} }
// clear remainder of screen // clear remainder of screen
out.push_str(&format!("{}", clear::AfterCursor)); out.push_str(&format!("{}", terminal::ClearAfterCursor));
out out
} }

@ -22,6 +22,7 @@ use crate::{
gopher::{self, Type}, gopher::{self, Type},
help, history, help, history,
menu::Menu, menu::Menu,
terminal,
text::Text, text::Text,
utils, BUG_URL, utils, BUG_URL,
}; };
@ -109,14 +110,14 @@ impl UI {
pub fn startup(&mut self) { pub fn startup(&mut self) {
let mut out = self.out.borrow_mut(); let mut out = self.out.borrow_mut();
out.activate_raw_mode().expect(ERR_RAW_MODE); out.activate_raw_mode().expect(ERR_RAW_MODE);
write!(out, "{}", termion::screen::ToAlternateScreen).expect(ERR_SCREEN); write!(out, "{}", terminal::ToAlternateScreen).expect(ERR_SCREEN);
} }
/// Clean up after ourselves. Should only be used after running in /// Clean up after ourselves. Should only be used after running in
/// interactive mode. /// interactive mode.
pub fn shutdown(&mut self) { pub fn shutdown(&mut self) {
let mut out = self.out.borrow_mut(); let mut out = self.out.borrow_mut();
write!(out, "{}", termion::screen::ToMainScreen).expect(ERR_SCREEN); write!(out, "{}", terminal::ToMainScreen).expect(ERR_SCREEN);
} }
/// Main loop. /// Main loop.
@ -139,8 +140,8 @@ impl UI {
write!( write!(
out, out,
"{}{}{}{}", "{}{}{}{}",
termion::cursor::Goto(1, 1), terminal::Goto(1, 1),
termion::cursor::Hide, terminal::HideCursor,
screen, screen,
status, status,
)?; )?;
@ -161,7 +162,7 @@ impl UI {
self.status.clear(); self.status.clear();
} }
if let Err(e) = self.process_action(action) { if let Err(e) = self.process_action(action) {
self.set_status(&format!("{}{}{}", color::Red, e, termion::cursor::Hide)); self.set_status(&format!("{}{}{}", color::Red, e, terminal::HideCursor));
} }
} }
@ -300,13 +301,13 @@ impl UI {
} }
print!( print!(
"{}{}{}{}{}{}{}", "{}{}{}{}{}{}{}",
termion::cursor::Goto(1, rows), terminal::Goto(1, rows),
termion::cursor::Hide, terminal::HideCursor,
label, label,
".".repeat(i), ".".repeat(i),
termion::clear::UntilNewline, terminal::ClearUntilNewline,
color::Reset, color::Reset,
termion::cursor::Show, terminal::ShowCursor,
); );
stdout().flush().expect(ERR_STDOUT); stdout().flush().expect(ERR_STDOUT);
thread::sleep(Duration::from_millis(500)); thread::sleep(Duration::from_millis(500));
@ -354,14 +355,14 @@ impl UI {
let status = color_string!("TLS", Black, GreenBG); let status = color_string!("TLS", Black, GreenBG);
return Some(format!( return Some(format!(
"{}{}", "{}{}",
termion::cursor::Goto(self.cols() - 3, self.rows()), terminal::Goto(self.cols() - 3, self.rows()),
if self.config.emoji { "🔐" } else { &status }, if self.config.emoji { "🔐" } else { &status },
)); ));
} else if view.is_tor() { } else if view.is_tor() {
let status = color_string!("TOR", Bold, White, MagentaBG); let status = color_string!("TOR", Bold, White, MagentaBG);
return Some(format!( return Some(format!(
"{}{}", "{}{}",
termion::cursor::Goto(self.cols() - 3, self.rows()), terminal::Goto(self.cols() - 3, self.rows()),
if self.config.emoji { "🧅" } else { &status }, if self.config.emoji { "🧅" } else { &status },
)); ));
} }
@ -372,9 +373,9 @@ impl UI {
fn render_status(&self) -> String { fn render_status(&self) -> String {
format!( format!(
"{}{}{}{}{}{}", "{}{}{}{}{}{}",
termion::cursor::Hide, terminal::HideCursor,
termion::cursor::Goto(1, self.rows()), terminal::Goto(1, self.rows()),
termion::clear::CurrentLine, terminal::ClearCurrentLine,
self.status, self.status,
self.render_conn_status().unwrap_or_else(|| "".into()), self.render_conn_status().unwrap_or_else(|| "".into()),
color::Reset, color::Reset,
@ -402,10 +403,10 @@ impl UI {
out, out,
"{}{}{}{} [Y/n]: {}", "{}{}{}{} [Y/n]: {}",
color::Reset, color::Reset,
termion::cursor::Goto(1, rows), terminal::Goto(1, rows),
termion::clear::CurrentLine, terminal::ClearCurrentLine,
question, question,
termion::cursor::Show, terminal::ShowCursor,
) )
.expect(ERR_STDOUT); .expect(ERR_STDOUT);
out.flush().expect(ERR_STDOUT); out.flush().expect(ERR_STDOUT);
@ -431,11 +432,11 @@ impl UI {
out, out,
"{}{}{}{}{}{}", "{}{}{}{}{}{}",
color::Reset, color::Reset,
termion::cursor::Goto(1, rows), terminal::Goto(1, rows),
termion::clear::CurrentLine, terminal::ClearCurrentLine,
prompt, prompt,
input, input,
termion::cursor::Show, terminal::ShowCursor,
) )
.expect(ERR_STDOUT); .expect(ERR_STDOUT);
out.flush().expect(ERR_STDOUT); out.flush().expect(ERR_STDOUT);
@ -447,8 +448,8 @@ impl UI {
write!( write!(
out, out,
"{}{}", "{}{}",
termion::clear::CurrentLine, terminal::ClearCurrentLine,
termion::cursor::Hide terminal::HideCursor
) )
.expect(ERR_STDOUT); .expect(ERR_STDOUT);
out.flush().expect(ERR_STDOUT); out.flush().expect(ERR_STDOUT);
@ -459,8 +460,8 @@ impl UI {
write!( write!(
out, out,
"{}{}", "{}{}",
termion::clear::CurrentLine, terminal::ClearCurrentLine,
termion::cursor::Hide terminal::HideCursor
) )
.expect(ERR_STDOUT); .expect(ERR_STDOUT);
out.flush().expect(ERR_STDOUT); out.flush().expect(ERR_STDOUT);
@ -478,8 +479,8 @@ impl UI {
write!( write!(
out, out,
"{}{}{}{}", "{}{}{}{}",
termion::cursor::Goto(1, rows), terminal::Goto(1, rows),
termion::clear::CurrentLine, terminal::ClearCurrentLine,
prompt, prompt,
input, input,
) )
@ -531,10 +532,10 @@ impl UI {
/// Ctrl-Z: Suspend Unix process w/ SIGTSTP. /// Ctrl-Z: Suspend Unix process w/ SIGTSTP.
fn suspend(&mut self) { fn suspend(&mut self) {
let mut out = self.out.borrow_mut(); let mut out = self.out.borrow_mut();
write!(out, "{}", termion::screen::ToMainScreen).expect(ERR_SCREEN); write!(out, "{}", terminal::ToMainScreen).expect(ERR_SCREEN);
out.flush().expect(ERR_STDOUT); out.flush().expect(ERR_STDOUT);
unsafe { libc::raise(libc::SIGTSTP) }; unsafe { libc::raise(libc::SIGTSTP) };
write!(out, "{}", termion::screen::ToAlternateScreen).expect(ERR_SCREEN); write!(out, "{}", terminal::ToAlternateScreen).expect(ERR_SCREEN);
out.flush().expect(ERR_STDOUT); out.flush().expect(ERR_STDOUT);
self.dirty = true; self.dirty = true;
} }
@ -647,7 +648,7 @@ impl UI {
impl Drop for UI { impl Drop for UI {
fn drop(&mut self) { fn drop(&mut self) {
let mut out = self.out.borrow_mut(); let mut out = self.out.borrow_mut();
write!(out, "{}{}", color::Reset, termion::cursor::Show).expect(ERR_STDOUT); write!(out, "{}{}", color::Reset, terminal::ShowCursor).expect(ERR_STDOUT);
out.flush().expect(ERR_STDOUT); out.flush().expect(ERR_STDOUT);
} }
} }

Loading…
Cancel
Save