mirror of
https://github.com/sharkdp/bat.git
synced 2025-01-12 00:28:23 +01:00
Move read_line functionality to inputfile module
This commit is contained in:
parent
87f021078e
commit
6d1cc8c2c8
@ -1,9 +1,9 @@
|
|||||||
use std::io::{self, BufRead, Write};
|
use std::io::{self, Write};
|
||||||
|
|
||||||
use app::Config;
|
use app::Config;
|
||||||
use assets::HighlightingAssets;
|
use assets::HighlightingAssets;
|
||||||
use errors::*;
|
use errors::*;
|
||||||
use inputfile::InputFile;
|
use inputfile::{InputFile, InputFileReader};
|
||||||
use line_range::LineRange;
|
use line_range::LineRange;
|
||||||
use output::OutputType;
|
use output::OutputType;
|
||||||
use printer::{InteractivePrinter, Printer, SimplePrinter};
|
use printer::{InteractivePrinter, Printer, SimplePrinter};
|
||||||
@ -61,14 +61,14 @@ impl<'b> Controller<'b> {
|
|||||||
&self,
|
&self,
|
||||||
printer: &mut P,
|
printer: &mut P,
|
||||||
writer: &mut Write,
|
writer: &mut Write,
|
||||||
mut reader: Box<BufRead + 'a>,
|
mut reader: InputFileReader,
|
||||||
line_ranges: &Option<LineRange>,
|
line_ranges: &Option<LineRange>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let mut line_buffer = Vec::new();
|
let mut line_buffer = Vec::new();
|
||||||
|
|
||||||
let mut line_number: usize = 1;
|
let mut line_number: usize = 1;
|
||||||
|
|
||||||
while reader.read_until(b'\n', &mut line_buffer)? > 0 {
|
while reader.read_line(&mut line_buffer)? {
|
||||||
match line_ranges {
|
match line_ranges {
|
||||||
&Some(ref range) => {
|
&Some(ref range) => {
|
||||||
if line_number < range.lower {
|
if line_number < range.lower {
|
||||||
|
@ -5,6 +5,22 @@ use errors::*;
|
|||||||
|
|
||||||
const THEME_PREVIEW_FILE: &[u8] = include_bytes!("../assets/theme_preview.rs");
|
const THEME_PREVIEW_FILE: &[u8] = include_bytes!("../assets/theme_preview.rs");
|
||||||
|
|
||||||
|
pub struct InputFileReader<'a> {
|
||||||
|
inner: Box<dyn BufRead + 'a>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> InputFileReader<'a> {
|
||||||
|
fn new<R: BufRead + 'a>(reader: R) -> InputFileReader<'a> {
|
||||||
|
InputFileReader {
|
||||||
|
inner: Box::new(reader),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read_line(&mut self, buf: &mut Vec<u8>) -> io::Result<bool> {
|
||||||
|
self.inner.read_until(b'\n', buf).map(|size| size > 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub enum InputFile<'a> {
|
pub enum InputFile<'a> {
|
||||||
StdIn,
|
StdIn,
|
||||||
@ -13,9 +29,9 @@ pub enum InputFile<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> InputFile<'a> {
|
impl<'a> InputFile<'a> {
|
||||||
pub fn get_reader(&self, stdin: &'a io::Stdin) -> Result<Box<dyn BufRead + 'a>> {
|
pub fn get_reader(&self, stdin: &'a io::Stdin) -> Result<InputFileReader> {
|
||||||
match self {
|
match self {
|
||||||
InputFile::StdIn => Ok(Box::new(stdin.lock())),
|
InputFile::StdIn => Ok(InputFileReader::new(stdin.lock())),
|
||||||
InputFile::Ordinary(filename) => {
|
InputFile::Ordinary(filename) => {
|
||||||
let file = File::open(filename)?;
|
let file = File::open(filename)?;
|
||||||
|
|
||||||
@ -23,9 +39,9 @@ impl<'a> InputFile<'a> {
|
|||||||
return Err(format!("'{}' is a directory.", filename).into());
|
return Err(format!("'{}' is a directory.", filename).into());
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Box::new(BufReader::new(file)))
|
Ok(InputFileReader::new(BufReader::new(file)))
|
||||||
}
|
}
|
||||||
InputFile::ThemePreviewFile => Ok(Box::new(THEME_PREVIEW_FILE)),
|
InputFile::ThemePreviewFile => Ok(InputFileReader::new(THEME_PREVIEW_FILE)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user