err when trying to save bookmark

pull/6/head
dvkt 5 years ago
parent 76d9396c90
commit 5f875d9920

@ -1,6 +1,6 @@
use config; use config;
use std::io::Read; use std::io::{Read, Result};
const BOOKMARKS_FILE: &str = "bookmarks.gph"; const BOOKMARKS_FILE: &str = "bookmarks.gph";
@ -17,6 +17,6 @@ pub fn as_raw_menu() -> String {
} }
// save a single history entry // save a single history entry
pub fn save(label: &str, url: &str) { pub fn save(label: &str, url: &str) -> Result<()> {
config::append(BOOKMARKS_FILE, label, url); config::append(BOOKMARKS_FILE, label, url)
} }

@ -6,26 +6,24 @@ 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>> { pub fn load(filename: &str) -> Result<BufReader<File>> {
if let Some(dotdir) = path() { path().and_then(|dotdir| {
let path = dotdir.join(filename); let path = dotdir.join(filename);
if let Ok(file) = OpenOptions::new().read(true).open(&path) { if let Ok(file) = OpenOptions::new().read(true).open(&path) {
Ok(BufReader::new(file)) Ok(BufReader::new(file))
} else { } else {
Err(error!("Couldn't open {:?}", path)) Err(error!("Couldn't open {:?}", path))
} }
} else { })
Err(error!("{} directory doesn't exist", DIR))
}
} }
// 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) { pub fn append(filename: &str, label: &str, url: &str) -> Result<()> {
if let Some(dotdir) = path() { path().and_then(|dotdir| {
let filename = dotdir.join(filename); let path = dotdir.join(filename);
if let Ok(mut file) = std::fs::OpenOptions::new() if let Ok(mut file) = std::fs::OpenOptions::new()
.append(true) .append(true)
.create(true) .create(true)
.open(filename) .open(path)
{ {
let (t, host, port, sel) = gopher::parse_url(&url); let (t, host, port, sel) = gopher::parse_url(&url);
file.write_all( file.write_all(
@ -39,23 +37,26 @@ pub fn append(filename: &str, label: &str, url: &str) {
) )
.as_ref(), .as_ref(),
); );
Ok(())
} else {
Err(error!("Can't open file for writing: {:?}", filename))
} }
} })
} }
// PathBuf to config dir if it exists. // PathBuf to config dir if it exists.
// None if the config dir doesn't exist. // None if the config dir doesn't exist.
pub fn path() -> Option<std::path::PathBuf> { pub fn path() -> Result<std::path::PathBuf> {
let homevar = std::env::var("HOME"); let homevar = std::env::var("HOME");
if homevar.is_err() { if homevar.is_err() {
return None; return Err(error!("$HOME not set, can't decode `~`"));
} }
let dotdir = DIR.replace('~', &homevar.unwrap()); let dotdir = DIR.replace('~', &homevar.unwrap());
let dotdir = std::path::Path::new(&dotdir); let dotdir = std::path::Path::new(&dotdir);
if dotdir.exists() { if dotdir.exists() {
Some(std::path::PathBuf::from(dotdir)) Ok(std::path::PathBuf::from(dotdir))
} else { } else {
None Err(error!("Config dir not found: {}", DIR))
} }
} }

@ -1,5 +1,5 @@
use config; use config;
use std::io::BufRead; use std::io::{BufRead, Result};
const HISTORY_FILE: &str = "history.gph"; const HISTORY_FILE: &str = "history.gph";
@ -23,6 +23,6 @@ pub fn as_raw_menu() -> String {
} }
// save a single history entry // save a single history entry
pub fn save(label: &str, url: &str) { pub fn save(label: &str, url: &str) -> Result<()> {
config::append(HISTORY_FILE, label, url); config::append(HISTORY_FILE, label, url)
} }

@ -470,7 +470,7 @@ impl Menu {
self.input.clear(); self.input.clear();
if let Some(line) = self.link(self.link) { if let Some(line) = self.link(self.link) {
let url = line.url.to_string(); let url = line.url.to_string();
let (typ, _, _, _) = gopher::parse_url(&url); let (typ, host, _, _) = gopher::parse_url(&url);
match typ { match typ {
Type::Search => { Type::Search => {
if let Some(query) = ui::prompt(&format!("{}> ", line.name)) { if let Some(query) = ui::prompt(&format!("{}> ", line.name)) {
@ -482,10 +482,15 @@ impl Menu {
Type::Error => Action::Error(line.name.to_string()), Type::Error => Action::Error(line.name.to_string()),
Type::Telnet => Action::Error("Telnet support coming soon".into()), Type::Telnet => Action::Error("Telnet support coming soon".into()),
_ => { _ => {
// don't record internal urls
if host != "help" {
let hurl = url.to_string(); let hurl = url.to_string();
let hname = line.name.clone(); let hname = line.name.clone();
thread::spawn(move || history::save(&hurl, &hname)); thread::spawn(move || history::save(&hurl, &hname));
Action::Open(url) Action::Open(url)
} else {
Action::None
}
} }
} }
} else { } else {

@ -328,8 +328,10 @@ impl UI {
Action::Keypress(Key::Ctrl('s')) => { Action::Keypress(Key::Ctrl('s')) => {
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();
self.set_status(format!("Saved bookmark: {}", url)); match bookmarks::save(&url, &url) {
bookmarks::save(&url, &url); Ok(()) => self.set_status(format!("Saved bookmark: {}", url)),
Err(e) => return Err(error!("Save failed: {}", e)),
}
} }
} }
Action::Keypress(Key::Ctrl('u')) => { Action::Keypress(Key::Ctrl('u')) => {

Loading…
Cancel
Save