on master help sign in
Fix warnings, sort imports, input from string
commit26c951fec485e21e2f28f6704590b55c16f00257 authorsharkdp <davidpeter@web.de> dateWed 22 Apr 2020, 18:10 (7 years ago) parent590960f
11 files changed+65−54
src/assets.rs+5−4 src/bin/bat/app.rs+3−7 src/bin/bat/assets.rs+2−1 src/bin/bat/main.rs+2−4 src/config.rs+4−4 src/controller.rs+1−1 src/input.rs+13−6 src/lib.rs+20−10 src/pretty_printer.rs+13−14 src/printer.rs+1−2 tests/no_duplicate_extensions.rs+1−1
src/assets.rs+5−4
@@ -10,7 +10,7 @@ use syntect::parsing::{SyntaxReference, SyntaxSet, SyntaxSetBuilder};
1010
1111use crate::assets_metadata::AssetsMetadata;
1212use crate::errors::*;
13−use crate::input::{Input, InputKind, InputReader, OpenedInput, OpenedInputKind};
13+use crate::input::{InputReader, OpenedInput, OpenedInputKind};
1414use crate::syntax_mapping::{MappingTarget, SyntaxMapping};
1515
1616#[derive(Debug)]
@@ -257,13 +257,14 @@ impl HighlightingAssets {
257257mod tests {
258258 use super::*;
259259
260− use std::ffi::{OsStr, OsString};
260+ use std::ffi::OsStr;
261261 use std::fs::File;
262− use std::io;
263262 use std::io::Write;
264263
265264 use tempdir::TempDir;
266265
266+ use crate::input::Input;
267+
267268 struct SyntaxDetectionTest<'a> {
268269 assets: HighlightingAssets,
269270 pub syntax_mapping: SyntaxMapping<'a>,
@@ -287,7 +288,7 @@ mod tests {
287288 writeln!(temp_file, "{}", first_line).unwrap();
288289 }
289290
290− let input: Input = Input::ordinary_file(file_path.as_os_str());
291+ let input = Input::ordinary_file(file_path.as_os_str());
291292 let dummy_stdin: &[u8] = &[];
292293 let mut opened_input = input.open(dummy_stdin).unwrap();
293294 let syntax = self
src/bin/bat/app.rs+3−7
@@ -14,13 +14,9 @@ use clap::ArgMatches;
1414use console::Term;
1515
1616use bat::{
17− config::{
18− Config, HighlightedLineRanges, LineRange, LineRanges, MappingTarget, PagingMode,
19− StyleComponent, StyleComponents, SyntaxMapping, WrappingMode,
20− },
21− errors::*,
22− input::Input,
23− HighlightingAssets,
17+ assets::HighlightingAssets, config::Config, errors::*, input::Input, HighlightedLineRanges,
18+ LineRange, LineRanges, MappingTarget, PagingMode, StyleComponent, StyleComponents,
19+ SyntaxMapping, WrappingMode,
2420};
2521
2622fn is_truecolor_terminal() -> bool {
src/bin/bat/assets.rs+2−1
@@ -5,8 +5,9 @@ use clap::crate_version;
55
66use crate::directories::PROJECT_DIRS;
77
8+use bat::assets::HighlightingAssets;
9+use bat::assets_metadata::AssetsMetadata;
810use bat::errors::*;
9−use bat::{AssetsMetadata, HighlightingAssets};
1011
1112pub fn config_dir() -> Cow<'static, str> {
1213 PROJECT_DIRS.config_dir().to_string_lossy()
src/bin/bat/main.rs+2−4
@@ -26,10 +26,8 @@ use clap::crate_version;
2626use directories::PROJECT_DIRS;
2727
2828use bat::{
29− config::{Config, StyleComponent, StyleComponents},
30− errors::*,
31− input::Input,
32− Controller, HighlightingAssets,
29+ assets::HighlightingAssets, config::Config, controller::Controller, errors::*, input::Input,
30+ StyleComponent, StyleComponents,
3331};
3432
3533fn run_cache_subcommand(matches: &clap::ArgMatches) -> Result<()> {
src/config.rs+4−4
@@ -1,7 +1,7 @@
1−pub use crate::line_range::{HighlightedLineRanges, LineRange, LineRanges};
2−pub use crate::style::{StyleComponent, StyleComponents};
3−pub use crate::syntax_mapping::{MappingTarget, SyntaxMapping};
4−pub use crate::wrap::WrappingMode;
1+use crate::line_range::{HighlightedLineRanges, LineRanges};
2+use crate::style::StyleComponents;
3+use crate::syntax_mapping::SyntaxMapping;
4+use crate::wrap::WrappingMode;
55
66#[derive(Debug, Clone, Copy, PartialEq)]
77#[cfg(feature = "paging")]
src/controller.rs+1−1
@@ -5,7 +5,7 @@ use crate::config::Config;
55#[cfg(feature = "paging")]
66use crate::config::PagingMode;
77use crate::errors::*;
8−use crate::input::{Input, InputDescription, InputKind, InputReader, OpenedInput};
8+use crate::input::{Input, InputKind, InputReader, OpenedInput};
99use crate::line_range::{LineRanges, RangeCheckResult};
1010use crate::output::OutputType;
1111use crate::printer::{InteractivePrinter, Printer, SimplePrinter};
src/input.rs+13−6
@@ -15,11 +15,11 @@ pub struct InputDescription {
1515 pub name: String,
1616}
1717
18−pub enum InputKind {
18+pub enum InputKind<'a> {
1919 OrdinaryFile(OsString),
2020 StdIn,
2121 ThemePreviewFile,
22− CustomReader(Box<dyn BufRead>),
22+ CustomReader(Box<dyn Read + 'a>),
2323}
2424
2525#[derive(Clone, Default)]
@@ -27,8 +27,8 @@ pub struct InputMetadata {
2727 pub user_provided_name: Option<OsString>,
2828}
2929
30−pub struct Input {
31− pub kind: InputKind,
30+pub struct Input<'a> {
31+ pub kind: InputKind<'a>,
3232 pub metadata: InputMetadata,
3333}
3434
@@ -45,7 +45,7 @@ pub struct OpenedInput<'a> {
4545 pub reader: InputReader<'a>,
4646}
4747
48−impl Input {
48+impl<'a> Input<'a> {
4949 pub fn ordinary_file(path: &OsStr) -> Self {
5050 Input {
5151 kind: InputKind::OrdinaryFile(path.to_os_string()),
@@ -67,6 +67,13 @@ impl Input {
6767 }
6868 }
6969
70+ pub fn from_reader(reader: Box<dyn Read + 'a>) -> Self {
71+ Input {
72+ kind: InputKind::CustomReader(reader),
73+ metadata: InputMetadata::default(),
74+ }
75+ }
76+
7077 pub fn is_stdin(&self) -> bool {
7178 if let InputKind::StdIn = self.kind {
7279 true
@@ -79,7 +86,7 @@ impl Input {
7986 self.metadata.user_provided_name = provided_name.map(|n| n.to_owned());
8087 }
8188
82− pub fn open<'a, R: BufRead + 'a>(self, stdin: R) -> Result<OpenedInput<'a>> {
89+ pub fn open<R: BufRead + 'a>(self, stdin: R) -> Result<OpenedInput<'a>> {
8390 match self.kind {
8491 InputKind::StdIn => Ok(OpenedInput {
8592 kind: OpenedInputKind::StdIn,
src/lib.rs+20−10
@@ -1,10 +1,18 @@
1−// `error_chain!` can recurse deeply
2−#![recursion_limit = "1024"]
3−
4−pub(crate) mod assets;
5−pub(crate) mod assets_metadata;
1+/// `bat` is a library to print syntax highlighted content.
2+///
3+/// ```
4+/// use bat::PrettyPrinter;
5+///
6+/// PrettyPrinter::new()
7+/// .input_from_bytes(b"<span style=\"color: #ff00cc\">Hello world!</span>\n")
8+/// .language("html")
9+/// .run()
10+/// .expect("no errors");
11+/// ```
12+pub mod assets;
13+pub mod assets_metadata;
614pub mod config;
7−pub(crate) mod controller;
15+pub mod controller;
816mod decorations;
917mod diff;
1018pub mod errors;
@@ -20,9 +28,11 @@ pub(crate) mod syntax_mapping;
2028mod terminal;
2129pub(crate) mod wrap;
2230
23−pub use assets::HighlightingAssets;
24−pub use assets_metadata::AssetsMetadata;
25−pub use controller::Controller;
31+pub use line_range::{HighlightedLineRanges, LineRange, LineRanges};
2632pub use pretty_printer::PrettyPrinter;
27−pub use printer::{InteractivePrinter, Printer, SimplePrinter};
2833pub use style::{StyleComponent, StyleComponents};
34+pub use syntax_mapping::{MappingTarget, SyntaxMapping};
35+pub use wrap::WrappingMode;
36+
37+#[cfg(feature = "paging")]
38+pub use config::PagingMode;
src/pretty_printer.rs+13−14
@@ -2,19 +2,15 @@ use std::ffi::OsStr;
22use std::io::Read;
33
44use crate::{
5− config::{
6− Config, HighlightedLineRanges, LineRanges, StyleComponents, SyntaxMapping, WrappingMode,
7− },
8− errors::Result,
9− input::{Input, InputKind, OpenedInput},
10− Controller, HighlightingAssets,
5+ assets::HighlightingAssets, config::Config, controller::Controller, errors::Result,
6+ input::Input, HighlightedLineRanges, LineRanges, StyleComponents, SyntaxMapping, WrappingMode,
117};
128
139#[cfg(feature = "paging")]
1410use crate::config::PagingMode;
1511
1612pub struct PrettyPrinter<'a> {
17− inputs: Vec<Input>,
13+ inputs: Vec<Input<'a>>,
1814 config: Config<'a>,
1915 assets: HighlightingAssets,
2016}
@@ -35,8 +31,7 @@ impl<'a> PrettyPrinter<'a> {
3531
3632 /// Add a file which should be pretty-printed
3733 pub fn input_file(&mut self, path: &OsStr) -> &mut Self {
38− // self.inputs
39− // .push(Input::Ordinary(OrdinaryFile::from_path(path)));
34+ self.inputs.push(Input::ordinary_file(path));
4035 self
4136 }
4237
@@ -47,21 +42,25 @@ impl<'a> PrettyPrinter<'a> {
4742 P: AsRef<OsStr>,
4843 {
4944 for path in paths {
50− // self.inputs
51− // .push(Input::Ordinary(OrdinaryFile::from_path(path.as_ref())));
45+ self.inputs.push(Input::ordinary_file(path.as_ref()));
5246 }
5347 self
5448 }
5549
5650 /// Add STDIN as an input
5751 pub fn input_stdin(&mut self) -> &mut Self {
58− // self.inputs.push(Input::StdIn(None));
52+ self.inputs.push(Input::stdin());
5953 self
6054 }
6155
56+ /// Use a string as an input
57+ pub fn input_from_bytes(&mut self, content: &'a [u8]) -> &mut Self {
58+ self.input_from_reader(content)
59+ }
60+
6261 /// Add a custom reader as an input
63− pub fn input_reader(&mut self, reader: impl Read) -> &mut Self {
64− //self.inputs.push(Input::FromReader(Box::new(reader), None));
62+ pub fn input_from_reader<R: Read + 'a>(&mut self, reader: R) -> &mut Self {
63+ self.inputs.push(Input::from_reader(Box::new(reader)));
6564 self
6665 }
6766
src/printer.rs+1−2
@@ -1,4 +1,3 @@
1−use std::borrow::Cow;
21use std::io::Write;
32use std::vec::Vec;
43
@@ -27,7 +26,7 @@ use crate::decorations::{Decoration, GridBorderDecoration, LineNumberDecoration}
2726#[cfg(feature = "git")]
2827use crate::diff::{get_git_diff, LineChanges};
2928use crate::errors::*;
30−use crate::input::{Input, InputDescription, InputKind, InputReader, OpenedInput, OpenedInputKind};
29+use crate::input::{OpenedInput, OpenedInputKind};
3130use crate::line_range::RangeCheckResult;
3231use crate::preprocessor::{expand_tabs, replace_nonprintable};
3332use crate::terminal::{as_terminal_escaped, to_ansi_color};
tests/no_duplicate_extensions.rs+1−1
@@ -1,6 +1,6 @@
11use std::collections::HashSet;
22
3−use bat::HighlightingAssets;
3+use bat::assets::HighlightingAssets;
44
55#[test]
66fn no_duplicate_extensions() {