1use std::io::{self, Write};
3use crate::assets::HighlightingAssets;
4use crate::config::{Config, VisibleLines};
6use crate::diff::{get_git_diff, LineChanges};
8use crate::input::{Input, InputReader, OpenedInput};
10use crate::line_range::LineRange;
11use crate::line_range::{LineRanges, RangeCheckResult};
12use crate::output::OutputType;
13#[cfg(feature = "paging")]
14use crate::paging::PagingMode;
15use crate::printer::{InteractivePrinter, Printer, SimplePrinter};
17pub struct Controller<'a> {
18 config: &'a Config<'a>,
19 assets: &'a HighlightingAssets,
22impl<'b> Controller<'b> {
23 pub fn new<'a>(config: &'a Config, assets: &'a HighlightingAssets) -> Controller<'a> {
24 Controller { config, assets }
27 pub fn run(&self, inputs: Vec<Input>) -> Result<bool> {
28 self.run_with_error_handler(inputs, default_error_handler)
31 pub fn run_with_error_handler(
34 handle_error: impl Fn(&Error, &mut dyn Write),
38 #[cfg(feature = "paging")]
40 use crate::input::InputKind;
43 // Do not launch the pager if NONE of the input files exist
44 let mut paging_mode = self.config.paging_mode;
45 if self.config.paging_mode != PagingMode::Never {
46 let call_pager = inputs.iter().any(|ref input| {
47 if let InputKind::OrdinaryFile(ref path) = input.kind {
48 Path::new(path).exists()
54 paging_mode = PagingMode::Never;
58 let wrapping_mode = self.config.wrapping_mode;
60 output_type = OutputType::from_mode(paging_mode, wrapping_mode, self.config.pager)?;
63 #[cfg(not(feature = "paging"))]
65 output_type = OutputType::stdout();
68 let attached_to_pager = output_type.is_pager();
69 let writer = output_type.handle()?;
70 let mut no_errors: bool = true;
72 let stderr = io::stderr();
73 let print_error = |error: &Error, write: &mut dyn Write| {
74 if attached_to_pager {
75 handle_error(error, write);
77 handle_error(error, &mut stderr.lock());
81 for (index, input) in inputs.into_iter().enumerate() {
82 match input.open(io::stdin().lock()) {
84 print_error(&error, writer);
87 Ok(mut opened_input) => {
88 #[cfg(feature = "git")]
89 let line_changes = if self.config.visible_lines.diff_mode()
90 || (!self.config.loop_through && self.config.style_components.changes())
92 match opened_input.kind {
93 crate::input::OpenedInputKind::OrdinaryFile(ref path) => {
94 let diff = get_git_diff(path);
96 // Skip files without Git modifications
97 if self.config.visible_lines.diff_mode()
100 .map(|changes| changes.is_empty())
108 _ if self.config.visible_lines.diff_mode() => {
109 // Skip non-file inputs in diff mode
118 let mut printer: Box<dyn Printer> = if self.config.loop_through {
119 Box::new(SimplePrinter::new(&self.config))
121 Box::new(InteractivePrinter::new(
125 #[cfg(feature = "git")]
130 let result = self.print_file(
135 #[cfg(feature = "git")]
139 if let Err(error) = result {
140 print_error(&error, writer);
152 printer: &mut dyn Printer,
153 writer: &mut dyn Write,
154 input: &mut OpenedInput,
155 add_header_padding: bool,
156 #[cfg(feature = "git")] line_changes: &Option<LineChanges>,
158 if !input.reader.first_line.is_empty() || self.config.style_components.header() {
159 printer.print_header(writer, input, add_header_padding)?;
162 if !input.reader.first_line.is_empty() {
163 let line_ranges = match self.config.visible_lines {
164 VisibleLines::Ranges(ref line_ranges) => line_ranges.clone(),
165 #[cfg(feature = "git")]
166 VisibleLines::DiffContext(context) => {
167 let mut line_ranges: Vec<LineRange> = vec![];
169 if let Some(line_changes) = line_changes {
170 for line in line_changes.keys() {
171 let line = *line as usize;
172 line_ranges.push(LineRange::new(line - context, line + context));
176 LineRanges::from(line_ranges)
180 self.print_file_ranges(printer, writer, &mut input.reader, &line_ranges)?;
182 printer.print_footer(writer, input)?;
187 fn print_file_ranges(
189 printer: &mut dyn Printer,
190 writer: &mut dyn Write,
191 reader: &mut InputReader,
192 line_ranges: &LineRanges,
194 let mut line_buffer = Vec::new();
195 let mut line_number: usize = 1;
197 let mut first_range: bool = true;
198 let mut mid_range: bool = false;
200 while reader.read_line(&mut line_buffer)? {
201 match line_ranges.check(line_number) {
202 RangeCheckResult::BeforeOrBetweenRanges => {
203 // Call the printer in case we need to call the syntax highlighter
204 // for this line. However, set `out_of_range` to `true`.
205 printer.print_line(true, writer, line_number, &line_buffer)?;
209 RangeCheckResult::InRange => {
210 if self.config.style_components.snip() {
214 } else if !mid_range {
216 printer.print_snip(writer)?;
220 printer.print_line(false, writer, line_number, &line_buffer)?;
222 RangeCheckResult::AfterLastRange => {
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list