aboutsummaryrefslogtreecommitdiff
path: root/src/util/formatting/mod.rs
blob: a20b48f908ac6da75750cec56122f09503a54d70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//! Tools for formatting irc messages

mod truncate;
mod color;

pub use truncate::*;
pub use color::*;

/// Turn strings bold, italic,underline, strikethrough, and monospace.
///
/// The Formatting trait is implemented on `String` and `&str` for your convenience
///
/// # Format a string
/// ```
/// use catinator::util::Formatting;
///
/// let text = "this should be bold";
/// assert_eq!("\x02this should be bold\x02", text.bold())
/// ```
///
/// # Use raw formatting code
/// ```
/// use catinator::util::Formatting;
///
/// let text = "this should be bold";
/// let result = format!("{}{}", String::BOLD, text);
/// assert_eq!("\x02this should be bold", result)
/// ```
pub trait Formatting<'r> {
    const BOLD: &'r str = "\x02";
    const ITALIC: &'r str = "\x1D";
    const UNDERLINE: &'r str = "\x1F";
    const STRIKETHROUGH: &'r str = "\x1E";
    const MONOSPACE: &'r str = "\x11";
    const COLOR: &'r str = "\x03";

    fn bold(self) -> String;
    fn italic(self) -> String;
    fn underline(self) -> String;
    fn strikethrough(self) -> String;
    fn monospace(self) -> String;
    // fn color(self, color: Color) -> Self;
}

impl<'r> Formatting<'r> for String {
    fn bold(mut self) -> Self {
        self.insert_str(0, Self::BOLD);
        self.push_str(Self::BOLD);

        return self;
    }

    fn italic(mut self) -> Self {
        self.insert_str(0, Self::ITALIC);
        self.push_str(Self::ITALIC);

        return self;
    }

    fn underline(mut self) -> Self {
        self.insert_str(0, Self::UNDERLINE);
        self.push_str(Self::UNDERLINE);

        return self;
    }

    fn strikethrough(mut self) -> Self {
        self.insert_str(0, Self::STRIKETHROUGH);
        self.push_str(Self::STRIKETHROUGH);

        return self;
    }

    fn monospace(mut self) -> Self {
        self.insert_str(0, Self::MONOSPACE);
        self.push_str(Self::MONOSPACE);

        return self;
    }

    // TODO implement color codes
    // fn color(mut self, foreground: Option<Color>, background: Option<Color>) -> Self {
    //     self.insert_str(0, Self::COLOR);
    //     self.push_str(Self::COLOR);

    //     return self;
    // }
}

impl<'r> Formatting<'r> for &str {
    fn bold(self) -> String {
        format!("{}{}{}", Self::BOLD, self, Self::BOLD)
    }

    fn italic(self) -> String {
        format!("{}{}{}", Self::ITALIC, self, Self::ITALIC)
    }

    fn underline(self) -> String {
        format!("{}{}{}", Self::UNDERLINE, self, Self::UNDERLINE)
    }

    fn strikethrough(self) -> String {
        format!("{}{}{}", Self::STRIKETHROUGH, self, Self::STRIKETHROUGH)
    }

    fn monospace(self) -> String {
        format!("{}{}{}", Self::MONOSPACE, self, Self::MONOSPACE)
    }

    // TODO implement color codes
    // fn color(mut self, foreground: Option<Color>, background: Option<Color>) -> Self { }
}

#[cfg(all(test, feature = "bench"))]
mod bench {
    use super::*;
    use test::Bencher;

    #[bench]
    fn bench_mut(b: &mut Bencher) {
        b.iter(|| {
            let mut src = "test".to_string();
            src.bold()
        });
    }

    #[bench]
    fn bench_format(b: &mut Bencher) {
        b.iter(|| {
            let mut src = "test".to_string();
            src.italic()
        });
    }
}