1use colored::{ColoredString, Colorize};
2use std::fmt::Debug;
3
4static MAX_DISP_SIZE: Option<usize> = Some(32);
5
6pub trait PrintDebug {
12 fn dbg(&self) -> String;
14}
15
16impl<T> PrintDebug for T
17where
18 T: Debug,
19{
20 fn dbg(&self) -> String {
21 let s = format!("{:?}", self);
25 if let Some(n) = MAX_DISP_SIZE {
26 let len = s.chars().count();
27 if len > n {
28 let start = n * 3 / 4;
29 let skip = len - n;
30 let mut it = s.chars();
31 let start: String = it.by_ref().take(start).collect();
32 let end: String = it.skip(skip).collect();
33
34 format!("{} (…) {}", strong(&start), strong(&end))
35 } else {
36 strong(&s).to_string()
37 }
38 } else {
39 strong(&s).to_string()
40 }
41 }
42}
43
44pub fn strong(s: &str) -> ColoredString {
50 s.bright_yellow()
51}
52
53pub trait Str {
59 fn str(self) -> &'static str;
63}
64
65impl Str for bool {
66 fn str(self) -> &'static str {
67 if self {
68 ""
69 } else {
70 " not"
71 }
72 }
73}