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::errors::*;
use crate::input::{Input, InputKind, InputReader, OpenedInput, OpenedInputKind};
use crate::input::{InputReader, OpenedInput, OpenedInputKind};
use crate::syntax_mapping::{MappingTarget, SyntaxMapping};
#[derive(Debug)]
@ -257,13 +257,14 @@ impl HighlightingAssets {
mod tests {
use super::*;
use std::ffi::{OsStr, OsString};
use std::ffi::OsStr;
use std::fs::File;
use std::io;
use std::io::Write;
use tempdir::TempDir;
use crate::input::Input;
struct SyntaxDetectionTest<'a> {
assets: HighlightingAssets,
pub syntax_mapping: SyntaxMapping<'a>,
@ -287,7 +288,7 @@ mod tests {
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 mut opened_input = input.open(dummy_stdin).unwrap();
let syntax = self

View File

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

View File

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

View File

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

View File

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

View File

@ -5,7 +5,7 @@ use crate::config::Config;
#[cfg(feature = "paging")]
use crate::config::PagingMode;
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::output::OutputType;
use crate::printer::{InteractivePrinter, Printer, SimplePrinter};

View File

@ -15,11 +15,11 @@ pub struct InputDescription {
pub name: String,
}
pub enum InputKind {
pub enum InputKind<'a> {
OrdinaryFile(OsString),
StdIn,
ThemePreviewFile,
CustomReader(Box<dyn BufRead>),
CustomReader(Box<dyn Read + 'a>),
}
#[derive(Clone, Default)]
@ -27,8 +27,8 @@ pub struct InputMetadata {
pub user_provided_name: Option<OsString>,
}
pub struct Input {
pub kind: InputKind,
pub struct Input<'a> {
pub kind: InputKind<'a>,
pub metadata: InputMetadata,
}
@ -45,7 +45,7 @@ pub struct OpenedInput<'a> {
pub reader: InputReader<'a>,
}
impl Input {
impl<'a> Input<'a> {
pub fn ordinary_file(path: &OsStr) -> Self {
Input {
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 {
if let InputKind::StdIn = self.kind {
true
@ -79,7 +86,7 @@ impl Input {
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 {
InputKind::StdIn => Ok(OpenedInput {
kind: OpenedInputKind::StdIn,

View File

@ -1,10 +1,18 @@
// `error_chain!` can recurse deeply
#![recursion_limit = "1024"]
pub(crate) mod assets;
pub(crate) mod assets_metadata;
/// `bat` is a library to print syntax highlighted content.
///
/// ```
/// use bat::PrettyPrinter;
///
/// 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(crate) mod controller;
pub mod controller;
mod decorations;
mod diff;
pub mod errors;
@ -20,9 +28,11 @@ pub(crate) mod syntax_mapping;
mod terminal;
pub(crate) mod wrap;
pub use assets::HighlightingAssets;
pub use assets_metadata::AssetsMetadata;
pub use controller::Controller;
pub use line_range::{HighlightedLineRanges, LineRange, LineRanges};
pub use pretty_printer::PrettyPrinter;
pub use printer::{InteractivePrinter, Printer, SimplePrinter};
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 crate::{
config::{
Config, HighlightedLineRanges, LineRanges, StyleComponents, SyntaxMapping, WrappingMode,
},
errors::Result,
input::{Input, InputKind, OpenedInput},
Controller, HighlightingAssets,
assets::HighlightingAssets, config::Config, controller::Controller, errors::Result,
input::Input, HighlightedLineRanges, LineRanges, StyleComponents, SyntaxMapping, WrappingMode,
};
#[cfg(feature = "paging")]
use crate::config::PagingMode;
pub struct PrettyPrinter<'a> {
inputs: Vec<Input>,
inputs: Vec<Input<'a>>,
config: Config<'a>,
assets: HighlightingAssets,
}
@ -35,8 +31,7 @@ impl<'a> PrettyPrinter<'a> {
/// Add a file which should be pretty-printed
pub fn input_file(&mut self, path: &OsStr) -> &mut Self {
// self.inputs
// .push(Input::Ordinary(OrdinaryFile::from_path(path)));
self.inputs.push(Input::ordinary_file(path));
self
}
@ -47,21 +42,25 @@ impl<'a> PrettyPrinter<'a> {
P: AsRef<OsStr>,
{
for path in paths {
// self.inputs
// .push(Input::Ordinary(OrdinaryFile::from_path(path.as_ref())));
self.inputs.push(Input::ordinary_file(path.as_ref()));
}
self
}
/// Add STDIN as an input
pub fn input_stdin(&mut self) -> &mut Self {
// self.inputs.push(Input::StdIn(None));
self.inputs.push(Input::stdin());
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
pub fn input_reader(&mut self, reader: impl Read) -> &mut Self {
//self.inputs.push(Input::FromReader(Box::new(reader), None));
pub fn input_from_reader<R: Read + 'a>(&mut self, reader: R) -> &mut Self {
self.inputs.push(Input::from_reader(Box::new(reader)));
self
}

View File

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

View File

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