summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Tay <sam.chong.tay@gmail.com>2020-06-08 12:27:13 -0700
committerSam Tay <sam.chong.tay@gmail.com>2020-06-08 12:27:13 -0700
commit3805b4ef067dbab23c4d9116bb46892f661657c9 (patch)
tree0c88d3686c417a5df9b4edb10003a38e2ee8885c
parent74196d6e4fbed6a1157ac29afcfe14728ecef03c (diff)
Use markdown in default error messages
-rw-r--r--TODO.md2
-rw-r--r--roadmap.md1
-rw-r--r--src/error.rs11
-rw-r--r--src/main.rs19
-rw-r--r--src/term.rs (renamed from src/macros.rs)22
5 files changed, 43 insertions, 12 deletions
diff --git a/TODO.md b/TODO.md
index 60ab5f7..c993fe7 100644
--- a/TODO.md
+++ b/TODO.md
@@ -1,7 +1,7 @@
# TODO
### v0.1.1
-1. Touch up CLI and all that
+1. Touch up CLI and all that, finish all TODOs in cli.rs
### v0.2.0
0. Termimad interface for viewing questions and answers
diff --git a/roadmap.md b/roadmap.md
index 0bd6a85..094bd2c 100644
--- a/roadmap.md
+++ b/roadmap.md
@@ -25,6 +25,7 @@
[ ] Add duckduckgo scraper
### at some point
+[ ] clean up error.rs and term.rs ; only keep whats actually ergonomic
[ ] ask legal@stackoverflow.com for permission to logo stackoverflow/stackexchange in readme
[ ] add duckduckgo logo to readme
[ ] cross-platform release binaries
diff --git a/src/error.rs b/src/error.rs
index b2afd60..9563359 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -2,11 +2,14 @@ use std::path::PathBuf;
pub type Result<T, E = Error> = std::result::Result<T, E>;
+#[derive(Debug)]
pub struct Error {
#[allow(dead_code)]
pub kind: ErrorKind,
pub error: String,
}
+
+#[derive(Debug)]
pub enum ErrorKind {
Malformed,
StackExchange,
@@ -41,7 +44,7 @@ impl Error {
pub fn malformed(path: &PathBuf) -> Self {
Error {
kind: ErrorKind::Malformed,
- error: format!("File {} is malformed; try removing it.", path.display()),
+ error: format!("File `{}` is malformed; try removing it.", path.display()),
}
}
pub fn se(err: String) -> Self {
@@ -54,7 +57,7 @@ impl Error {
Error {
kind: ErrorKind::Permissions,
error: format!(
- "Couldn't create directory {}; please check the permissions
+ "Couldn't create directory `{}`; please check the permissions
on the parent directory",
path.display()
),
@@ -64,7 +67,7 @@ impl Error {
Error {
kind: ErrorKind::Permissions,
error: format!(
- "Couldn't create file {}; please check the directory permissions",
+ "Couldn't create file `{}`; please check the directory permissions",
path.display()
),
}
@@ -73,7 +76,7 @@ impl Error {
Error {
kind: ErrorKind::Permissions,
error: format!(
- "Couldn't write to file {}; please check its permissions",
+ "Couldn't write to file `{}`; please check its permissions",
path.display()
),
}
diff --git a/src/main.rs b/src/main.rs
index cad09a7..8ce16cc 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,26 +1,27 @@
mod cli;
mod config;
mod error;
-mod macros;
mod stackexchange;
+mod term;
use crossterm::style::Color;
use error::{Error, ErrorKind};
use lazy_static::lazy_static;
use minimad::mad_inline;
use stackexchange::{LocalStorage, StackExchange};
+use term::with_error_style;
use termimad::{CompoundStyle, MadSkin};
fn main() {
+ let mut skin = MadSkin::default();
+ // TODO style configuration
+ skin.inline_code = CompoundStyle::with_fg(Color::Cyan);
+ skin.code_block.set_fgbg(Color::Cyan, termimad::gray(20));
(|| {
let opts = cli::get_opts()?;
let config = opts.config;
let site = &config.site;
let mut ls = LocalStorage::new()?;
- // TODO style configuration
- let mut skin = MadSkin::default();
- skin.inline_code = CompoundStyle::with_fg(Color::Cyan);
- skin.code_block.set_fgbg(Color::Cyan, termimad::gray(20));
if opts.update_sites {
ls.update_sites()?;
@@ -94,9 +95,13 @@ fn main() {
Ok(())
})()
- .or_else(|e| print_error!(MadSkin::default(), "{}", &e.error))
+ .or_else(|e| {
+ with_error_style(&mut skin, |err_skin, stderr| {
+ err_skin.write_text_on(stderr, &e.error)
+ })
+ })
.unwrap_or_else(|e| {
- println!("{}", e.error);
+ println!("panic! {}", e.error);
});
}
diff --git a/src/macros.rs b/src/term.rs
index 40c37cf..a2c9433 100644
--- a/src/macros.rs
+++ b/src/term.rs
@@ -1,3 +1,25 @@
+use crate::error::{Error, Result};
+use crossterm::style::Color;
+use lazy_static::lazy_static;
+use minimad::mad_inline;
+use std::io::Stderr;
+use termimad::{mad_write_inline, MadSkin};
+
+pub fn with_error_style<R, F>(skin: &mut MadSkin, f: F) -> Result<R>
+where
+ F: FnOnce(&MadSkin, &mut Stderr) -> Result<R, termimad::Error>,
+{
+ (|| {
+ let err = &mut std::io::stderr();
+ let p = skin.paragraph.clone();
+ skin.paragraph.set_fg(Color::Red);
+ mad_write_inline!(err, skin, "✖ ")?;
+ let r: R = f(&skin, err)?;
+ skin.paragraph = p;
+ Ok::<R, termimad::Error>(r)
+ })()
+ .map_err(Error::from)
+}
#[macro_export]
macro_rules! print_error {
($skin: expr, $md: literal $(, $value: expr )* $(,)? ) => {{