@@ -9,7 +9,7 @@ use crate::{
99 config::{Config, VisibleLines},
1010 controller::Controller,
1111 error::Result,
12− input::Input,
12+ input::Input as BatInput,
1313 line_range::{HighlightedLineRanges, LineRange, LineRanges},
1414 style::{StyleComponent, StyleComponents},
1515 SyntaxMapping, WrappingMode,
@@ -61,10 +61,17 @@ impl<'a> PrettyPrinter<'a> {
6161 self
6262 }
6363
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);
68+ }
69+ self
70+ }
71+
6472 /// Add a file which should be pretty-printed
6573 pub fn input_file(&mut self, path: impl AsRef<OsStr>) -> &mut Self {
66− self.inputs.push(Input::ordinary_file(path.as_ref()));
67− self
74+ self.input(Input::from_file(path).kind("File"))
6875 }
6976
7077 /// Add multiple files which should be pretty-printed
@@ -73,22 +80,20 @@ impl<'a> PrettyPrinter<'a> {
7380 I: IntoIterator<Item = P>,
7481 P: AsRef<OsStr>,
7582 {
76− for path in paths {
77− self.inputs.push(Input::ordinary_file(path.as_ref()));
78− }
79− self
83+ self.inputs(paths.into_iter().map(Input::from_file))
8084 }
8185
8286 /// Add STDIN as an input
8387 pub fn input_stdin(&mut self) -> &mut Self {
84− self.inputs.push(Input::stdin());
88+ self.inputs.push(Input::from_stdin());
8589 self
8690 }
8791
8892 /// Add STDIN as an input (with customized name)
93+ #[deprecated]
8994 pub fn input_stdin_with_name(&mut self, name: impl AsRef<OsStr>) -> &mut Self {
9095 self.inputs
91− .push(Input::stdin().with_name(Some(name.as_ref())));
96+ .push(Input::from_stdin().name(name).kind("File"));
9297 self
9398 }
9499
@@ -98,6 +103,8 @@ impl<'a> PrettyPrinter<'a> {
98103 }
99104
100105 /// Add a byte string as an input (with customized name)
106+ #[deprecated]
107+ #[allow(deprecated)]
101108 pub fn input_from_bytes_with_name(
102109 &mut self,
103110 content: &'a [u8],
@@ -108,18 +115,19 @@ impl<'a> PrettyPrinter<'a> {
108115
109116 /// Add a custom reader as an input
110117 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));
112119 self
113120 }
114121
115122 /// Add a custom reader as an input (with customized name)
123+ #[deprecated]
116124 pub fn input_from_reader_with_name<R: Read + 'a>(
117125 &mut self,
118126 reader: R,
119127 name: impl AsRef<OsStr>,
120128 ) -> &mut Self {
121129 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"));
123131 self
124132 }
125133
@@ -292,10 +300,74 @@ impl<'a> PrettyPrinter<'a> {
292300 }
293301 self.config.style_components = StyleComponents::new(&style_components);
294302
303+ // Collect the inputs to print
295304 let mut inputs: Vec<Input> = vec![];
296305 std::mem::swap(&mut inputs, &mut self.inputs);
297306
307+ // Run the controller
298308 let controller = Controller::new(&self.config, &self.assets);
299− controller.run(inputs)
309+ controller.run(inputs.into_iter().map(|i| i.into()).collect())
310+ }
311+}
312+
313+pub struct Input<'a> {
314+ input: BatInput<'a>,
315+}
316+
317+impl<'a> Input<'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()
321+ }
322+
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()
326+ }
327+
328+ /// A new input from bytes.
329+ pub fn from_bytes(bytes: &'a [u8]) -> Self {
330+ Input::from_reader(bytes).into()
331+ }
332+
333+ /// A new input from STDIN.
334+ pub fn from_stdin() -> Self {
335+ BatInput::stdin().into()
336+ }
337+
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()));
342+ self
343+ }
344+
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();
349+ self.input
350+ .description_mut()
351+ .set_kind(if kind.is_empty() { None } else { Some(kind) });
352+ self
353+ }
354+
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()));
359+ self
360+ }
361+}
362+
363+impl<'a> Into<Input<'a>> for BatInput<'a> {
364+ fn into(self) -> Input<'a> {
365+ Input { input: self }
366+ }
367+}
368+
369+impl<'a> Into<BatInput<'a>> for Input<'a> {
370+ fn into(self) -> BatInput<'a> {
371+ self.input
300372 }
301373}