From f711fb500629e9331b5c413a62e86cc24e06653c Mon Sep 17 00:00:00 2001 From: Ezinwa Okpoechi Date: Fri, 11 May 2018 02:32:31 +0200 Subject: [PATCH] Split style to separate module --- src/app.rs | 2 +- src/main.rs | 94 +--------------------------------------------------- src/style.rs | 73 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 94 deletions(-) create mode 100644 src/style.rs diff --git a/src/app.rs b/src/app.rs index a7f0018f..c7e8a9d2 100644 --- a/src/app.rs +++ b/src/app.rs @@ -4,7 +4,7 @@ use console::Term; use errors::*; use std::collections::HashSet; use std::env; -use {OutputComponent, OutputComponents}; +use style::{OutputComponent, OutputComponents}; pub struct App { pub matches: ArgMatches<'static>, diff --git a/src/main.rs b/src/main.rs index 6ece21a4..e0639af0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,13 +21,12 @@ mod app; mod assets; mod diff; mod printer; +mod style; mod terminal; -use std::collections::HashSet; use std::fs::{self, File}; use std::io::{self, BufRead, BufReader, Write}; use std::process::{self, Child, Command, Stdio}; -use std::str::FromStr; #[cfg(unix)] use std::os::unix::fs::FileTypeExt; @@ -50,102 +49,11 @@ mod errors { Clap(::clap::Error); Io(::std::io::Error); } - - errors { - NoCorrectStylesSpecified { - description("no correct styles specified") - } - - UnknownStyleName(name: String) { - description("unknown style name") - display("unknown style name: '{}'", name) - } - } } } use errors::*; -#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)] -pub enum OutputComponent { - Auto, - Changes, - Grid, - Header, - Numbers, - Full, - Plain, -} - -impl OutputComponent { - fn components(&self, interactive_terminal: bool) -> &'static [OutputComponent] { - match *self { - OutputComponent::Auto => if interactive_terminal { - OutputComponent::Full.components(interactive_terminal) - } else { - OutputComponent::Plain.components(interactive_terminal) - }, - OutputComponent::Changes => &[OutputComponent::Changes], - OutputComponent::Grid => &[OutputComponent::Grid], - OutputComponent::Header => &[OutputComponent::Header], - OutputComponent::Numbers => &[OutputComponent::Numbers], - OutputComponent::Full => &[ - OutputComponent::Changes, - OutputComponent::Grid, - OutputComponent::Header, - OutputComponent::Numbers, - ], - OutputComponent::Plain => &[], - } - } -} - -impl FromStr for OutputComponent { - type Err = Error; - - fn from_str(s: &str) -> Result { - match s { - "auto" => Ok(OutputComponent::Auto), - "changes" => Ok(OutputComponent::Changes), - "grid" => Ok(OutputComponent::Grid), - "header" => Ok(OutputComponent::Header), - "numbers" => Ok(OutputComponent::Numbers), - "full" => Ok(OutputComponent::Full), - "plain" => Ok(OutputComponent::Plain), - _ => Err(ErrorKind::UnknownStyleName(s.to_owned()).into()), - } - } -} - -pub struct OutputComponents(HashSet); - -impl OutputComponents { - fn changes(&self) -> bool { - self.0.contains(&OutputComponent::Changes) - } - - fn grid(&self) -> bool { - self.0.contains(&OutputComponent::Grid) - } - - fn header(&self) -> bool { - self.0.contains(&OutputComponent::Header) - } - - fn numbers(&self) -> bool { - self.0.contains(&OutputComponent::Numbers) - } -} - -pub struct Options<'a> { - pub true_color: bool, - pub output_components: OutputComponents, - pub language: Option<&'a str>, - pub colored_output: bool, - pub paging: bool, - pub term_width: usize, -} - enum OutputType { Pager(Child), Stdout(io::Stdout), diff --git a/src/style.rs b/src/style.rs new file mode 100644 index 00000000..ce55c7d7 --- /dev/null +++ b/src/style.rs @@ -0,0 +1,73 @@ +use errors::*; +use std::collections::HashSet; +use std::str::FromStr; + +#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)] +pub enum OutputComponent { + Auto, + Changes, + Grid, + Header, + Numbers, + Full, + Plain, +} + +impl OutputComponent { + pub fn components(&self, interactive_terminal: bool) -> &'static [OutputComponent] { + match *self { + OutputComponent::Auto => if interactive_terminal { + OutputComponent::Full.components(interactive_terminal) + } else { + OutputComponent::Plain.components(interactive_terminal) + }, + OutputComponent::Changes => &[OutputComponent::Changes], + OutputComponent::Grid => &[OutputComponent::Grid], + OutputComponent::Header => &[OutputComponent::Header], + OutputComponent::Numbers => &[OutputComponent::Numbers], + OutputComponent::Full => &[ + OutputComponent::Changes, + OutputComponent::Grid, + OutputComponent::Header, + OutputComponent::Numbers, + ], + OutputComponent::Plain => &[], + } + } +} + +impl FromStr for OutputComponent { + type Err = Error; + + fn from_str(s: &str) -> Result { + match s { + "auto" => Ok(OutputComponent::Auto), + "changes" => Ok(OutputComponent::Changes), + "grid" => Ok(OutputComponent::Grid), + "header" => Ok(OutputComponent::Header), + "numbers" => Ok(OutputComponent::Numbers), + "full" => Ok(OutputComponent::Full), + "plain" | _ => Ok(OutputComponent::Plain), + } + } +} + +pub struct OutputComponents(pub HashSet); + +impl OutputComponents { + pub fn changes(&self) -> bool { + self.0.contains(&OutputComponent::Changes) + } + + pub fn grid(&self) -> bool { + self.0.contains(&OutputComponent::Grid) + } + + pub fn header(&self) -> bool { + self.0.contains(&OutputComponent::Header) + } + + pub fn numbers(&self) -> bool { + self.0.contains(&OutputComponent::Numbers) + } +}