1use std::io::{self, BufRead, 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};
17use clircle::{Clircle, Identifier};
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(|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;
79 let stderr = io::stderr();
81 for (index, input) in inputs.into_iter().enumerate() {
82 let identifier = stdout_identifier.as_ref();
83 let is_first = index == 0;
84 let result = if input.is_stdin() {
85 self.print_input(input, writer, io::stdin().lock(), identifier, is_first)
87 // Use dummy stdin since stdin is actually not used (#1902)
88 self.print_input(input, writer, io::empty(), identifier, is_first)
90 if let Err(error) = result {
91 if attached_to_pager {
92 handle_error(&error, writer);
94 handle_error(&error, &mut stderr.lock());
103 fn print_input<R: BufRead>(
106 writer: &mut dyn Write,
108 stdout_identifier: Option<&Identifier>,
111 let mut opened_input = input.open(stdin, stdout_identifier)?;
112 #[cfg(feature = "git")]
113 let line_changes = if self.config.visible_lines.diff_mode()
114 || (!self.config.loop_through && self.config.style_components.changes())
116 match opened_input.kind {
117 crate::input::OpenedInputKind::OrdinaryFile(ref path) => {
118 let diff = get_git_diff(path);
120 // Skip files without Git modifications
121 if self.config.visible_lines.diff_mode()
124 .map(|changes| changes.is_empty())
132 _ if self.config.visible_lines.diff_mode() => {
133 // Skip non-file inputs in diff mode
142 let mut printer: Box<dyn Printer> = if self.config.loop_through {
143 Box::new(SimplePrinter::new(self.config))
145 Box::new(InteractivePrinter::new(
149 #[cfg(feature = "git")]
159 #[cfg(feature = "git")]
166 printer: &mut dyn Printer,
167 writer: &mut dyn Write,
168 input: &mut OpenedInput,
169 add_header_padding: bool,
170 #[cfg(feature = "git")] line_changes: &Option<LineChanges>,
172 if !input.reader.first_line.is_empty() || self.config.style_components.header() {
173 printer.print_header(writer, input, add_header_padding)?;
176 if !input.reader.first_line.is_empty() {
177 let line_ranges = match self.config.visible_lines {
178 VisibleLines::Ranges(ref line_ranges) => line_ranges.clone(),
179 #[cfg(feature = "git")]
180 VisibleLines::DiffContext(context) => {
181 let mut line_ranges: Vec<LineRange> = vec![];
183 if let Some(line_changes) = line_changes {
184 for &line in line_changes.keys() {
185 let line = line as usize;
187 .push(LineRange::new(line.saturating_sub(context), line + context));
191 LineRanges::from(line_ranges)
195 self.print_file_ranges(printer, writer, &mut input.reader, &line_ranges)?;
197 printer.print_footer(writer, input)?;
202 fn print_file_ranges(
204 printer: &mut dyn Printer,
205 writer: &mut dyn Write,
206 reader: &mut InputReader,
207 line_ranges: &LineRanges,
209 let mut line_buffer = Vec::new();
210 let mut line_number: usize = 1;
212 let mut first_range: bool = true;
213 let mut mid_range: bool = false;
215 let style_snip = self.config.style_components.snip();
217 while reader.read_line(&mut line_buffer)? {
218 match line_ranges.check(line_number) {
219 RangeCheckResult::BeforeOrBetweenRanges => {
220 // Call the printer in case we need to call the syntax highlighter
221 // for this line. However, set `out_of_range` to `true`.
222 printer.print_line(true, writer, line_number, &line_buffer)?;
226 RangeCheckResult::InRange => {
231 } else if !mid_range {
233 printer.print_snip(writer)?;
237 printer.print_line(false, writer, line_number, &line_buffer)?;
239 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