summaryrefslogtreecommitdiffstats
path: root/src/skin.rs
blob: e361ad30dea8afa2bcdeb9b08ec642c4e0bcc59c (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
136
/// Defines the Skin structure with its defautl value.
///
/// A skin is a collection of termimad compound_style
use std::{collections::HashMap, fmt, io::Write};

use crossterm::{
    style::{
        Attribute::*,
        Color::AnsiValue,
        Color::{self, *},
        ResetColor,
    },
    queue,
};
use termimad::{Alignment, CompoundStyle, LineStyle, MadSkin};

use crate::{
    app::W,
    errors::ProgramError,
};

macro_rules! Skin {
    (
        $($name:ident: $fg:expr, $bg:expr; $({$a:expr})*)*
    ) => {
        pub struct Skin {
            $(pub $name: CompoundStyle,)*
        }
        impl Skin {
            /// build a skin without any terminal control character (for file output)
            pub fn no_term() -> Skin {
                Skin {
                    $($name: CompoundStyle::default(),)*
                }
            }
            /// build a skin with some entry overloaded by configuration
            pub fn create(mut skin_conf: HashMap<String, CompoundStyle>) -> Skin {
                Skin {
                    $($name: skin_conf.remove(stringify!($name)).unwrap_or(CompoundStyle::new(
                        $fg,
                        $bg,
                        [$($a),*].to_vec(),
                    )),)*
                }
            }
        }
        impl Clone for Skin {
            fn clone(&self) -> Self {
                Skin {
                    $($name: self.$name.clone(),)*
                }
            }
        }
    }
}

pub fn gray(level: u8) -> Option<Color> {
    Some(AnsiValue(0xE8 + level))
}

pub fn rgb(r: u8, g: u8, b: u8) -> Option<Color> {
    Some(Rgb { r, g, b })
}

pub fn ansi(v: u8) -> Option<Color> {
    Some(AnsiValue(v))
}

Skin! {
    tree: gray(5), None;
    file: gray(18), None;
    directory: Some(Blue), None; {Bold}
    exe: Some(Cyan), None;
    link: Some(Magenta), None;
    pruning: gray(17), None; {Italic}
    permissions: gray(15), None;
    dates: ansi(109), None;
    selected_line: None, gray(4);
    size_bar: Some(White), gray(2);
    char_match: Some(Green), None;
    file_error: Some(Red), None;
    flag_label: gray(15), gray(1);
    flag_value: Some(Blue), gray(1); {Bold}
    input: Some(White), None;
    spinner: gray(10), gray(3);
    status_error: Some(White), Some(Red);
    status_normal: Some(White), gray(3);
    status_elision: gray(15), None;
    scrollbar_track: gray(7), None;
    scrollbar_thumb: gray(22), None;
    help_paragraph: gray(20), None;
    help_bold: ansi(178), None; {Bold}
    help_italic: ansi(229), None; {Italic}
    help_code: gray(21), gray(3);
    help_headers: ansi(178), None;
    help_table_border: ansi(239), None;
}

impl Skin {
    /// build a MadSkin, which will be used for markdown formatting
    /// (for the help screen) by applying the `help_*` entries
    /// of the skin.
    pub fn to_mad_skin(&self) -> MadSkin {
        let mut ms = MadSkin::default();
        ms.paragraph.compound_style = CompoundStyle::from(self.help_paragraph.clone());
        ms.inline_code = CompoundStyle::from(self.help_code.clone());
        ms.code_block.compound_style = ms.inline_code.clone();
        ms.bold = CompoundStyle::from(self.help_bold.clone());
        ms.italic = CompoundStyle::from(self.help_italic.clone());
        ms.table = LineStyle {
            compound_style: CompoundStyle::from(self.help_table_border.clone()),
            align: Alignment::Center,
        };
        if let Some(c) = self.help_headers.get_fg() {
            ms.set_headers_fg(c);
        }
        ms.scrollbar
            .track
            .set_compound_style(CompoundStyle::from(self.scrollbar_track.clone()));
        ms.scrollbar
            .thumb
            .set_compound_style(CompoundStyle::from(self.scrollbar_thumb.clone()));
        ms
    }
}

pub fn reset(w: &mut W) -> Result<(), ProgramError> {
    queue!(w, ResetColor)?;
    Ok(())
}

impl fmt::Debug for Skin {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Skin")
    }
}