This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

♻️ Improve ui::Log, implement it on ()

To be able to log without a progress bar

+27 -16
+6 -5
examples/schedule-hell/src/main.rs
··· 2 2 3 3 use anyhow::Result; 4 4 use rand::{SeedableRng, rngs::SmallRng}; 5 - use shapemaker::*; 5 + use shapemaker::{ui::Log, *}; 6 6 use std::{path::PathBuf, time::Duration}; 7 7 8 8 pub struct State { ··· 24 24 } 25 25 26 26 #[tokio::main] 27 - pub async fn main() -> Result<()> { 27 + pub async fn main() { 28 28 let mut canvas = Canvas::with_layers(vec![]); 29 29 30 30 canvas.set_grid_size(16, 9); ··· 97 97 if destination.starts_with("localhost:") { 98 98 video.serve("localhost:8000").await; 99 99 } else { 100 - video.encode(destination)?; 100 + match video.encode(destination) { 101 + Ok(_) => (), 102 + Err(e) => ().log_error("Failed", &format!("{e:?}")), 103 + }; 101 104 } 102 - 103 - Ok(()) 104 105 }
+21 -11
src/ui.rs
··· 20 20 impl Spinner { 21 21 pub fn start(verb: &'static str, message: &str) -> Self { 22 22 let spinner = ProgressBar::new(0).with_style( 23 - ProgressStyle::with_template(&format_log_msg_cyan( 23 + ProgressStyle::with_template(&format_log_msg( 24 + Style::new().bold().cyan(), 24 25 verb, 25 26 &(message.to_owned() + " {spinner:.cyan}"), 26 27 )) ··· 68 69 } 69 70 70 71 pub trait Log { 71 - fn log(&self, verb: &'static str, message: &str); 72 + fn log_styled(&self, style: Style, verb: &'static str, message: &str); 73 + fn log(&self, verb: &'static str, message: &str) { 74 + self.log_styled(Style::new().bold().green(), verb, message); 75 + } 76 + fn log_cyan(&self, verb: &'static str, message: &str) { 77 + self.log_styled(Style::new().bold().cyan(), verb, message); 78 + } 79 + fn log_error(&self, verb: &'static str, message: &str) { 80 + self.log_styled(Style::new().bold().red(), verb, message); 81 + } 72 82 } 73 83 74 - pub fn format_log_msg(verb: &'static str, message: &str) -> String { 75 - let style = Style::new().bold().green(); 84 + fn format_log_msg(style: Style, verb: &'static str, message: &str) -> String { 76 85 format!("{} {}", style.apply_to(format!("{verb:>12}")), message) 77 86 } 78 87 79 - pub fn format_log_msg_cyan(verb: &'static str, message: &str) -> String { 80 - let style = Style::new().bold().cyan(); 81 - format!("{} {}", style.apply_to(format!("{verb:>12}")), message) 88 + impl Log for () { 89 + fn log_styled(&self, style: Style, verb: &'static str, message: &str) { 90 + println!("{}", format_log_msg(style, verb, message)); 91 + } 82 92 } 83 93 84 94 impl Log for ProgressBar { 85 - fn log(&self, verb: &'static str, message: &str) { 86 - self.println(format_log_msg(verb, message)); 95 + fn log_styled(&self, style: Style, verb: &'static str, message: &str) { 96 + self.println(format_log_msg(style, verb, message)); 87 97 } 88 98 } 89 99 90 100 impl Log for Option<&ProgressBar> { 91 - fn log(&self, verb: &'static str, message: &str) { 101 + fn log_styled(&self, style: Style, verb: &'static str, message: &str) { 92 102 if let Some(pb) = self { 93 - pb.println(format_log_msg(verb, message)); 103 + pb.println(format_log_msg(style, verb, message)); 94 104 } 95 105 } 96 106 }