on master help sign in
Split style to separate module
commitf711fb500629e9331b5c413a62e86cc24e06653c authorEzinwa Okpoechi <brainmaestro@outlook.com> dateFri 11 May 2018, 02:32 (9 years ago) parent64a9341
3 files changed+75−94
src/app.rs+1−1 src/main.rs+1−93 src/style.rs+73
src/app.rs+1−1
@@ -4,7 +4,7 @@ use console::Term;
44use errors::*;
55use std::collections::HashSet;
66use std::env;
7−use {OutputComponent, OutputComponents};
7+use style::{OutputComponent, OutputComponents};
88
99pub struct App {
1010 pub matches: ArgMatches<'static>,
src/main.rs+1−93
@@ -21,13 +21,12 @@ mod app;
2121mod assets;
2222mod diff;
2323mod printer;
24+mod style;
2425mod terminal;
2526
26−use std::collections::HashSet;
2727use std::fs::{self, File};
2828use std::io::{self, BufRead, BufReader, Write};
2929use std::process::{self, Child, Command, Stdio};
30−use std::str::FromStr;
3130
3231#[cfg(unix)]
3332use std::os::unix::fs::FileTypeExt;
@@ -50,102 +49,11 @@ mod errors {
5049 Clap(::clap::Error);
5150 Io(::std::io::Error);
5251 }
53−
54− errors {
55− NoCorrectStylesSpecified {
56− description("no correct styles specified")
57− }
58−
59− UnknownStyleName(name: String) {
60− description("unknown style name")
61− display("unknown style name: '{}'", name)
62− }
63− }
6452 }
6553}
6654
6755use errors::*;
6856
69−#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)]
70−pub enum OutputComponent {
71− Auto,
72− Changes,
73− Grid,
74− Header,
75− Numbers,
76− Full,
77− Plain,
78−}
79−
80−impl OutputComponent {
81− fn components(&self, interactive_terminal: bool) -> &'static [OutputComponent] {
82− match *self {
83− OutputComponent::Auto => if interactive_terminal {
84− OutputComponent::Full.components(interactive_terminal)
85− } else {
86− OutputComponent::Plain.components(interactive_terminal)
87− },
88− OutputComponent::Changes => &[OutputComponent::Changes],
89− OutputComponent::Grid => &[OutputComponent::Grid],
90− OutputComponent::Header => &[OutputComponent::Header],
91− OutputComponent::Numbers => &[OutputComponent::Numbers],
92− OutputComponent::Full => &[
93− OutputComponent::Changes,
94− OutputComponent::Grid,
95− OutputComponent::Header,
96− OutputComponent::Numbers,
97− ],
98− OutputComponent::Plain => &[],
99− }
100− }
101−}
102−
103−impl FromStr for OutputComponent {
104− type Err = Error;
105−
106− fn from_str(s: &str) -> Result<Self> {
107− match s {
108− "auto" => Ok(OutputComponent::Auto),
109− "changes" => Ok(OutputComponent::Changes),
110− "grid" => Ok(OutputComponent::Grid),
111− "header" => Ok(OutputComponent::Header),
112− "numbers" => Ok(OutputComponent::Numbers),
113− "full" => Ok(OutputComponent::Full),
114− "plain" => Ok(OutputComponent::Plain),
115− _ => Err(ErrorKind::UnknownStyleName(s.to_owned()).into()),
116− }
117− }
118−}
119−
120−pub struct OutputComponents(HashSet<OutputComponent>);
121−
122−impl OutputComponents {
123− fn changes(&self) -> bool {
124− self.0.contains(&OutputComponent::Changes)
125− }
126−
127− fn grid(&self) -> bool {
128− self.0.contains(&OutputComponent::Grid)
129− }
130−
131− fn header(&self) -> bool {
132− self.0.contains(&OutputComponent::Header)
133− }
134−
135− fn numbers(&self) -> bool {
136− self.0.contains(&OutputComponent::Numbers)
137− }
138−}
139−
140−pub struct Options<'a> {
141− pub true_color: bool,
142− pub output_components: OutputComponents,
143− pub language: Option<&'a str>,
144− pub colored_output: bool,
145− pub paging: bool,
146− pub term_width: usize,
147−}
148−
14957enum OutputType {
15058 Pager(Child),
15159 Stdout(io::Stdout),
src/style.rsnew file+73
@@ -0,0 +1,73 @@
1+use errors::*;
2+use std::collections::HashSet;
3+use std::str::FromStr;
4+
5+#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)]
6+pub enum OutputComponent {
7+ Auto,
8+ Changes,
9+ Grid,
10+ Header,
11+ Numbers,
12+ Full,
13+ Plain,
14+}
15+
16+impl OutputComponent {
17+ pub fn components(&self, interactive_terminal: bool) -> &'static [OutputComponent] {
18+ match *self {
19+ OutputComponent::Auto => if interactive_terminal {
20+ OutputComponent::Full.components(interactive_terminal)
21+ } else {
22+ OutputComponent::Plain.components(interactive_terminal)
23+ },
24+ OutputComponent::Changes => &[OutputComponent::Changes],
25+ OutputComponent::Grid => &[OutputComponent::Grid],
26+ OutputComponent::Header => &[OutputComponent::Header],
27+ OutputComponent::Numbers => &[OutputComponent::Numbers],
28+ OutputComponent::Full => &[
29+ OutputComponent::Changes,
30+ OutputComponent::Grid,
31+ OutputComponent::Header,
32+ OutputComponent::Numbers,
33+ ],
34+ OutputComponent::Plain => &[],
35+ }
36+ }
37+}
38+
39+impl FromStr for OutputComponent {
40+ type Err = Error;
41+
42+ fn from_str(s: &str) -> Result<Self> {
43+ match s {
44+ "auto" => Ok(OutputComponent::Auto),
45+ "changes" => Ok(OutputComponent::Changes),
46+ "grid" => Ok(OutputComponent::Grid),
47+ "header" => Ok(OutputComponent::Header),
48+ "numbers" => Ok(OutputComponent::Numbers),
49+ "full" => Ok(OutputComponent::Full),
50+ "plain" | _ => Ok(OutputComponent::Plain),
51+ }
52+ }
53+}
54+
55+pub struct OutputComponents(pub HashSet<OutputComponent>);
56+
57+impl OutputComponents {
58+ pub fn changes(&self) -> bool {
59+ self.0.contains(&OutputComponent::Changes)
60+ }
61+
62+ pub fn grid(&self) -> bool {
63+ self.0.contains(&OutputComponent::Grid)
64+ }
65+
66+ pub fn header(&self) -> bool {
67+ self.0.contains(&OutputComponent::Header)
68+ }
69+
70+ pub fn numbers(&self) -> bool {
71+ self.0.contains(&OutputComponent::Numbers)
72+ }
73+}