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};
19pub struct Controller<'a> {
20 config: &'a Config<'a>,
21 assets: &'a HighlightingAssets,
24impl<'b> Controller<'b> {
25 pub fn new<'a>(config: &'a Config, assets: &'a HighlightingAssets) -> Controller<'a> {
26 Controller { config, assets }
29 pub fn run(&self, inputs: Vec<Input>) -> Result<bool> {
30 self.run_with_error_handler(inputs, default_error_handler)
33 pub fn run_with_error_handler(
36 handle_error: impl Fn(&Error, &mut dyn Write),
40 #[cfg(feature = "paging")]
42 use crate::input::InputKind;
45 // Do not launch the pager if NONE of the input files exist
46 let mut paging_mode = self.config.paging_mode;
47 if self.config.paging_mode != PagingMode::Never {
48 let call_pager = inputs.iter().any(|ref input| {
49 if let InputKind::OrdinaryFile(ref path) = input.kind {
50 Path::new(path).exists()
56 paging_mode = PagingMode::Never;
60 let wrapping_mode = self.config.wrapping_mode;
62 output_type = OutputType::from_mode(paging_mode, wrapping_mode, self.config.pager)?;
65 #[cfg(not(feature = "paging"))]
67 output_type = OutputType::stdout();
70 let attached_to_pager = output_type.is_pager();
71 let stdout_identifier = if cfg!(windows) || attached_to_pager {
74 clircle::Identifier::stdout()
77 let writer = output_type.handle()?;
78 let mut no_errors: bool = true;
80 let stderr = io::stderr();
81 let print_error = |error: &Error, write: &mut dyn Write| {
82 if attached_to_pager {
83 handle_error(error, write);
85 handle_error(error, &mut stderr.lock());
89 for (index, input) in inputs.into_iter().enumerate() {
90 match input.open(io::stdin().lock(), stdout_identifier.as_ref()) {
92 print_error(&error, writer);
95 Ok(mut opened_input) => {
96 #[cfg(feature = "git")]
97 let line_changes = if self.config.visible_lines.diff_mode()
98 || (!self.config.loop_through && self.config.style_components.changes())
100 match opened_input.kind {
101 crate::input::OpenedInputKind::OrdinaryFile(ref path) => {
102 let diff = get_git_diff(path);
104 // Skip files without Git modifications
105 if self.config.visible_lines.diff_mode()
108 .map(|changes| changes.is_empty())
116 _ if self.config.visible_lines.diff_mode() => {
117 // Skip non-file inputs in diff mode
126 let mut printer: Box<dyn Printer> = if self.config.loop_through {
127 Box::new(SimplePrinter::new(&self.config))
129 Box::new(InteractivePrinter::new(
133 #[cfg(feature = "git")]
138 let result = self.print_file(
143 #[cfg(feature = "git")]
147 if let Err(error) = result {
148 print_error(&error, writer);
160 printer: &mut dyn Printer,
161 writer: &mut dyn Write,
162 input: &mut OpenedInput,
163 add_header_padding: bool,
164 #[cfg(feature = "git")] line_changes: &Option<LineChanges>,
166 if !input.reader.first_line.is_empty() || self.config.style_components.header() {
167 printer.print_header(writer, input, add_header_padding)?;
170 if !input.reader.first_line.is_empty() {
171 let line_ranges = match self.config.visible_lines {
172 VisibleLines::Ranges(ref line_ranges) => line_ranges.clone(),
173 #[cfg(feature = "git")]
174 VisibleLines::DiffContext(context) => {
175 let mut line_ranges: Vec<LineRange> = vec![];
177 if let Some(line_changes) = line_changes {
178 for line in line_changes.keys() {
179 let line = *line as usize;
180 line_ranges.push(LineRange::new(line - context, line + context));
184 LineRanges::from(line_ranges)
188 self.print_file_ranges(printer, writer, &mut input.reader, &line_ranges)?;
190 printer.print_footer(writer, input)?;
195 fn print_file_ranges(
197 printer: &mut dyn Printer,
198 writer: &mut dyn Write,
199 reader: &mut InputReader,
200 line_ranges: &LineRanges,
202 let mut line_buffer = Vec::new();
203 let mut line_number: usize = 1;
205 let mut first_range: bool = true;
206 let mut mid_range: bool = false;
208 let style_snip = self.config.style_components.snip();
210 while reader.read_line(&mut line_buffer)? {
211 match line_ranges.check(line_number) {
212 RangeCheckResult::BeforeOrBetweenRanges => {
213 // Call the printer in case we need to call the syntax highlighter
214 // for this line. However, set `out_of_range` to `true`.
215 printer.print_line(true, writer, line_number, &line_buffer)?;
219 RangeCheckResult::InRange => {
224 } else if !mid_range {
226 printer.print_snip(writer)?;
230 printer.print_line(false, writer, line_number, &line_buffer)?;
232 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