2
0
mirror of https://github.com/xvxx/phetch synced 2024-11-16 21:25:45 +00:00
phetch/src/bookmarks.rs

44 lines
1.1 KiB
Rust
Raw Normal View History

2019-12-23 08:22:08 +00:00
use config;
2019-12-23 19:06:52 +00:00
use std::io::{Read, Result};
2019-12-23 08:36:04 +00:00
2019-12-23 20:44:20 +00:00
// Bookmarks only work if you've created a ~/.config/phetch/ manually.
2019-12-23 08:22:08 +00:00
const BOOKMARKS_FILE: &str = "bookmarks.gph";
2019-12-23 20:44:20 +00:00
macro_rules! dir_missing_fmt {
() => {
"i\r\ni\r
i\r\ni\x1b[91m{error}\x1b[0m\r
2019-12-23 21:56:34 +00:00
i\r\niBookmarks can only be saved if {dir} exists.\r
2019-12-23 20:44:20 +00:00
i\r\niRun this in your terminal to enable bookmarking:\r
i\r\nimkdir -p {dir}"
};
}
2019-12-23 08:22:08 +00:00
pub fn as_raw_menu() -> String {
2019-12-23 20:44:20 +00:00
let path = config::path();
if let Err(e) = path {
return format!(dir_missing_fmt!(), error = e, dir = config::DIR);
}
2019-12-23 21:56:34 +00:00
let mut out = format!("i{}{}:\r\ni\r\n", config::DIR, BOOKMARKS_FILE);
2019-12-23 20:44:20 +00:00
let path = path.unwrap().join(BOOKMARKS_FILE);
if !path.exists() {
2019-12-23 21:56:34 +00:00
out.push_str("iNo bookmarks yet.\r\ni\r\niUse <ctrl-s> to bookmark a page.\r\n");
2019-12-23 20:44:20 +00:00
return out;
}
2019-12-23 08:22:08 +00:00
config::load(BOOKMARKS_FILE)
2019-12-23 08:36:04 +00:00
.and_then(|mut reader| reader.read_to_string(&mut out))
2019-12-23 08:22:08 +00:00
.map_err(|e| {
2019-12-23 08:36:04 +00:00
out = format!("3{}", e);
2019-12-23 08:22:08 +00:00
e
});
2019-12-23 08:36:04 +00:00
out
2019-12-23 08:22:08 +00:00
}
2019-12-23 20:44:20 +00:00
// save a single bookmark entry
2019-12-23 19:06:52 +00:00
pub fn save(label: &str, url: &str) -> Result<()> {
config::append(BOOKMARKS_FILE, label, url)
2019-12-23 08:22:08 +00:00
}