views remember their wide mode setting

pull/14/head
chris west 4 years ago
parent 6a429c8a86
commit e32d0c9da5

@ -158,14 +158,22 @@ impl View for Menu {
self.raw.as_ref() self.raw.as_ref()
} }
fn render(&mut self, cfg: &Config) -> String { fn render(&mut self) -> String {
self.render_lines(cfg) self.render_lines()
} }
fn respond(&mut self, key: Key) -> Action { fn respond(&mut self, key: Key) -> Action {
self.process_key(key) self.process_key(key)
} }
fn set_wide(&mut self, wide: bool) {
self.wide = wide;
}
fn wide(&mut self) -> bool {
self.wide
}
fn term_size(&mut self, cols: usize, rows: usize) { fn term_size(&mut self, cols: usize, rows: usize) {
self.size = (cols, rows); self.size = (cols, rows);
} }
@ -178,10 +186,12 @@ impl View for Menu {
impl Menu { impl Menu {
/// Create a representation of a Gopher Menu from a raw Gopher /// Create a representation of a Gopher Menu from a raw Gopher
/// response and a few options. /// response and a few options.
pub fn from(url: &str, response: String, tls: bool, tor: bool) -> Menu { pub fn from(url: &str, response: String, config: &Config, tls: bool) -> Menu {
Menu { Menu {
tls, tls,
tor, tor: config.tor,
wide: config.wide,
mode: config.mode,
..parse(url, response) ..parse(url, response)
} }
} }
@ -256,9 +266,7 @@ impl Menu {
Some((x as u16, y as u16)) Some((x as u16, y as u16))
} }
fn render_lines(&mut self, cfg: &Config) -> String { fn render_lines(&mut self) -> String {
self.wide = cfg.wide;
self.mode = cfg.mode;
let mut out = String::new(); let mut out = String::new();
let limit = if self.mode == ui::Mode::Run { let limit = if self.mode == ui::Mode::Run {
// only show as many lines as screen rows minus one // only show as many lines as screen rows minus one

@ -28,6 +28,8 @@ pub struct Text {
pub tls: bool, pub tls: bool,
/// Retrieved via Tor? /// Retrieved via Tor?
pub tor: bool, pub tor: bool,
/// UI mode. Interactive (Run), Printing, Raw mode...
mode: ui::Mode,
/// Currently in wide mode? /// Currently in wide mode?
pub wide: bool, pub wide: bool,
} }
@ -59,6 +61,14 @@ impl View for Text {
self.size = (cols, rows); self.size = (cols, rows);
} }
fn set_wide(&mut self, wide: bool) {
self.wide = wide;
}
fn wide(&mut self) -> bool {
self.wide
}
fn respond(&mut self, c: Key) -> Action { fn respond(&mut self, c: Key) -> Action {
match c { match c {
Key::Home => { Key::Home => {
@ -108,8 +118,7 @@ impl View for Text {
} }
} }
fn render(&mut self, cfg: &Config) -> String { fn render(&mut self) -> String {
self.wide = cfg.wide;
let (cols, rows) = self.size; let (cols, rows) = self.size;
let mut out = String::new(); let mut out = String::new();
let longest = if self.longest > MAX_COLS { let longest = if self.longest > MAX_COLS {
@ -124,7 +133,7 @@ impl View for Text {
} else { } else {
String::from("") String::from("")
}; };
let limit = if cfg.mode == ui::Mode::Run { let limit = if self.mode == ui::Mode::Run {
rows - 1 rows - 1
} else { } else {
self.lines self.lines
@ -161,7 +170,7 @@ impl View for Text {
impl Text { impl Text {
/// Create a Text View from a raw Gopher response and a few options. /// Create a Text View from a raw Gopher response and a few options.
pub fn from(url: &str, response: String, tls: bool, tor: bool) -> Text { pub fn from(url: &str, response: String, config: &Config, tls: bool) -> Text {
let mut lines = 0; let mut lines = 0;
let mut longest = 0; let mut longest = 0;
for line in response.split_terminator('\n') { for line in response.split_terminator('\n') {
@ -179,9 +188,10 @@ impl Text {
lines, lines,
longest, longest,
size: (0, 0), size: (0, 0),
mode: config.mode,
tls, tls,
tor, tor: config.tor,
wide: false, wide: config.wide,
} }
} }

@ -251,8 +251,8 @@ impl UI {
}; };
let typ = gopher::type_for_url(&url); let typ = gopher::type_for_url(&url);
match typ { match typ {
Type::Menu | Type::Search => Ok(Box::new(Menu::from(url, res, tls, tor))), Type::Menu | Type::Search => Ok(Box::new(Menu::from(url, res, &self.config, tls))),
Type::Text | Type::HTML => Ok(Box::new(Text::from(url, res, tls, tor))), Type::Text | Type::HTML => Ok(Box::new(Text::from(url, res, &self.config, tls))),
_ => Err(error!("Unsupported Gopher Response: {:?}", typ)), _ => Err(error!("Unsupported Gopher Response: {:?}", typ)),
} }
} }
@ -263,7 +263,7 @@ impl UI {
&url.trim_start_matches("gopher://phetch/") &url.trim_start_matches("gopher://phetch/")
.trim_start_matches("1/"), .trim_start_matches("1/"),
) { ) {
Ok(Box::new(Menu::from(url, source, false, false))) Ok(Box::new(Menu::from(url, source, &self.config, false)))
} else { } else {
Err(error!("phetch URL not found: {}", url)) Err(error!("phetch URL not found: {}", url))
} }
@ -330,7 +330,7 @@ impl UI {
if !self.views.is_empty() && self.focused < self.views.len() { if !self.views.is_empty() && self.focused < self.views.len() {
if let Some(page) = self.views.get_mut(self.focused) { if let Some(page) = self.views.get_mut(self.focused) {
page.term_size(cols as usize, rows as usize); page.term_size(cols as usize, rows as usize);
return Ok(page.render(&self.config)); return Ok(page.render());
} }
} }
Err(error!( Err(error!(
@ -595,7 +595,7 @@ impl UI {
if let Some(page) = self.views.get(self.focused) { if let Some(page) = self.views.get(self.focused) {
let url = page.url(); let url = page.url();
let raw = page.raw().to_string(); let raw = page.raw().to_string();
let mut text = Text::from(url, raw, page.is_tls(), page.is_tor()); let mut text = Text::from(url, raw, &self.config, page.is_tls());
text.wide = true; text.wide = true;
self.add_page(Box::new(text)); self.add_page(Box::new(text));
} }
@ -632,7 +632,11 @@ impl UI {
} }
'w' => { 'w' => {
self.config.wide = !self.config.wide; self.config.wide = !self.config.wide;
self.dirty = true; if let Some(view) = self.views.get_mut(self.focused) {
let w = view.wide();
view.set_wide(!w);
self.dirty = true;
}
} }
'q' => self.running = false, 'q' => self.running = false,
c => return Err(error!("Unknown keypress: {}", c)), c => return Err(error!("Unknown keypress: {}", c)),

@ -1,4 +1,4 @@
use crate::{config::Config, ui}; use crate::ui;
use std::fmt; use std::fmt;
/// Views represent what's on screen, a Gopher Menu/Text/etc item. /// Views represent what's on screen, a Gopher Menu/Text/etc item.
@ -8,7 +8,7 @@ pub trait View: fmt::Display {
fn respond(&mut self, key: ui::Key) -> ui::Action; fn respond(&mut self, key: ui::Key) -> ui::Action;
/// Create a String of the current view's state that can be /// Create a String of the current view's state that can be
/// printed to the screen. /// printed to the screen.
fn render(&mut self, cfg: &Config) -> String; fn render(&mut self) -> String;
/// Was this View's content fetched using TLS? /// Was this View's content fetched using TLS?
fn is_tls(&self) -> bool; fn is_tls(&self) -> bool;
/// Was this View's content fetched over Tor? /// Was this View's content fetched over Tor?
@ -17,6 +17,10 @@ pub trait View: fmt::Display {
fn url(&self) -> &str; fn url(&self) -> &str;
/// The raw Gopher representation of this View. /// The raw Gopher representation of this View.
fn raw(&self) -> &str; fn raw(&self) -> &str;
/// Set wide mode on this view.
fn set_wide(&mut self, wide: bool);
/// In wide mode?
fn wide(&mut self) -> bool;
/// Set the current screen size. /// Set the current screen size.
fn term_size(&mut self, cols: usize, rows: usize); fn term_size(&mut self, cols: usize, rows: usize);
} }

Loading…
Cancel
Save