Skip to main content

fluid/core/
display.rs

1use colored::{ColoredString, Colorize};
2use std::fmt::Debug;
3
4static MAX_DISP_SIZE: Option<usize> = Some(32);
5
6/*
7 * PrintDebug
8 */
9
10/// Trait for an extension. See the method documentation.
11pub trait PrintDebug {
12    /// Display the user's input as a debug string.
13    fn dbg(&self) -> String;
14}
15
16impl<T> PrintDebug for T
17where
18    T: Debug,
19{
20    fn dbg(&self) -> String {
21        //let max = ::std::env::var(super::FLUID_MAX_SIZE)
22        //    .and_then(|s| s.parse::<u32>())
23        //    .unwrap_or(32);
24        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
44/*
45 * strong
46 */
47
48/// Colorize the input to emphatise it.
49pub fn strong(s: &str) -> ColoredString {
50    s.bright_yellow()
51}
52
53/*
54 * Str
55 */
56
57/// Trait for an extension. See the method documentation.
58pub trait Str {
59    /// Convert a bool into a borrowed string as following:
60    /// true → `""`
61    /// false → `" not"`
62    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}