From 05b8e6ef5673c96e3a1bfe903beb039cc3d0d57e Mon Sep 17 00:00:00 2001 From: dvkt Date: Mon, 23 Dec 2019 00:22:08 -0800 Subject: [PATCH] config, bookmarks --- src/bookmarks.rs | 22 ++++++++++++ src/config.rs | 31 +++++++++-------- src/history.rs | 89 +++++++++--------------------------------------- src/lib.rs | 1 + 4 files changed, 55 insertions(+), 88 deletions(-) create mode 100644 src/bookmarks.rs diff --git a/src/bookmarks.rs b/src/bookmarks.rs new file mode 100644 index 0000000..98f94bd --- /dev/null +++ b/src/bookmarks.rs @@ -0,0 +1,22 @@ +use config; +use std::io::{BufRead, BufReader, Result, Write}; + +const BOOKMARKS_FILE: &str = "bookmarks.gph"; + +pub fn as_raw_menu() -> String { + let mut out = vec![format!("i** bookmarks **\r\ni")]; + + config::load(BOOKMARKS_FILE) + .and_then(|reader| reader.read(&mut out)) + .map_err(|e| { + out.push(format!("3{}", e)); + e + }); + + out.join("\r\n") +} + +// save a single history entry +pub fn save(label: &str, url: &str) { + config::append(BOOKMARKS_FILE, label, url); +} diff --git a/src/config.rs b/src/config.rs index 353b2c5..1bc2f28 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,6 @@ -use std::fs::{File, OpenOptions}; -use std::io::{BufRead, BufReader, Result, Write}; use gopher; +use std::fs::{File, OpenOptions}; +use std::io::{BufReader, Result, Write}; pub const DIR: &str = "~/.config/phetch/"; @@ -26,19 +26,20 @@ pub fn append(filename: &str, label: &str, url: &str) { .append(true) .create(true) .open(filename) - { - let (t, host, port, sel) = gopher::parse_url(&url); - file.write_all( - format!( - "{}{}\t{}\t{}\t{}\r\n", - gopher::char_for_type(t).unwrap_or('i'), - label, - sel, - host, - port - ) - .as_ref(), - ); + { + let (t, host, port, sel) = gopher::parse_url(&url); + file.write_all( + format!( + "{}{}\t{}\t{}\t{}\r\n", + gopher::char_for_type(t).unwrap_or('i'), + label, + sel, + host, + port + ) + .as_ref(), + ); + } } } diff --git a/src/history.rs b/src/history.rs index 70efeb2..bdc9970 100644 --- a/src/history.rs +++ b/src/history.rs @@ -1,85 +1,28 @@ -use gopher; -use std::fs::File; -use std::io::{BufRead, BufReader, Result, Write}; +use config; +use std::io::BufRead; -const CONFIG_DIR: &str = "~/.config/phetch/"; const HISTORY_FILE: &str = "history.gph"; -/// History file is saved in ~/.config/phetch/history.gph if the phetch -/// config directory exists. -pub fn load_as_raw_menu() -> String { - let mut out = vec![]; +pub fn as_raw_menu() -> String { + let mut out = vec![format!("i{}{}:\r\ni", config::DIR, HISTORY_FILE)]; - match load() { - Ok(reader) => { - let mut lines = reader.lines(); + config::load(HISTORY_FILE) + .and_then(|reader| { + let lines = reader.lines(); while let Some(Ok(line)) = lines.next() { - out.insert(0, line); // reverse order + out.insert(1, line); } - } - Err(e) => out.push(format!("3{}", e)), - } + Ok(()) + }) + .map_err(|e| { + out.push(format!("3{}", e)); + e + }); - out.insert(0, format!("i{}{}:\r\ni", CONFIG_DIR, HISTORY_FILE)); out.join("\r\n") } -pub fn load() -> Result> { - let dotdir = config_dir_path(); - if dotdir.is_none() { - return Err(error!("{} directory doesn't exist", CONFIG_DIR)); - } - let history = dotdir.unwrap().join(HISTORY_FILE); - if let Ok(file) = std::fs::OpenOptions::new().read(true).open(&history) { - return Ok(BufReader::new(file)); - } - Err(error!("Couldn't open {:?}", history)) -} - // save a single history entry -pub fn save(url: &str, label: &str) { - let dotdir = config_dir_path(); - if dotdir.is_none() { - return; - } - let dotdir = dotdir.unwrap(); - let history = dotdir.join(HISTORY_FILE); - if let Ok(mut file) = std::fs::OpenOptions::new() - .append(true) - .create(true) - .open(history) - { - let (t, host, port, sel) = gopher::parse_url(&url); - // ignore internal URLs - if host == "help" { - return; - } - file.write_all( - format!( - "{}{}\t{}\t{}\t{}\r\n", - gopher::char_for_type(t).unwrap_or('i'), - label, - sel, - host, - port - ) - .as_ref(), - ); - } -} - -// Returns None if the config dir doesn't exist. -pub fn config_dir_path() -> Option { - let homevar = std::env::var("HOME"); - if homevar.is_err() { - return None; - } - - let dotdir = CONFIG_DIR.replace('~', &homevar.unwrap()); - let dotdir = std::path::Path::new(&dotdir); - if dotdir.exists() { - Some(std::path::PathBuf::from(dotdir)) - } else { - None - } +pub fn save(label: &str, url: &str) { + config::append(HISTORY_FILE, label, url); } diff --git a/src/lib.rs b/src/lib.rs index 86b9f34..f4c2ffc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,7 @@ extern crate termion; #[macro_use] pub mod gopher; +pub mod bookmarks; pub mod help; pub mod history; pub mod menu;