You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
phetch/src/menu.rs

365 lines
10 KiB
Rust

use gopher::Type;
5 years ago
use std::io::stdout;
use std::io::Write;
5 years ago
use ui::{Action, Key, View};
pub struct MenuView {
5 years ago
pub input: String, // user's inputted value
pub menu: Menu, // data
5 years ago
pub link: usize, // selected link
5 years ago
pub scroll: usize, // scrolling offset
5 years ago
}
pub struct Menu {
5 years ago
url: String, // gopher url
5 years ago
lines: Vec<Line>, // lines
longest: usize, // size of the longest line
5 years ago
}
5 years ago
5 years ago
#[derive(Debug)]
5 years ago
pub struct Line {
5 years ago
name: String,
5 years ago
url: String,
5 years ago
typ: Type,
5 years ago
link: usize, // link #, if any
5 years ago
}
impl View for MenuView {
5 years ago
fn render(&self, cols: usize, rows: usize) -> String {
5 years ago
self.render_lines(cols, rows)
5 years ago
}
5 years ago
fn process_input(&mut self, key: Key) -> Action {
5 years ago
self.process_key(key)
5 years ago
}
5 years ago
fn url(&self) -> String {
self.menu.url.to_string()
}
5 years ago
}
impl MenuView {
pub fn from(url: String, response: String) -> MenuView {
MenuView {
menu: Menu::from(url, response),
input: String::new(),
5 years ago
link: 0,
5 years ago
scroll: 0,
}
}
5 years ago
fn lines(&self) -> &Vec<Line> {
&self.menu.lines
}
5 years ago
fn links(&self) -> impl Iterator<Item = &Line> {
self.menu.links()
}
5 years ago
fn render_lines(&self, cols: usize, rows: usize) -> String {
5 years ago
let mut out = String::new();
macro_rules! push {
($c:expr, $e:expr) => {{
5 years ago
out.push_str("\x1b[");
out.push_str($c);
out.push_str("m");
out.push_str($e);
out.push_str("\x1b[0m");
5 years ago
}};
}
5 years ago
let iter = self.lines().iter().skip(self.scroll).take(rows - 1);
5 years ago
5 years ago
let indent = if self.menu.longest > cols {
String::from("")
} else {
5 years ago
let left = (cols - self.menu.longest) / 2;
5 years ago
if left > 6 {
" ".repeat(left - 6)
} else {
String::from("")
}
};
5 years ago
for line in iter {
out.push_str(&indent);
5 years ago
if line.typ == Type::Info {
5 years ago
out.push_str(" ");
5 years ago
} else {
5 years ago
if line.link - 1 == self.link {
out.push_str("\x1b[97;1m*\x1b[0m")
5 years ago
} else {
out.push(' ');
}
5 years ago
out.push(' ');
out.push_str("\x1b[95m");
5 years ago
if line.link < 10 {
5 years ago
out.push(' ');
}
5 years ago
out.push_str(&line.link.to_string());
5 years ago
out.push_str(".\x1b[0m ");
}
5 years ago
// truncate long lines, instead of wrapping
5 years ago
let name = if line.name.len() + 6 > cols {
&line.name[0..cols - 6]
5 years ago
} else {
&line.name
};
5 years ago
match line.typ {
5 years ago
Type::Text => push!("96", name),
Type::Menu => push!("94", name),
Type::Info => push!("93", name),
Type::HTML => push!("92", name),
Type::Error => push!("91", name),
typ if typ.is_download() => push!("4;97", name),
_ => push!("0", name),
5 years ago
}
out.push('\n');
5 years ago
}
5 years ago
out.push_str(&self.input);
5 years ago
out
5 years ago
}
5 years ago
fn redraw_input(&self) -> Action {
5 years ago
print!("\r\x1b[K{}", self.input);
stdout().flush();
5 years ago
Action::None
}
5 years ago
fn action_page_down(&mut self) -> Action {
5 years ago
if self.scroll < self.lines().len() - 15 {
5 years ago
self.scroll += 15;
Action::Redraw
} else {
Action::None
}
}
fn action_page_up(&mut self) -> Action {
if self.scroll > 0 {
self.scroll -= 15;
if self.scroll < 0 {
self.scroll = 0;
}
Action::Redraw
} else {
Action::None
}
}
5 years ago
fn action_up(&mut self) -> Action {
if self.link > 0 {
self.link -= 1;
Action::Redraw
} else {
Action::None
}
}
fn action_down(&mut self) -> Action {
5 years ago
let count = self.links().count();
if count > 0 && self.link < count - 1 {
5 years ago
self.link += 1;
Action::Redraw
} else {
Action::None
}
}
5 years ago
5 years ago
fn action_select_link(&mut self, line: usize) -> Action {
if line < self.links().count() {
self.link = line;
Action::Redraw
} else {
Action::None
}
}
5 years ago
fn action_follow_link(&mut self, link: usize) -> Action {
self.input.clear();
if let Some(line) = self.links().nth(link) {
let url = line.url.to_string();
Action::Open(url)
5 years ago
} else {
Action::None
}
}
5 years ago
fn process_key(&mut self, key: Key) -> Action {
5 years ago
match key {
5 years ago
Key::Char('\n') => {
5 years ago
self.input.clear();
if let Some(line) = self.links().nth(self.link) {
5 years ago
let url = line.url.to_string();
Action::Open(url)
5 years ago
} else {
Action::None
}
}
5 years ago
Key::Up | Key::Ctrl('p') | Key::Ctrl('k') => self.action_up(),
Key::Down | Key::Ctrl('n') | Key::Ctrl('j') => self.action_down(),
5 years ago
Key::Backspace | Key::Delete => {
5 years ago
if self.input.is_empty() {
Action::Back
} else {
5 years ago
self.input.pop();
self.redraw_input()
5 years ago
}
}
Key::Ctrl('c') => {
if self.input.len() > 0 {
self.input.clear();
5 years ago
self.redraw_input()
5 years ago
} else {
Action::Quit
}
}
5 years ago
Key::PageUp | Key::Char('-') => {
5 years ago
if self.input.is_empty() {
5 years ago
self.action_page_up()
5 years ago
} else {
self.input.push('-');
5 years ago
self.redraw_input()
5 years ago
}
}
5 years ago
Key::PageDown | Key::Char(' ') => {
5 years ago
if self.input.is_empty() {
5 years ago
self.action_page_down()
5 years ago
} else {
self.input.push(' ');
5 years ago
self.redraw_input()
5 years ago
}
}
Key::Char(c) => {
self.input.push(c);
5 years ago
let count = self.links().count();
5 years ago
let input = &self.input;
5 years ago
for i in 0..count {
5 years ago
// jump to number
5 years ago
for z in 1..=9 {
5 years ago
if count < (z * 10) && c == to_char(z as u32) && i == z - 1 {
return self.action_follow_link(i);
}
}
if input.len() > 1 && input == &(i + 1).to_string() {
5 years ago
return self.action_select_link(i);
5 years ago
} else {
5 years ago
let name = if let Some(link) = self.links().nth(i) {
link.name.to_ascii_lowercase()
} else {
"".to_string()
};
5 years ago
5 years ago
if name.contains(&self.input.to_ascii_lowercase()) {
5 years ago
return self.action_select_link(i);
5 years ago
}
}
}
5 years ago
self.action_select_link(0)
5 years ago
}
5 years ago
_ => Action::Unknown,
5 years ago
}
}
}
impl Menu {
pub fn from(url: String, gopher_response: String) -> Menu {
Self::parse(url, gopher_response)
5 years ago
}
5 years ago
pub fn links(&self) -> impl Iterator<Item = &Line> {
self.lines.iter().filter(|&line| line.link > 0)
}
fn parse(url: String, raw: String) -> Menu {
5 years ago
let mut lines = vec![];
5 years ago
let mut link = 0;
let mut longest = 0;
for line in raw.split_terminator("\n") {
if let Some(c) = line.chars().nth(0) {
let typ = match c {
'0' => Type::Text,
'1' => Type::Menu,
5 years ago
'2' => Type::CSOEntity,
'3' => Type::Error,
5 years ago
'4' => Type::Binhex,
'5' => Type::DOSFile,
'6' => Type::UUEncoded,
'7' => Type::Search,
'8' => Type::Telnet,
'9' => Type::Binary,
'+' => Type::Mirror,
'g' => Type::GIF,
'T' => Type::Telnet3270,
'h' => Type::HTML,
'i' => Type::Info,
5 years ago
's' => Type::Sound,
'd' => Type::Document,
5 years ago
'.' => continue,
5 years ago
'\n' => continue,
5 years ago
_ => continue,
};
5 years ago
// build string URL
let parts: Vec<&str> = line.split_terminator("\t").collect();
5 years ago
let mut url = String::from("gopher://");
if parts.len() > 2 {
url.push_str(parts[2]); // host
}
if parts.len() > 3 {
// port
let port = parts[3].trim_end_matches('\r');
if port != "70" {
url.push(':');
url.push_str(parts[3].trim_end_matches('\r'));
}
5 years ago
}
5 years ago
5 years ago
// auto-prepend gopher type to selector
5 years ago
if let Some(first_char) = parts[0].chars().nth(0) {
5 years ago
if first_char == '0' || first_char == '1' || first_char == 'h' {
5 years ago
url.push_str("/");
url.push(first_char);
}
}
5 years ago
if parts.len() > 1 {
url.push_str(parts[1]); // selector
}
let name = parts[0][1..].to_string();
5 years ago
if typ != Type::Info {
link += 1;
}
5 years ago
let link = if typ == Type::Info { 0 } else { link };
5 years ago
if name.len() > longest {
longest = name.len();
}
5 years ago
lines.push(Line {
name,
url,
typ,
5 years ago
link,
5 years ago
});
5 years ago
}
}
Menu {
url,
lines,
longest,
}
5 years ago
}
5 years ago
}
5 years ago
// number -> char of that number
fn to_char(c: u32) -> char {
if let Some(ch) = std::char::from_digit(c, 10) {
ch
} else {
'0'
}
}