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};
16use std::convert::TryFrom;
18pub struct Controller<'a> {
19 config: &'a Config<'a>,
20 assets: &'a HighlightingAssets,
23impl<'b> Controller<'b> {
24 pub fn new<'a>(config: &'a Config, assets: &'a HighlightingAssets) -> Controller<'a> {
25 Controller { config, assets }
28 pub fn run(&self, inputs: Vec<Input>) -> Result<bool> {
29 self.run_with_error_handler(inputs, default_error_handler)
32 pub fn run_with_error_handler(
35 handle_error: impl Fn(&Error, &mut dyn Write),
39 #[cfg(feature = "paging")]
41 use crate::input::InputKind;
44 // Do not launch the pager if NONE of the input files exist
45 let mut paging_mode = self.config.paging_mode;
46 if self.config.paging_mode != PagingMode::Never {
47 let call_pager = inputs.iter().any(|ref input| {
48 if let InputKind::OrdinaryFile(ref path) = input.kind {
49 Path::new(path).exists()
55 paging_mode = PagingMode::Never;
59 let wrapping_mode = self.config.wrapping_mode;
61 output_type = OutputType::from_mode(paging_mode, wrapping_mode, self.config.pager)?;
64 #[cfg(not(feature = "paging"))]
66 output_type = OutputType::stdout();
69 let attached_to_pager = output_type.is_pager();
70 let rw_cycle_detected = !attached_to_pager && {
71 let identifiers: Vec<_> = inputs
73 .flat_map(clircle::Identifier::try_from)
75 clircle::stdout_among_inputs(&identifiers)
78 let writer = output_type.handle()?;
79 let mut no_errors: bool = true;
81 let stderr = io::stderr();
82 let print_error = |error: &Error, write: &mut dyn Write| {
83 if attached_to_pager {
84 handle_error(error, write);
86 handle_error(error, &mut stderr.lock());
90 if rw_cycle_detected {
91 print_error(&"The output file is also an input!".into(), writer);
95 for (index, input) in inputs.into_iter().enumerate() {
96 match input.open(io::stdin().lock()) {
98 print_error(&error, writer);
101 Ok(mut opened_input) => {
102 #[cfg(feature = "git")]
103 let line_changes = if self.config.visible_lines.diff_mode()
104 || (!self.config.loop_through && self.config.style_components.changes())
106 match opened_input.kind {
107 crate::input::OpenedInputKind::OrdinaryFile(ref path) => {
108 let diff = get_git_diff(path);
110 // Skip files without Git modifications
111 if self.config.visible_lines.diff_mode()
114 .map(|changes| changes.is_empty())
122 _ if self.config.visible_lines.diff_mode() => {
123 // Skip non-file inputs in diff mode
132 let mut printer: Box<dyn Printer> = if self.config.loop_through {
133 Box::new(SimplePrinter::new(&self.config))
135 Box::new(InteractivePrinter::new(
139 #[cfg(feature = "git")]
144 let result = self.print_file(
149 #[cfg(feature = "git")]
153 if let Err(error) = result {
154 print_error(&error, writer);
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;
186 line_ranges.push(LineRange::new(line - context, line + context));
190 LineRanges::from(line_ranges)
194 self.print_file_ranges(printer, writer, &mut input.reader, &line_ranges)?;
196 printer.print_footer(writer, input)?;
201 fn print_file_ranges(
203 printer: &mut dyn Printer,
204 writer: &mut dyn Write,
205 reader: &mut InputReader,
206 line_ranges: &LineRanges,
208 let mut line_buffer = Vec::new();
209 let mut line_number: usize = 1;
211 let mut first_range: bool = true;
212 let mut mid_range: bool = false;
214 while reader.read_line(&mut line_buffer)? {
215 match line_ranges.check(line_number) {
216 RangeCheckResult::BeforeOrBetweenRanges => {
217 // Call the printer in case we need to call the syntax highlighter
218 // for this line. However, set `out_of_range` to `true`.
219 printer.print_line(true, writer, line_number, &line_buffer)?;
223 RangeCheckResult::InRange => {
224 if self.config.style_components.snip() {
228 } else if !mid_range {
230 printer.print_snip(writer)?;
234 printer.print_line(false, writer, line_number, &line_buffer)?;
236 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