pull/14/head
dvkt 5 years ago
parent cb1efced5f
commit d0dfeae29b

@ -14,6 +14,7 @@ i\r\nimkdir -p {dir}"
};
}
/// Get all bookmarks in Gophermap format.
pub fn as_raw_menu() -> String {
let path = config::path();
if let Err(e) = path {
@ -37,7 +38,7 @@ pub fn as_raw_menu() -> String {
out
}
// save a single bookmark entry
/// Save a single bookmark entry.
pub fn save(label: &str, url: &str) -> Result<()> {
config::append(
BOOKMARKS_FILE,

@ -6,7 +6,7 @@ use std::{
pub const DIR: &str = "~/.config/phetch/";
// Loads a file in the config directory for reading.
/// Loads a file in the config directory for reading.
pub fn load(filename: &str) -> Result<BufReader<File>> {
path().and_then(|dotdir| {
let path = dotdir.join(filename);
@ -18,7 +18,7 @@ pub fn load(filename: &str) -> Result<BufReader<File>> {
})
}
// Append a menu item as a line to a file in the config dir.
/// Append a menu item as a line to a file in the config dir.
pub fn append(filename: &str, label: &str, url: &str) -> Result<()> {
path().and_then(|dotdir| {
let path = dotdir.join(filename);
@ -42,7 +42,7 @@ pub fn append(filename: &str, label: &str, url: &str) -> Result<()> {
})
}
// Add a menu item as the first line in a file in the config dir.
/// Add a menu item as the first line in a file in the config dir.
pub fn prepend(filename: &str, label: &str, url: &str) -> Result<()> {
path().and_then(|dotdir| {
let path = dotdir.join(filename);
@ -75,8 +75,8 @@ pub fn prepend(filename: &str, label: &str, url: &str) -> Result<()> {
})
}
// PathBuf to expanded config dir if it exists.
// None if the config dir doesn't exist.
/// PathBuf to expanded config dir if it exists.
/// None if the config dir doesn't exist.
pub fn path() -> Result<std::path::PathBuf> {
let homevar = std::env::var("HOME");
if homevar.is_err() {

@ -10,18 +10,19 @@ use termion::input::TermRead;
mod r#type;
pub use self::r#type::Type;
// Some Gopher servers can be kind of slow, we may want to up this or
// make it configurable eventually.
/// Some Gopher servers can be kind of slow, we may want to up this or
/// make it configurable eventually.
pub const TCP_TIMEOUT_IN_SECS: u64 = 8;
pub const TCP_TIMEOUT_DURATION: Duration = Duration::from_secs(TCP_TIMEOUT_IN_SECS);
// Fetches a gopher URL and returns a raw Gopher response.
/// Fetches a gopher URL and returns a raw Gopher response.
pub fn fetch_url(url: &str) -> Result<String> {
let (_, host, port, sel) = parse_url(url);
fetch(host, port, sel)
}
// Fetches a gopher URL by its component parts and returns a raw Gopher response.
/// Fetches a gopher URL by its comeponent parts and returns a raw
/// Gopher response.
pub fn fetch(host: &str, port: &str, selector: &str) -> Result<String> {
get(host, port, selector).and_then(|mut stream| {
let mut body = String::new();
@ -30,8 +31,8 @@ pub fn fetch(host: &str, port: &str, selector: &str) -> Result<String> {
})
}
// Downloads a binary to disk. Allows canceling with Ctrl-c.
// Returns the path it was saved to and the size in bytes.
/// Downloads a binary to disk. Allows canceling with Ctrl-c.
/// Returns the path it was saved to and the size in bytes.
pub fn download_url(url: &str) -> Result<(String, usize)> {
let (_, host, port, sel) = parse_url(url);
let filename = sel
@ -68,7 +69,7 @@ pub fn download_url(url: &str) -> Result<(String, usize)> {
})
}
// Make a Gopher request and return a TcpStream ready to be read()'d.
/// Make a Gopher request and return a TcpStream ready to be read()'d.
pub fn get(host: &str, port: &str, selector: &str) -> Result<TcpStream> {
let selector = selector.replace('?', "\t"); // search queries
format!("{}:{}", host, port)
@ -82,8 +83,8 @@ pub fn get(host: &str, port: &str, selector: &str) -> Result<TcpStream> {
})
}
// Parses gopher URL into parts.
// Return (Type, host, port, sel)
/// Parses gopher URL into parts.
/// Returns (Type, host, port, sel)
pub fn parse_url(url: &str) -> (Type, &str, &str, &str) {
let url = url.trim_start_matches("gopher://");

@ -1,5 +1,6 @@
use crate::{bookmarks, history};
/// Find a help file/page. If found, gives the raw Gophermap.
pub fn lookup(name: &str) -> Option<String> {
Some(match name {
"" | "/" | "home" | "home/" => format!("{}{}", HEADER, START),
@ -21,7 +22,7 @@ pub fn lookup(name: &str) -> Option<String> {
})
}
pub const HEADER: &str = "
const HEADER: &str = "
i
i / / /
i ___ (___ ___ (___ ___ (___
@ -31,7 +32,7 @@ i|
i
";
pub const START: &str = "
const START: &str = "
i ~ * ~
i
7search gopher /v2/vs gopher.floodgap.com
@ -47,7 +48,7 @@ i
i
";
pub const HELP: &str = "
const HELP: &str = "
i ** help topics **
i
1keyboard shortcuts /help/keys phetch
@ -64,7 +65,7 @@ i
i
";
pub const KEYS: &str = "
const KEYS: &str = "
i ** keyboard shortcuts **
i
ileft back in history
@ -101,7 +102,7 @@ iwork with the ctrl key.
i
";
pub const NAV: &str = "
const NAV: &str = "
i ** menu navigation **
i
ithere are three ways to
@ -137,7 +138,7 @@ ior ctrl-p/n to cycle matches.
i
";
pub const BOOKMARKS: &str = "
const BOOKMARKS: &str = "
i ** bookmarks **
i
iphetch has two ways to save
@ -158,7 +159,7 @@ i- `pbcopy` on macos
i- `xclip -sel clip` on linux
i";
pub const HISTORY: &str = "
const HISTORY: &str = "
i ** history **
i
iif you create a history.gph
@ -177,7 +178,7 @@ ifile directly, or share it
iwith your friends!
";
pub const TYPES: &str = "
const TYPES: &str = "
i ** gopher types **
i
iphetch supports these links:
@ -208,7 +209,7 @@ TTelnet3270 /help/types phetch
i
";
pub const ABOUT: &str = "
const ABOUT: &str = "
i ~ version: v{version} ~
i
ispecial thanks to:

@ -1,7 +1,7 @@
use crate::config;
use std::io::{BufRead, Result};
// History only works if you've created ~/.config/phetch/history.gph manually.
/// History only works if you've created ~/.config/phetch/history.gph manually.
const HISTORY_FILE: &str = "history.gph";
macro_rules! file_missing_fmt {
@ -14,6 +14,7 @@ i\r\nimkdir -p {dir} && touch {file}"
};
}
/// Returns history as a Gophermap.
pub fn as_raw_menu() -> String {
let homepath = format!("{}{}", config::DIR, HISTORY_FILE);
let path = config::path();
@ -56,7 +57,7 @@ pub fn as_raw_menu() -> String {
out.join("\r\n")
}
// save a single history entry if the history file exists
/// Save a single history entry if the history file exists.
pub fn save(label: &str, url: &str) -> Result<()> {
if let Err(e) = config::path() {
return Err(error!("History file doesn't exist: {}", e));

@ -24,7 +24,7 @@ pub struct Line {
pub link: usize, // link #, if any
}
// direction of a given link relative to the visible screen
/// Direction of a given link relative to the visible screen.
#[derive(PartialEq)]
enum LinkPos {
Above,
@ -411,13 +411,13 @@ impl Menu {
}
}
// search through links to find a match based on the pattern,
// starting at link position `start`. returns the link position.
/// Search through links to find a match based on the pattern,
/// starting at link position `start`. returns the link position.
fn link_matching(&self, start: usize, pattern: &str) -> Option<usize> {
self.link_match_with_iter(pattern, &mut self.links.iter().skip(start))
}
// search backwards
/// Search backwards through all links.
fn rlink_matching(&self, start: usize, pattern: &str) -> Option<usize> {
self.link_match_with_iter(pattern, &mut self.links.iter().take(start).rev())
}
@ -535,11 +535,13 @@ impl Menu {
}
}
/// Select and open link.
fn action_follow_link(&mut self, link: usize) -> Action {
self.action_select_link(link);
self.action_open()
}
/// Scroll to a link if it's not visible.
fn scroll_to(&mut self, link: usize) -> Action {
if !self.is_visible(link) {
if let Some(&pos) = self.links.get(link) {
@ -557,6 +559,7 @@ impl Menu {
Action::None
}
/// Open the currently selected link.
fn action_open(&mut self) -> Action {
// if the selected link isn't visible, jump to it:
if !self.is_visible(self.link) {
@ -592,7 +595,7 @@ impl Menu {
}
}
// self.searching == true
/// self.searching == true
fn process_search_mode_char(&mut self, c: char) -> Action {
if c == '\n' {
if self.link_matching(0, &self.input).is_some() {
@ -613,6 +616,7 @@ impl Menu {
}
}
/// Respond to user input.
fn process_key(&mut self, key: Key) -> Action {
if self.searching {
if let Key::Char(c) = key {
@ -699,7 +703,7 @@ impl Menu {
}
}
// parse gopher response into a Menu object
/// Parse gopher response into a Menu object.
pub fn parse(url: String, raw: String) -> Menu {
let mut lines = vec![];
let mut links = vec![];

@ -180,7 +180,7 @@ impl UI {
}
}
// get Menu for on-line help, home page, etc, ex: gopher://home/1/help/types
/// Get Menu for on-line help, home page, etc, ex: gopher://home/1/help/types
fn fetch_internal(&mut self, url: &str) -> Result<Page> {
if let Some(source) = help::lookup(
&url.trim_start_matches("gopher://phetch/")
@ -208,8 +208,8 @@ impl UI {
self.size = (cols, rows);
}
// Show a spinner while running a thread. Used to make gopher requests or
// download files.
/// Show a spinner while running a thread. Used to make gopher requests or
/// download files.
fn spinner<T: Send + 'static, F: 'static + Send + FnOnce() -> T>(
&mut self,
label: &str,
@ -294,7 +294,7 @@ impl UI {
}
}
// Ask user to confirm action with ENTER or Y.
/// Ask user to confirm action with ENTER or Y.
fn confirm(&self, question: &str) -> bool {
let rows = self.rows();
@ -321,7 +321,7 @@ impl UI {
}
}
// Prompt user for input and return what was entered, if anything.
/// Prompt user for input and return what was entered, if anything.
fn prompt(&self, prompt: &str) -> Option<String> {
let rows = self.rows();

Loading…
Cancel
Save