Fix warnings, sort imports, input from string

This commit is contained in:
sharkdp 2020-04-22 18:10:26 +02:00 committed by David Peter
parent 590960f7f5
commit 26c951fec4
11 changed files with 65 additions and 54 deletions

View File

@ -10,7 +10,7 @@ use syntect::parsing::{SyntaxReference, SyntaxSet, SyntaxSetBuilder};
use crate::assets_metadata::AssetsMetadata; use crate::assets_metadata::AssetsMetadata;
use crate::errors::*; use crate::errors::*;
use crate::input::{Input, InputKind, InputReader, OpenedInput, OpenedInputKind}; use crate::input::{InputReader, OpenedInput, OpenedInputKind};
use crate::syntax_mapping::{MappingTarget, SyntaxMapping}; use crate::syntax_mapping::{MappingTarget, SyntaxMapping};
#[derive(Debug)] #[derive(Debug)]
@ -257,13 +257,14 @@ impl HighlightingAssets {
mod tests { mod tests {
use super::*; use super::*;
use std::ffi::{OsStr, OsString}; use std::ffi::OsStr;
use std::fs::File; use std::fs::File;
use std::io;
use std::io::Write; use std::io::Write;
use tempdir::TempDir; use tempdir::TempDir;
use crate::input::Input;
struct SyntaxDetectionTest<'a> { struct SyntaxDetectionTest<'a> {
assets: HighlightingAssets, assets: HighlightingAssets,
pub syntax_mapping: SyntaxMapping<'a>, pub syntax_mapping: SyntaxMapping<'a>,
@ -287,7 +288,7 @@ mod tests {
writeln!(temp_file, "{}", first_line).unwrap(); writeln!(temp_file, "{}", first_line).unwrap();
} }
let input: Input = Input::ordinary_file(file_path.as_os_str()); let input = Input::ordinary_file(file_path.as_os_str());
let dummy_stdin: &[u8] = &[]; let dummy_stdin: &[u8] = &[];
let mut opened_input = input.open(dummy_stdin).unwrap(); let mut opened_input = input.open(dummy_stdin).unwrap();
let syntax = self let syntax = self

View File

@ -14,13 +14,9 @@ use clap::ArgMatches;
use console::Term; use console::Term;
use bat::{ use bat::{
config::{ assets::HighlightingAssets, config::Config, errors::*, input::Input, HighlightedLineRanges,
Config, HighlightedLineRanges, LineRange, LineRanges, MappingTarget, PagingMode, LineRange, LineRanges, MappingTarget, PagingMode, StyleComponent, StyleComponents,
StyleComponent, StyleComponents, SyntaxMapping, WrappingMode, SyntaxMapping, WrappingMode,
},
errors::*,
input::Input,
HighlightingAssets,
}; };
fn is_truecolor_terminal() -> bool { fn is_truecolor_terminal() -> bool {

View File

@ -5,8 +5,9 @@ use clap::crate_version;
use crate::directories::PROJECT_DIRS; use crate::directories::PROJECT_DIRS;
use bat::assets::HighlightingAssets;
use bat::assets_metadata::AssetsMetadata;
use bat::errors::*; use bat::errors::*;
use bat::{AssetsMetadata, HighlightingAssets};
pub fn config_dir() -> Cow<'static, str> { pub fn config_dir() -> Cow<'static, str> {
PROJECT_DIRS.config_dir().to_string_lossy() PROJECT_DIRS.config_dir().to_string_lossy()

View File

@ -26,10 +26,8 @@ use clap::crate_version;
use directories::PROJECT_DIRS; use directories::PROJECT_DIRS;
use bat::{ use bat::{
config::{Config, StyleComponent, StyleComponents}, assets::HighlightingAssets, config::Config, controller::Controller, errors::*, input::Input,
errors::*, StyleComponent, StyleComponents,
input::Input,
Controller, HighlightingAssets,
}; };
fn run_cache_subcommand(matches: &clap::ArgMatches) -> Result<()> { fn run_cache_subcommand(matches: &clap::ArgMatches) -> Result<()> {

View File

@ -1,7 +1,7 @@
pub use crate::line_range::{HighlightedLineRanges, LineRange, LineRanges}; use crate::line_range::{HighlightedLineRanges, LineRanges};
pub use crate::style::{StyleComponent, StyleComponents}; use crate::style::StyleComponents;
pub use crate::syntax_mapping::{MappingTarget, SyntaxMapping}; use crate::syntax_mapping::SyntaxMapping;
pub use crate::wrap::WrappingMode; use crate::wrap::WrappingMode;
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
#[cfg(feature = "paging")] #[cfg(feature = "paging")]

View File

@ -5,7 +5,7 @@ use crate::config::Config;
#[cfg(feature = "paging")] #[cfg(feature = "paging")]
use crate::config::PagingMode; use crate::config::PagingMode;
use crate::errors::*; use crate::errors::*;
use crate::input::{Input, InputDescription, InputKind, InputReader, OpenedInput}; use crate::input::{Input, InputKind, InputReader, OpenedInput};
use crate::line_range::{LineRanges, RangeCheckResult}; use crate::line_range::{LineRanges, RangeCheckResult};
use crate::output::OutputType; use crate::output::OutputType;
use crate::printer::{InteractivePrinter, Printer, SimplePrinter}; use crate::printer::{InteractivePrinter, Printer, SimplePrinter};

View File

@ -15,11 +15,11 @@ pub struct InputDescription {
pub name: String, pub name: String,
} }
pub enum InputKind { pub enum InputKind<'a> {
OrdinaryFile(OsString), OrdinaryFile(OsString),
StdIn, StdIn,
ThemePreviewFile, ThemePreviewFile,
CustomReader(Box<dyn BufRead>), CustomReader(Box<dyn Read + 'a>),
} }
#[derive(Clone, Default)] #[derive(Clone, Default)]
@ -27,8 +27,8 @@ pub struct InputMetadata {
pub user_provided_name: Option<OsString>, pub user_provided_name: Option<OsString>,
} }
pub struct Input { pub struct Input<'a> {
pub kind: InputKind, pub kind: InputKind<'a>,
pub metadata: InputMetadata, pub metadata: InputMetadata,
} }
@ -45,7 +45,7 @@ pub struct OpenedInput<'a> {
pub reader: InputReader<'a>, pub reader: InputReader<'a>,
} }
impl Input { impl<'a> Input<'a> {
pub fn ordinary_file(path: &OsStr) -> Self { pub fn ordinary_file(path: &OsStr) -> Self {
Input { Input {
kind: InputKind::OrdinaryFile(path.to_os_string()), kind: InputKind::OrdinaryFile(path.to_os_string()),
@ -67,6 +67,13 @@ impl Input {
} }
} }
pub fn from_reader(reader: Box<dyn Read + 'a>) -> Self {
Input {
kind: InputKind::CustomReader(reader),
metadata: InputMetadata::default(),
}
}
pub fn is_stdin(&self) -> bool { pub fn is_stdin(&self) -> bool {
if let InputKind::StdIn = self.kind { if let InputKind::StdIn = self.kind {
true true
@ -79,7 +86,7 @@ impl Input {
self.metadata.user_provided_name = provided_name.map(|n| n.to_owned()); self.metadata.user_provided_name = provided_name.map(|n| n.to_owned());
} }
pub fn open<'a, R: BufRead + 'a>(self, stdin: R) -> Result<OpenedInput<'a>> { pub fn open<R: BufRead + 'a>(self, stdin: R) -> Result<OpenedInput<'a>> {
match self.kind { match self.kind {
InputKind::StdIn => Ok(OpenedInput { InputKind::StdIn => Ok(OpenedInput {
kind: OpenedInputKind::StdIn, kind: OpenedInputKind::StdIn,

View File

@ -1,10 +1,18 @@
// `error_chain!` can recurse deeply /// `bat` is a library to print syntax highlighted content.
#![recursion_limit = "1024"] ///
/// ```
pub(crate) mod assets; /// use bat::PrettyPrinter;
pub(crate) mod assets_metadata; ///
/// PrettyPrinter::new()
/// .input_from_bytes(b"<span style=\"color: #ff00cc\">Hello world!</span>\n")
/// .language("html")
/// .run()
/// .expect("no errors");
/// ```
pub mod assets;
pub mod assets_metadata;
pub mod config; pub mod config;
pub(crate) mod controller; pub mod controller;
mod decorations; mod decorations;
mod diff; mod diff;
pub mod errors; pub mod errors;
@ -20,9 +28,11 @@ pub(crate) mod syntax_mapping;
mod terminal; mod terminal;
pub(crate) mod wrap; pub(crate) mod wrap;
pub use assets::HighlightingAssets; pub use line_range::{HighlightedLineRanges, LineRange, LineRanges};
pub use assets_metadata::AssetsMetadata;
pub use controller::Controller;
pub use pretty_printer::PrettyPrinter; pub use pretty_printer::PrettyPrinter;
pub use printer::{InteractivePrinter, Printer, SimplePrinter};
pub use style::{StyleComponent, StyleComponents}; pub use style::{StyleComponent, StyleComponents};
pub use syntax_mapping::{MappingTarget, SyntaxMapping};
pub use wrap::WrappingMode;
#[cfg(feature = "paging")]
pub use config::PagingMode;

View File

@ -2,19 +2,15 @@ use std::ffi::OsStr;
use std::io::Read; use std::io::Read;
use crate::{ use crate::{
config::{ assets::HighlightingAssets, config::Config, controller::Controller, errors::Result,
Config, HighlightedLineRanges, LineRanges, StyleComponents, SyntaxMapping, WrappingMode, input::Input, HighlightedLineRanges, LineRanges, StyleComponents, SyntaxMapping, WrappingMode,
},
errors::Result,
input::{Input, InputKind, OpenedInput},
Controller, HighlightingAssets,
}; };
#[cfg(feature = "paging")] #[cfg(feature = "paging")]
use crate::config::PagingMode; use crate::config::PagingMode;
pub struct PrettyPrinter<'a> { pub struct PrettyPrinter<'a> {
inputs: Vec<Input>, inputs: Vec<Input<'a>>,
config: Config<'a>, config: Config<'a>,
assets: HighlightingAssets, assets: HighlightingAssets,
} }
@ -35,8 +31,7 @@ impl<'a> PrettyPrinter<'a> {
/// Add a file which should be pretty-printed /// Add a file which should be pretty-printed
pub fn input_file(&mut self, path: &OsStr) -> &mut Self { pub fn input_file(&mut self, path: &OsStr) -> &mut Self {
// self.inputs self.inputs.push(Input::ordinary_file(path));
// .push(Input::Ordinary(OrdinaryFile::from_path(path)));
self self
} }
@ -47,21 +42,25 @@ impl<'a> PrettyPrinter<'a> {
P: AsRef<OsStr>, P: AsRef<OsStr>,
{ {
for path in paths { for path in paths {
// self.inputs self.inputs.push(Input::ordinary_file(path.as_ref()));
// .push(Input::Ordinary(OrdinaryFile::from_path(path.as_ref())));
} }
self self
} }
/// Add STDIN as an input /// Add STDIN as an input
pub fn input_stdin(&mut self) -> &mut Self { pub fn input_stdin(&mut self) -> &mut Self {
// self.inputs.push(Input::StdIn(None)); self.inputs.push(Input::stdin());
self self
} }
/// Use a string as an input
pub fn input_from_bytes(&mut self, content: &'a [u8]) -> &mut Self {
self.input_from_reader(content)
}
/// Add a custom reader as an input /// Add a custom reader as an input
pub fn input_reader(&mut self, reader: impl Read) -> &mut Self { pub fn input_from_reader<R: Read + 'a>(&mut self, reader: R) -> &mut Self {
//self.inputs.push(Input::FromReader(Box::new(reader), None)); self.inputs.push(Input::from_reader(Box::new(reader)));
self self
} }

View File

@ -1,4 +1,3 @@
use std::borrow::Cow;
use std::io::Write; use std::io::Write;
use std::vec::Vec; use std::vec::Vec;
@ -27,7 +26,7 @@ use crate::decorations::{Decoration, GridBorderDecoration, LineNumberDecoration}
#[cfg(feature = "git")] #[cfg(feature = "git")]
use crate::diff::{get_git_diff, LineChanges}; use crate::diff::{get_git_diff, LineChanges};
use crate::errors::*; use crate::errors::*;
use crate::input::{Input, InputDescription, InputKind, InputReader, OpenedInput, OpenedInputKind}; use crate::input::{OpenedInput, OpenedInputKind};
use crate::line_range::RangeCheckResult; use crate::line_range::RangeCheckResult;
use crate::preprocessor::{expand_tabs, replace_nonprintable}; use crate::preprocessor::{expand_tabs, replace_nonprintable};
use crate::terminal::{as_terminal_escaped, to_ansi_color}; use crate::terminal::{as_terminal_escaped, to_ansi_color};

View File

@ -1,6 +1,6 @@
use std::collections::HashSet; use std::collections::HashSet;
use bat::HighlightingAssets; use bat::assets::HighlightingAssets;
#[test] #[test]
fn no_duplicate_extensions() { fn no_duplicate_extensions() {