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
#[cfg(feature = "serde_derive")]
use serde::Deserialize;
use std::fmt::{self, Display};
use std::str::FromStr;

use error::{FrameworkError, FrameworkErrorKind::ParseError};

/// Color configuration
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde_derive", derive(Deserialize))]
pub enum ColorConfig {
    /// Pick colors automatically based on whether we're using a TTY
    Auto,

    /// Always use colors
    Always,

    /// Never use colors
    Never,
}

impl Default for ColorConfig {
    fn default() -> ColorConfig {
        ColorConfig::Auto
    }
}

impl Display for ColorConfig {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            ColorConfig::Always => "always",
            ColorConfig::Auto => "auto",
            ColorConfig::Never => "never",
        }.fmt(f)
    }
}

impl FromStr for ColorConfig {
    type Err = FrameworkError;

    fn from_str(s: &str) -> Result<Self, FrameworkError> {
        Ok(match s {
            "always" => ColorConfig::Always,
            "auto" => ColorConfig::Auto,
            "never" => ColorConfig::Never,
            other => fail!(ParseError, "bad color config option: {}", other),
        })
    }
}

impl ColorConfig {
    /// Initialize the shell using this color configuration
    pub fn init(self) {
        super::config(self)
    }
}