summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsharkdp <davidpeter@web.de>2020-04-22 18:10:26 +0200
committerDavid Peter <sharkdp@users.noreply.github.com>2020-04-22 23:55:28 +0200
commit26c951fec485e21e2f28f6704590b55c16f00257 (patch)
tree7120181abe2a884d59f3a6e9ea607e954587d3f0
parent590960f7f5b5454c2da8c36409a8d57a58b2980c (diff)
Fix warnings, sort imports, input from string
-rw-r--r--src/assets.rs9
-rw-r--r--src/bin/bat/app.rs10
-rw-r--r--src/bin/bat/assets.rs3
-rw-r--r--src/bin/bat/main.rs6
-rw-r--r--src/config.rs8
-rw-r--r--src/controller.rs2
-rw-r--r--src/input.rs19
-rw-r--r--src/lib.rs30
-rw-r--r--src/pretty_printer.rs27
-rw-r--r--src/printer.rs3
-rw-r--r--tests/no_duplicate_extensions.rs2
11 files changed, 65 insertions, 54 deletions
diff --git a/src/assets.rs b/src/assets.rs
index 6e3190a0..355c8d73 100644
--- a/src/assets.rs
+++ b/src/assets.rs
@@ -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
diff --git a/src/bin/bat/app.rs b/src/bin/bat/app.rs
index b5e8f28e..3ed47c07 100644
--- a/src/bin/bat/app.rs
+++ b/src/bin/bat/app.rs
@@ -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 {
diff --git a/src/bin/bat/assets.rs b/src/bin/bat/assets.rs
index bfb3c178..1c937433 100644
--- a/src/bin/bat/assets.rs
+++ b/src/bin/bat/assets.rs
@@ -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()
diff --git a/src/bin/bat/main.rs b/src/bin/bat/main.rs
index 3d8b3ff7..16ada31a 100644
--- a/src/bin/bat/main.rs
+++ b/src/bin/bat/main.rs
@@ -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<()> {
diff --git a/src/config.rs b/src/config.rs
index d708fad4..52b6fb27 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -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")]
diff --git a/src/controller.rs b/src/controller.rs
index bc37330d..1ce8f965 100644
--- a/src/controller.rs
+++ b/src/controller.rs
@@ -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};
diff --git a/src/input.rs b/src/input.rs
index dae61d49..2f981c55 100644
--- a/src/input.rs
+++ b/src/input.rs
@@ -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,
diff --git a/src/lib.rs b/src/lib.rs
index cdd5a416..f3bda196 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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;
diff --git a/src/pretty_printer.rs b/src/pretty_printer.rs
index da46ce9b..f4ac6605 100644
--- a/src/pretty_printer.rs
+++ b/src/pretty_printer.rs
@@ -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
}
diff --git a/src/printer.rs b/src/printer.rs
index fc409433..324f0a39 100644
--- a/src/printer.rs
+++ b/src/printer.rs
@@ -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};
diff --git a/tests/no_duplicate_extensions.rs b/tests/no_duplicate_extensions.rs
index 20dfc349..cdc96128 100644
--- a/tests/no_duplicate_extensions.rs
+++ b/tests/no_duplicate_extensions.rs
@@ -1,6 +1,6 @@
use std::collections::HashSet;
-use bat::HighlightingAssets;
+use bat::assets::HighlightingAssets;
#[test]
fn no_duplicate_extensions() {