--print shows all lines

pull/14/head
chris west 4 years ago
parent 7cf9e25c6b
commit ab364c5b8a

@ -1,4 +1,4 @@
use crate::phetchdir;
use crate::{phetchdir, ui};
use std::{
collections::HashMap,
fs::OpenOptions,
@ -39,6 +39,7 @@ pub struct Config {
pub wide: bool,
pub emoji: bool,
pub cursor: bool,
pub mode: ui::Mode, // can't be set in conf file
}
impl Default for Config {
@ -50,6 +51,7 @@ impl Default for Config {
wide: false,
emoji: false,
cursor: true,
mode: ui::Mode::default(),
}
}
}

@ -1,13 +1,9 @@
use phetch::{config, gopher, ui::UI};
use phetch::{
config, gopher,
ui::{Mode, UI},
};
use std::process::exit;
#[derive(PartialEq)]
enum Mode {
Run,
Print,
Raw,
}
fn main() {
exit(run())
}
@ -173,6 +169,7 @@ fn run() -> i32 {
}
let start = cfg.start.clone();
cfg.mode = mode;
let mut ui = UI::new(cfg);
if let Err(e) = ui.open(&start, &start) {
eprintln!("{}", e);

@ -1,4 +1,4 @@
use crate::ui::{Action, Key, View, MAX_COLS, SCROLL_LINES};
use crate::ui::{self, Action, Key, View, MAX_COLS, SCROLL_LINES};
use crate::{
config::Config,
gopher::{self, Type},
@ -13,7 +13,7 @@ pub struct Menu {
pub longest: usize, // size of the longest line
pub raw: String, // raw response
pub input: String, // user's inputted value
pub cursor: bool, // show the cursor? usually true
pub mode: ui::Mode, // interactive or print mode?
pub link: usize, // selected link
pub scroll: usize, // scrolling offset
pub searching: bool, // search mode?
@ -155,9 +155,16 @@ impl Menu {
fn render_lines(&mut self, cfg: &Config) -> String {
self.wide = cfg.wide;
self.cursor = cfg.cursor;
self.mode = cfg.mode;
let mut out = String::new();
let iter = self.lines.iter().skip(self.scroll).take(self.rows() - 1);
let iter = if self.mode == ui::Mode::Run {
// only show as many lines as screen rows minus one
// (status bar is always last line)
self.lines.iter().skip(self.scroll).take(self.rows() - 1)
} else {
// show all lines in print mode
self.lines.iter().skip(self.scroll).take(self.lines.len())
};
let indent = self.indent();
let left_margin = " ".repeat(indent);
@ -167,7 +174,7 @@ impl Menu {
if line.typ == Type::Info {
out.push_str(" ");
} else {
if line.link == self.link && self.cursor {
if line.link == self.link && self.show_cursor() {
out.push_str("\x1b[97;1m*\x1b[0m")
} else {
out.push(' ');
@ -240,7 +247,7 @@ impl Menu {
/// Print this string to draw the cursor on screen.
/// Returns None if no is link selected.
fn draw_cursor(&self) -> Option<String> {
if self.links.is_empty() || !self.cursor {
if self.links.is_empty() || !self.show_cursor() {
return None;
}
let (x, y) = self.screen_coords(self.link)?;
@ -251,6 +258,11 @@ impl Menu {
))
}
/// Should we show the cursor? Not when printing.
fn show_cursor(&self) -> bool {
self.mode == ui::Mode::Run
}
/// User input field.
fn render_input(&self) -> String {
format!("Find: {}{}", self.input, cursor::Show)
@ -807,8 +819,8 @@ impl Menu {
raw,
input: String::new(),
link: 0,
mode: Default::default(),
scroll: 0,
cursor: true,
searching: false,
size: (0, 0),
tls: false,

@ -29,6 +29,22 @@ use termion::{
pub type Key = termion::event::Key;
pub type Page = Box<dyn View>;
/// The mode our text UI is in. Run mode is the default while
/// printing doesn't show the cursor, among other things.
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum Mode {
Run,
Print,
NoTTY,
Raw,
}
impl Default for Mode {
fn default() -> Self {
Mode::Run
}
}
pub const SCROLL_LINES: usize = 15;
pub const MAX_COLS: usize = 77;

Loading…
Cancel
Save