on master help sign in
Move output to separate module
commit120b33a9dbc88cc21ba0d1a99b72aa655a77792a authorEzinwa Okpoechi <brainmaestro@outlook.com> dateMon 21 May 2018, 14:59 (8 years ago) parent17b37fe
2 files changed+62−87
src/main.rs+6−87 src/output.rs+56
src/main.rs+6−87
@@ -21,25 +21,26 @@ mod app;
2121mod assets;
2222mod decorations;
2323mod diff;
24+mod output;
2425mod printer;
2526mod style;
2627mod terminal;
2728
2829use std::fs::{self, File};
29−use std::io::{self, BufRead, BufReader, Write};
30+use std::io::{self, BufRead, BufReader};
3031use std::path::Path;
31−use std::process::{self, Child, Command, Stdio};
32+use std::process;
3233
33−use ansi_term::Colour::{Fixed, Green, Red, White, Yellow};
34−use ansi_term::Style;
34+use ansi_term::Colour::{Green, Red};
3535
3636use syntect::easy::HighlightLines;
3737use syntect::highlighting::Theme;
3838use syntect::parsing::SyntaxDefinition;
3939
40−use app::{App, PagingMode};
40+use app::App;
4141use assets::{config_dir, syntax_set_path, theme_set_path, HighlightingAssets};
4242use diff::get_git_diff;
43+use output::OutputType;
4344use printer::Printer;
4445
4546mod errors {
@@ -53,88 +54,6 @@ mod errors {
5354
5455use errors::*;
5556
56−enum OutputType {
57− Pager(Child),
58− Stdout(io::Stdout),
59−}
60−
61−impl OutputType {
62− fn from_mode(mode: PagingMode) -> Self {
63− use PagingMode::*;
64− match mode {
65− Always => OutputType::try_pager(false),
66− QuitIfOneScreen => OutputType::try_pager(true),
67− _ => OutputType::stdout(),
68− }
69− }
70−
71− /// Try to launch the pager. Fall back to stdout in case of errors.
72− fn try_pager(quit_if_one_screen: bool) -> Self {
73− let mut args = vec!["--RAW-CONTROL-CHARS", "--no-init"];
74− if quit_if_one_screen {
75− args.push("--quit-if-one-screen");
76− }
77− Command::new("less")
78− .args(&args)
79− .stdin(Stdio::piped())
80− .spawn()
81− .map(OutputType::Pager)
82− .unwrap_or_else(|_| OutputType::stdout())
83− }
84−
85− fn stdout() -> Self {
86− OutputType::Stdout(io::stdout())
87− }
88−
89− fn handle(&mut self) -> Result<&mut Write> {
90− Ok(match *self {
91− OutputType::Pager(ref mut command) => command
92− .stdin
93− .as_mut()
94− .chain_err(|| "Could not open stdin for pager")?,
95− OutputType::Stdout(ref mut handle) => handle,
96− })
97− }
98−}
99−
100−impl Drop for OutputType {
101− fn drop(&mut self) {
102− if let OutputType::Pager(ref mut command) = *self {
103− let _ = command.wait();
104− }
105− }
106−}
107−
108−const GRID_COLOR: u8 = 238;
109−const LINE_NUMBER_COLOR: u8 = 244;
110−
111−#[derive(Default)]
112−pub struct Colors {
113− pub grid: Style,
114− pub filename: Style,
115− pub git_added: Style,
116− pub git_removed: Style,
117− pub git_modified: Style,
118− pub line_number: Style,
119−}
120−
121−impl Colors {
122− fn plain() -> Self {
123− Colors::default()
124− }
125−
126− fn colored() -> Self {
127− Colors {
128− grid: Fixed(GRID_COLOR).normal(),
129− filename: White.bold(),
130− git_added: Green.normal(),
131− git_removed: Red.normal(),
132− git_modified: Yellow.normal(),
133− line_number: Fixed(LINE_NUMBER_COLOR).normal(),
134− }
135− }
136−}
137−
13857fn print_file(
13958 theme: &Theme,
14059 syntax: &SyntaxDefinition,
src/output.rsnew file+56
@@ -0,0 +1,56 @@
1+use app::PagingMode;
2+use errors::*;
3+use std::io::{self, Write};
4+use std::process::{Child, Command, Stdio};
5+
6+pub enum OutputType {
7+ Pager(Child),
8+ Stdout(io::Stdout),
9+}
10+
11+impl OutputType {
12+ pub fn from_mode(mode: PagingMode) -> Self {
13+ use self::PagingMode::*;
14+ match mode {
15+ Always => OutputType::try_pager(false),
16+ QuitIfOneScreen => OutputType::try_pager(true),
17+ _ => OutputType::stdout(),
18+ }
19+ }
20+
21+ /// Try to launch the pager. Fall back to stdout in case of errors.
22+ fn try_pager(quit_if_one_screen: bool) -> Self {
23+ let mut args = vec!["--RAW-CONTROL-CHARS", "--no-init"];
24+ if quit_if_one_screen {
25+ args.push("--quit-if-one-screen");
26+ }
27+ Command::new("less")
28+ .args(&args)
29+ .stdin(Stdio::piped())
30+ .spawn()
31+ .map(OutputType::Pager)
32+ .unwrap_or_else(|_| OutputType::stdout())
33+ }
34+
35+ fn stdout() -> Self {
36+ OutputType::Stdout(io::stdout())
37+ }
38+
39+ pub fn handle(&mut self) -> Result<&mut Write> {
40+ Ok(match *self {
41+ OutputType::Pager(ref mut command) => command
42+ .stdin
43+ .as_mut()
44+ .chain_err(|| "Could not open stdin for pager")?,
45+ OutputType::Stdout(ref mut handle) => handle,
46+ })
47+ }
48+}
49+
50+impl Drop for OutputType {
51+ fn drop(&mut self) {
52+ if let OutputType::Pager(ref mut command) = *self {
53+ let _ = command.wait();
54+ }
55+ }
56+}