Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split long long lines into slices. #712

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ wild = "2.0"
content_inspector = "0.2.4"
encoding = "0.2"
shell-words = "0.1.0"
memchr = "2.2.1"

[dependencies.git2]
version = "0.10"
Expand Down
4 changes: 2 additions & 2 deletions src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ impl HighlightingAssets {
.find_syntax_by_extension(&file_name)
.or_else(|| self.syntax_set.find_syntax_by_extension(&extension));
let line_syntax = if ext_syntax.is_none() {
String::from_utf8(reader.first_line.clone())
String::from_utf8(reader.current_line.clone())
.ok()
.and_then(|l| self.syntax_set.find_syntax_by_first_line(&l))
} else {
Expand All @@ -195,7 +195,7 @@ impl HighlightingAssets {

ext_syntax.or(line_syntax)
}
(None, InputFile::StdIn) => String::from_utf8(reader.first_line.clone())
(None, InputFile::StdIn) => String::from_utf8(reader.current_line.clone())
.ok()
.and_then(|l| self.syntax_set.find_syntax_by_first_line(&l)),
(_, InputFile::ThemePreviewFile) => self.syntax_set.find_syntax_by_name("Rust"),
Expand Down
2 changes: 1 addition & 1 deletion src/bin/bat/clap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
t.parse::<i32>()
.map_err(|_e| "must be an offset or number")
.and_then(|v| if v == 0 && !is_offset {
Err("terminal width cannot be zero".into())
Err("terminal width cannot be zero")
} else {
Ok(())
})
Expand Down
2 changes: 1 addition & 1 deletion src/bin/bat/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub fn list_languages(config: &Config) -> Result<()> {

if config.loop_through {
for lang in languages {
write!(stdout, "{}:{}\n", lang.name, lang.file_extensions.join(","))?;
writeln!(stdout, "{}:{}", lang.name, lang.file_extensions.join(","))?;
}
} else {
let longest = languages
Expand Down
36 changes: 17 additions & 19 deletions src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::Path;

use crate::assets::HighlightingAssets;
use crate::errors::*;
use crate::inputfile::{InputFile, InputFileReader};
use crate::inputfile::{InputFile, InputFileReader, ReadPosition};
use crate::line_range::{LineRanges, RangeCheckResult};
use crate::output::OutputType;
use crate::printer::{InteractivePrinter, Printer, SimplePrinter};
Expand Down Expand Up @@ -80,9 +80,7 @@ impl<'b> Controller<'b> {
input_file: InputFile<'a>,
) -> Result<()> {
printer.print_header(writer, input_file)?;
if !reader.first_line.is_empty() {
self.print_file_ranges(printer, writer, reader, &self.config.line_ranges)?;
}
self.print_file_ranges(printer, writer, reader, &self.config.line_ranges)?;
printer.print_footer(writer)?;

Ok(())
Expand All @@ -95,19 +93,18 @@ impl<'b> Controller<'b> {
mut reader: InputFileReader,
line_ranges: &LineRanges,
) -> Result<()> {
let mut line_buffer = Vec::new();
let mut line_number: usize = 1;

let mut first_range: bool = true;
let mut mid_range: bool = false;

while reader.read_line(&mut line_buffer)? {
match line_ranges.check(line_number) {
loop {
let position = reader.read_line()?;
if position == ReadPosition::Eof { break; }
let is_out_range = match line_ranges.check(reader.line_number) {
RangeCheckResult::OutsideRange => {
// Call the printer in case we need to call the syntax highlighter
// for this line. However, set `out_of_range` to `true`.
printer.print_line(true, writer, line_number, &line_buffer)?;
mid_range = false;
true
}

RangeCheckResult::InRange => {
Expand All @@ -120,16 +117,17 @@ impl<'b> Controller<'b> {
printer.print_snip(writer)?;
}
}

printer.print_line(false, writer, line_number, &line_buffer)?;
false
}
RangeCheckResult::AfterLastRange => {
break;
}
}

line_number += 1;
line_buffer.clear();
RangeCheckResult::AfterLastRange => break,
};
printer.print_line(
is_out_range,
position,
writer,
reader.line_number,
&reader.current_line,
)?;
}
Ok(())
}
Expand Down
Loading