summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Tay <sam.chong.tay@gmail.com>2020-06-07 18:28:10 -0700
committerSam Tay <sam.chong.tay@gmail.com>2020-06-07 18:36:39 -0700
commit1bf39e9128b67e234aa0306ee19b41342bd8c4e1 (patch)
tree8bed8569254df81e73359711fb3900944dc3f431
parent556612119a77d8235904819a2f17a8ed82ca304c (diff)
Leverage termimad to print sites in a table
And scrap all the crossterm wrappers in term.rs
-rw-r--r--Cargo.lock12
-rw-r--r--src/macros.rs66
-rw-r--r--src/main.rs42
-rw-r--r--src/stackexchange.rs1
-rw-r--r--src/term.rs73
5 files changed, 69 insertions, 125 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 838d441..cd50817 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -838,17 +838,6 @@ dependencies = [
]
[[package]]
-name = "pulldown-cmark"
-version = "0.7.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "3e142c3b8f49d2200605ee6ba0b1d757310e9e7a72afe78c36ee2ef67300ee00"
-dependencies = [
- "bitflags",
- "memchr",
- "unicase",
-]
-
-[[package]]
name = "quote"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1113,7 +1102,6 @@ dependencies = [
"flate2",
"lazy_static",
"minimad",
- "pulldown-cmark",
"reqwest",
"serde",
"serde_json",
diff --git a/src/macros.rs b/src/macros.rs
index 2b1de62..40c37cf 100644
--- a/src/macros.rs
+++ b/src/macros.rs
@@ -1,20 +1,4 @@
#[macro_export]
-macro_rules! printerr {
- ($($arg:tt)*) => ({
- use std::io::{Write, stderr};
- use crossterm::{execute};
- use crossterm::style::{Print, SetForegroundColor, ResetColor, Color};
- execute!(
- stderr(),
- SetForegroundColor(Color::Red),
- Print("✖ ".to_string()),
- Print($($arg)*.to_string()),
- ResetColor
- ).ok();
- })
-}
-
-#[macro_export]
macro_rules! print_error {
($skin: expr, $md: literal $(, $value: expr )* $(,)? ) => {{
use lazy_static::lazy_static;
@@ -39,7 +23,55 @@ macro_rules! print_notice {
let err = &mut std::io::stderr();
let p = $skin.paragraph.clone();
$skin.paragraph.set_fg(crossterm::style::Color::Yellow);
- termimad::mad_write_inline!(err, $skin, "✖ ").map_err(Error::from)?;
+ termimad::mad_write_inline!(err, $skin, "➜ ").map_err(Error::from)?;
+ $skin.write_composite(err, mad_inline!($md $(, $value)*)).map_err(Error::from)?;
+ $skin.paragraph = p;
+ Ok::<(), Error>(())
+ }};
+}
+
+#[macro_export]
+macro_rules! print_success {
+ ($skin: expr, $md: literal $(, $value: expr )* $(,)? ) => {{
+ use lazy_static::lazy_static;
+ use minimad::mad_inline;
+ use crate::error::Error;
+ let err = &mut std::io::stderr();
+ let p = $skin.paragraph.clone();
+ $skin.paragraph.set_fg(crossterm::style::Color::Green);
+ termimad::mad_write_inline!(err, $skin, "✔ ").map_err(Error::from)?;
+ $skin.write_composite(err, mad_inline!($md $(, $value)*)).map_err(Error::from)?;
+ $skin.paragraph = p;
+ Ok::<(), Error>(())
+ }};
+}
+
+#[macro_export]
+macro_rules! print_log {
+ ($skin: expr, $md: literal $(, $value: expr )* $(,)? ) => {{
+ use lazy_static::lazy_static;
+ use minimad::mad_inline;
+ use crate::error::Error;
+ let err = &mut std::io::stderr();
+ let p = $skin.paragraph.clone();
+ $skin.paragraph.set_fg(crossterm::style::Color::Blue);
+ termimad::mad_write_inline!(err, $skin, "• ").map_err(Error::from)?;
+ $skin.write_composite(err, mad_inline!($md $(, $value)*)).map_err(Error::from)?;
+ $skin.paragraph = p;
+ Ok::<(), Error>(())
+ }};
+}
+
+#[macro_export]
+macro_rules! print_warn {
+ ($skin: expr, $md: literal $(, $value: expr )* $(,)? ) => {{
+ use lazy_static::lazy_static;
+ use minimad::mad_inline;
+ use crate::error::Error;
+ let err = &mut std::io::stderr();
+ let p = $skin.paragraph.clone();
+ $skin.paragraph.set_fg(crossterm::style::Color::Magenta);
+ termimad::mad_write_inline!(err, $skin, "⚡").map_err(Error::from)?;
$skin.write_composite(err, mad_inline!($md $(, $value)*)).map_err(Error::from)?;
$skin.paragraph = p;
Ok::<(), Error>(())
diff --git a/src/main.rs b/src/main.rs
index 9c75662..5e91580 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -3,7 +3,6 @@ mod config;
mod error;
mod macros;
mod stackexchange;
-mod term;
use config::Config;
use crossterm::style::Color;
@@ -11,8 +10,6 @@ use error::{Error, ErrorKind};
use lazy_static::lazy_static;
use minimad::mad_inline;
use stackexchange::{LocalStorage, StackExchange};
-use std::io::stderr;
-use term::{prefix_err, ColoredOutput};
use termimad::MadSkin;
fn main() {
@@ -32,20 +29,15 @@ fn main() {
if opts.list_sites {
let sites = ls.sites()?;
- match sites.iter().map(|s| s.api_site_parameter.len()).max() {
- Some(max_w) => {
- for s in sites {
- // TODO print as table!
- println!("{:>w$}: {}", s.api_site_parameter, s.site_url, w = max_w);
- }
- }
- None => {
- stderr()
- .queue_error("The site list is empty. Try running ")
- .queue_code_inline("so --update-sites")
- .unsafe_flush();
- }
+ let mut md = String::new();
+ md.push_str("|:-:|:-:|\n");
+ md.push_str("|Site Code|Site URL|\n");
+ md.push_str("|-:|:-|\n");
+ for s in sites.iter() {
+ md.push_str(&format!("|{}|{}\n", s.api_site_parameter, s.site_url));
}
+ md.push_str("|-\n");
+ termimad::print_text(&md);
return Ok(());
}
@@ -69,10 +61,14 @@ fn main() {
kind: ErrorKind::EmptySites,
..
}) => {
- stderr()
- .queue_error("The cached site list is empty. This can likely be fixed by\n\n")
- .queue_code("so --update-sites\n\n")
- .unsafe_flush();
+ // TODO use text wrapping feature
+ print_error!(
+ skin,
+ "The cached site list is empty. This can likely be fixed by\n\n\
+ ```\n\
+ so --update-sites\n\
+ ```"
+ )?;
return Ok(());
}
Err(e) => return Err(e),
@@ -101,10 +97,10 @@ fn main() {
Ok(())
})()
+ .or_else(|e| print_error!(MadSkin::default(), "{}", &e.error))
.unwrap_or_else(|e| {
- let Error { error, .. } = e;
- printerr!(error);
- })
+ println!("{}", e.error);
+ });
}
#[cfg(test)]
diff --git a/src/stackexchange.rs b/src/stackexchange.rs
index 10edaea..58a2c0f 100644
--- a/src/stackexchange.rs
+++ b/src/stackexchange.rs
@@ -145,6 +145,7 @@ impl LocalStorage {
}
// TODO make this async, inform user if we are downloading
+ // TODO issue EmptySites from here when appropriate
pub fn sites(&mut self) -> Result<&Vec<Site>> {
// Stop once Option ~ Some or Result ~ Err
if self.sites.is_some() {
diff --git a/src/term.rs b/src/term.rs
deleted file mode 100644
index cd2db46..0000000
--- a/src/term.rs
+++ /dev/null
@@ -1,73 +0,0 @@
-use crossterm::style::{Color, Print, ResetColor, SetForegroundColor};
-use crossterm::QueueableCommand;
-use lazy_static::lazy_static;
-use minimad::mad_inline;
-use std::io::Write;
-use termimad::{mad_write_inline, MadSkin};
-
-pub fn prefix_err() -> Result<MadSkin, termimad::Error> {
- let mut skin = MadSkin::default();
- skin.paragraph.set_fg(Color::Red);
- mad_write_inline!(&mut std::io::stderr(), &skin, "✖ ")?;
- Ok(skin)
-}
-
-pub trait ColoredOutput {
- fn queue_general(&mut self, color: Color, prefix: &str, s: &str) -> &mut Self;
-
- // TODO is it cool to unwrap flushing some known text?
- fn unsafe_flush(&mut self);
-
- fn queue_error(&mut self, s: &str) -> &mut Self {
- self.queue_general(Color::Red, "✖ ", s)
- }
-
- fn queue_success(&mut self, s: &str) -> &mut Self {
- self.queue_general(Color::Green, "✔ ", s)
- }
-
- fn queue_notice(&mut self, s: &str) -> &mut Self {
- self.queue_general(Color::Yellow, "➜ ", s)
- }
-
- fn queue_notice_inline(&mut self, s: &str) -> &mut Self {
- self.queue_general(Color::Yellow, "", s)
- }
-
- fn queue_log(&mut self, s: &str) -> &mut Self {
- self.queue_general(Color::Blue, "• ", s)
- }
-
- fn queue_code(&mut self, s: &str) -> &mut Self {
- self.queue_general(Color::Cyan, "\t", s)
- }
-
- fn queue_code_inline(&mut self, s: &str) -> &mut Self {
- self.queue_general(Color::Cyan, "", s)
- }
-
- fn queue_warn(&mut self, s: &str) -> &mut Self {
- self.queue_general(Color::Magenta, "⚡", s)
- }
-}
-
-impl<T> ColoredOutput for T
-where
- T: Write,
-{
- fn queue_general(&mut self, color: Color, prefix: &str, s: &str) -> &mut Self {
- (|| -> Result<(), crossterm::ErrorKind> {
- self.queue(SetForegroundColor(color))?
- .queue(Print(prefix.to_string()))?
- .queue(Print(s.to_string()))?
- .queue(ResetColor)?;
- Ok(())
- })()
- .unwrap();
- self
- }
-
- fn unsafe_flush(&mut self) {
- self.flush().unwrap();
- }
-}