use clipboard::{ClipboardContext, ClipboardProvider};
use colored::*;
use serde::Serialize;
#[derive(Clone, Copy, Debug)]
pub struct Log {
pub should_copy: bool,
pub suppress_output: bool,
}
impl Log {
pub fn log_pretty(&self, obj: impl Serialize) {
let buf = Vec::new();
let formatter = serde_json::ser::PrettyFormatter::with_indent(b" ");
let mut ser = serde_json::Serializer::with_formatter(buf, formatter);
obj.serialize(&mut ser).unwrap();
let output = &String::from_utf8(ser.into_inner()).unwrap();
self.log(output);
}
pub fn log(&self, string: impl AsRef<str>) {
if !self.suppress_output {
println!("{}", string.as_ref());
}
if self.should_copy {
self.copy_to_buffer(string);
}
}
pub fn log_list(&self, list: Vec<impl AsRef<str>>) {
list.iter().for_each(|x| self.log(x));
}
pub fn error<T: AsRef<str>>(&self, string: T) -> ! {
eprintln!("{}: {}", "Error".red(), string.as_ref());
std::process::exit(1)
}
pub fn copy_to_buffer(&self, string: impl AsRef<str>) {
let mut ctx: ClipboardContext = ClipboardProvider::new().unwrap();
ctx.set_contents(string.as_ref().to_string()).unwrap();
}
}