on 3446fdf help sign in
bat/src/style.rs
88 lines 2.4 kB history raw
1use std::collections::HashSet;
2use std::str::FromStr;
4use errors::*;
6#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)]
7pub enum OutputComponent {
8 Auto,
9 Changes,
10 Grid,
11 Header,
12 Numbers,
13 Full,
14 Plain,
17#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)]
18pub enum OutputWrap {
19 Character,
20 None,
23impl OutputComponent {
24 pub fn components(&self, interactive_terminal: bool) -> &'static [OutputComponent] {
25 match *self {
26 OutputComponent::Auto => {
27 if interactive_terminal {
28 OutputComponent::Full.components(interactive_terminal)
29 } else {
30 OutputComponent::Plain.components(interactive_terminal)
31 }
32 }
33 OutputComponent::Changes => &[OutputComponent::Changes],
34 OutputComponent::Grid => &[OutputComponent::Grid],
35 OutputComponent::Header => &[OutputComponent::Header],
36 OutputComponent::Numbers => &[OutputComponent::Numbers],
37 OutputComponent::Full => &[
38 OutputComponent::Changes,
39 OutputComponent::Grid,
40 OutputComponent::Header,
41 OutputComponent::Numbers,
42 ],
43 OutputComponent::Plain => &[],
44 }
45 }
48impl FromStr for OutputComponent {
49 type Err = Error;
51 fn from_str(s: &str) -> Result<Self> {
52 match s {
53 "auto" => Ok(OutputComponent::Auto),
54 "changes" => Ok(OutputComponent::Changes),
55 "grid" => Ok(OutputComponent::Grid),
56 "header" => Ok(OutputComponent::Header),
57 "numbers" => Ok(OutputComponent::Numbers),
58 "full" => Ok(OutputComponent::Full),
59 "plain" => Ok(OutputComponent::Plain),
60 _ => Err(format!("Unknown style '{}'", s).into()),
61 }
62 }
65#[derive(Clone)]
66pub struct OutputComponents(pub HashSet<OutputComponent>);
68impl OutputComponents {
69 pub fn changes(&self) -> bool {
70 self.0.contains(&OutputComponent::Changes)
71 }
73 pub fn grid(&self) -> bool {
74 self.0.contains(&OutputComponent::Grid)
75 }
77 pub fn header(&self) -> bool {
78 self.0.contains(&OutputComponent::Header)
79 }
81 pub fn numbers(&self) -> bool {
82 self.0.contains(&OutputComponent::Numbers)
83 }
85 pub fn plain(&self) -> bool {
86 self.0.iter().all(|c| c == &OutputComponent::Plain)
87 }