@@ -9,7 +9,7 @@ use crate::{
9 config::{Config, VisibleLines},
9 config::{Config, VisibleLines},
10 controller::Controller,
10 controller::Controller,
11 error::Result,
11 error::Result,
12− input::Input,
12+ input::Input as BatInput,
13 line_range::{HighlightedLineRanges, LineRange, LineRanges},
13 line_range::{HighlightedLineRanges, LineRange, LineRanges},
14 style::{StyleComponent, StyleComponents},
14 style::{StyleComponent, StyleComponents},
15 SyntaxMapping, WrappingMode,
15 SyntaxMapping, WrappingMode,
@@ -61,10 +61,17 @@ impl<'a> PrettyPrinter<'a> {
64+ /// Adds multiple inputs which should be pretty-printed
65+ pub fn inputs(&mut self, inputs: impl IntoIterator<Item = Input<'a>>) -> &mut Self {
66+ for input in inputs {
67+ self.inputs.push(input);
64 /// Add a file which should be pretty-printed
72 /// Add a file which should be pretty-printed
65 pub fn input_file(&mut self, path: impl AsRef<OsStr>) -> &mut Self {
73 pub fn input_file(&mut self, path: impl AsRef<OsStr>) -> &mut Self {
66− self.inputs.push(Input::ordinary_file(path.as_ref()));
74+ self.input(Input::from_file(path).kind("File"))
70 /// Add multiple files which should be pretty-printed
77 /// Add multiple files which should be pretty-printed
@@ -73,22 +80,20 @@ impl<'a> PrettyPrinter<'a> {
73 I: IntoIterator<Item = P>,
80 I: IntoIterator<Item = P>,
74 P: AsRef<OsStr>,
81 P: AsRef<OsStr>,
76− for path in paths {
83+ self.inputs(paths.into_iter().map(Input::from_file))
77− self.inputs.push(Input::ordinary_file(path.as_ref()));
82 /// Add STDIN as an input
86 /// Add STDIN as an input
83 pub fn input_stdin(&mut self) -> &mut Self {
87 pub fn input_stdin(&mut self) -> &mut Self {
84− self.inputs.push(Input::stdin());
88+ self.inputs.push(Input::from_stdin());
88 /// Add STDIN as an input (with customized name)
92 /// Add STDIN as an input (with customized name)
89 pub fn input_stdin_with_name(&mut self, name: impl AsRef<OsStr>) -> &mut Self {
94 pub fn input_stdin_with_name(&mut self, name: impl AsRef<OsStr>) -> &mut Self {
90 self.inputs
95 self.inputs
91− .push(Input::stdin().with_name(Some(name.as_ref())));
96+ .push(Input::from_stdin().name(name).kind("File"));
@@ -98,6 +103,8 @@ impl<'a> PrettyPrinter<'a> {
100 /// Add a byte string as an input (with customized name)
105 /// Add a byte string as an input (with customized name)
107+ #[allow(deprecated)]
101 pub fn input_from_bytes_with_name(
108 pub fn input_from_bytes_with_name(
102 &mut self,
109 &mut self,
103 content: &'a [u8],
110 content: &'a [u8],
@@ -108,18 +115,19 @@ impl<'a> PrettyPrinter<'a> {
109 /// Add a custom reader as an input
116 /// Add a custom reader as an input
110 pub fn input_from_reader<R: Read + 'a>(&mut self, reader: R) -> &mut Self {
117 pub fn input_from_reader<R: Read + 'a>(&mut self, reader: R) -> &mut Self {
111− self.inputs.push(Input::from_reader(Box::new(reader)));
118+ self.inputs.push(Input::from_reader(reader));
115 /// Add a custom reader as an input (with customized name)
122 /// Add a custom reader as an input (with customized name)
116 pub fn input_from_reader_with_name<R: Read + 'a>(
124 pub fn input_from_reader_with_name<R: Read + 'a>(
117 &mut self,
125 &mut self,
118 reader: R,
126 reader: R,
119 name: impl AsRef<OsStr>,
127 name: impl AsRef<OsStr>,
120 ) -> &mut Self {
128 ) -> &mut Self {
121 self.inputs
129 self.inputs
122− .push(Input::from_reader(Box::new(reader)).with_name(Some(name.as_ref())));
130+ .push(Input::from_reader(reader).name(name).kind("File"));
@@ -292,10 +300,74 @@ impl<'a> PrettyPrinter<'a> {
293 self.config.style_components = StyleComponents::new(&style_components);
301 self.config.style_components = StyleComponents::new(&style_components);
303+ // Collect the inputs to print
295 let mut inputs: Vec<Input> = vec![];
304 let mut inputs: Vec<Input> = vec![];
296 std::mem::swap(&mut inputs, &mut self.inputs);
305 std::mem::swap(&mut inputs, &mut self.inputs);
307+ // Run the controller
298 let controller = Controller::new(&self.config, &self.assets);
308 let controller = Controller::new(&self.config, &self.assets);
299− controller.run(inputs)
309+ controller.run(inputs.into_iter().map(|i| i.into()).collect())
313+pub struct Input<'a> {
314+ input: BatInput<'a>,
318+ /// A new input from a reader.
319+ pub fn from_reader<R: Read + 'a>(reader: R) -> Self {
320+ BatInput::from_reader(Box::new(reader)).into()
323+ /// A new input from a file.
324+ pub fn from_file(path: impl AsRef<OsStr>) -> Self {
325+ BatInput::ordinary_file(path.as_ref()).into()
328+ /// A new input from bytes.
329+ pub fn from_bytes(bytes: &'a [u8]) -> Self {
330+ Input::from_reader(bytes).into()
333+ /// A new input from STDIN.
334+ pub fn from_stdin() -> Self {
335+ BatInput::stdin().into()
338+ /// The filename of the input.
339+ /// This affects syntax detection and changes the default header title.
340+ pub fn name(mut self, name: impl AsRef<OsStr>) -> Self {
341+ self.input = self.input.with_name(Some(name.as_ref()));
345+ /// The description for the type of input (e.g. "File")
346+ /// This defaults to "File" for files, and nothing for other inputs.
347+ pub fn kind(mut self, kind: impl Into<String>) -> Self {
348+ let kind = kind.into();
351+ .set_kind(if kind.is_empty() { None } else { Some(kind) });
355+ /// The title for the input (e.g. "http://example.com/example.txt")
356+ /// This defaults to the file name.
357+ pub fn title(mut self, title: impl Into<String>) -> Self {
358+ self.input.description_mut().set_title(Some(title.into()));
363+impl<'a> Into<Input<'a>> for BatInput<'a> {
364+ fn into(self) -> Input<'a> {
365+ Input { input: self }
369+impl<'a> Into<BatInput<'a>> for Input<'a> {
370+ fn into(self) -> BatInput<'a> {