on master help sign in
Add *_with_name methods
commit261a7ea1549cbcd01986f891bbaf1b5936e7641d authorsharkdp <davidpeter@web.de> dateWed 22 Apr 2020, 22:41 (7 years ago) parent53a973e
5 files changed+37−13
examples/yaml.rs+3−1 src/assets.rs+1−2 src/bin/bat/app.rs+3−8 src/input.rs+2−1 src/pretty_printer.rs+28−1
examples/yaml.rs+3−1
@@ -27,7 +27,9 @@ fn main() {
2727 PrettyPrinter::new()
2828 .language("yaml")
2929 .line_numbers(true)
30− .input_from_bytes(&bytes)
30+ .grid(true)
31+ .header(true)
32+ .input_from_bytes_with_name(&bytes, "person.yaml")
3133 .print()
3234 .unwrap();
3335}
src/assets.rs+1−2
@@ -311,8 +311,7 @@ mod tests {
311311 }
312312
313313 fn syntax_for_stdin_with_content(&self, file_name: &str, content: &[u8]) -> String {
314− let mut input = Input::stdin();
315− input.set_provided_name(Some(OsStr::new(file_name)));
314+ let input = Input::stdin().with_name(Some(OsStr::new(file_name)));
316315 let mut opened_input = input.open(content).unwrap();
317316
318317 let syntax = self
src/bin/bat/app.rs+3−8
@@ -246,8 +246,7 @@ impl App {
246246 let files: Option<Vec<&OsStr>> = self.matches.values_of_os("FILE").map(|vs| vs.collect());
247247
248248 if files.is_none() {
249− let mut input = Input::stdin();
250− input.set_provided_name(filenames_or_none.nth(0).unwrap_or(None));
249+ let input = Input::stdin().with_name(filenames_or_none.nth(0).unwrap_or(None));
251250 return Ok(vec![input]);
252251 }
253252 let files_or_none: Box<dyn Iterator<Item = _>> = match files {
@@ -259,13 +258,9 @@ impl App {
259258 for (filepath, provided_name) in files_or_none.zip(filenames_or_none) {
260259 if let Some(filepath) = filepath {
261260 if filepath.to_str().unwrap_or_default() == "-" {
262− let mut input = Input::stdin();
263− input.set_provided_name(provided_name);
264− file_input.push(input);
261+ file_input.push(Input::stdin().with_name(provided_name));
265262 } else {
266− let mut input = Input::ordinary_file(filepath);
267− input.set_provided_name(provided_name);
268− file_input.push(input);
263+ file_input.push(Input::ordinary_file(filepath).with_name(provided_name));
269264 }
270265 }
271266 }
src/input.rs+2−1
@@ -82,8 +82,9 @@ impl<'a> Input<'a> {
8282 }
8383 }
8484
85− pub fn set_provided_name(&mut self, provided_name: Option<&OsStr>) {
85+ pub fn with_name(mut self, provided_name: Option<&OsStr>) -> Self {
8686 self.metadata.user_provided_name = provided_name.map(|n| n.to_owned());
87+ self
8788 }
8889
8990 pub(crate) fn open<R: BufRead + 'a>(self, stdin: R) -> Result<OpenedInput<'a>> {
src/pretty_printer.rs+28−1
@@ -79,17 +79,44 @@ impl<'a> PrettyPrinter<'a> {
7979 self
8080 }
8181
82− /// Use a string as an input
82+ /// Add STDIN as an input (with customized name)
83+ pub fn input_stdin_with_name(&mut self, name: impl AsRef<OsStr>) -> &mut Self {
84+ self.inputs
85+ .push(Input::stdin().with_name(Some(name.as_ref())));
86+ self
87+ }
88+
89+ /// Add a byte string as an input
8390 pub fn input_from_bytes(&mut self, content: &'a [u8]) -> &mut Self {
8491 self.input_from_reader(content)
8592 }
8693
94+ /// Add a byte string as an input (with customized name)
95+ pub fn input_from_bytes_with_name(
96+ &mut self,
97+ content: &'a [u8],
98+ name: impl AsRef<OsStr>,
99+ ) -> &mut Self {
100+ self.input_from_reader_with_name(content, name)
101+ }
102+
87103 /// Add a custom reader as an input
88104 pub fn input_from_reader<R: Read + 'a>(&mut self, reader: R) -> &mut Self {
89105 self.inputs.push(Input::from_reader(Box::new(reader)));
90106 self
91107 }
92108
109+ /// Add a custom reader as an input (with customized name)
110+ pub fn input_from_reader_with_name<R: Read + 'a>(
111+ &mut self,
112+ reader: R,
113+ name: impl AsRef<OsStr>,
114+ ) -> &mut Self {
115+ self.inputs
116+ .push(Input::from_reader(Box::new(reader)).with_name(Some(name.as_ref())));
117+ self
118+ }
119+
93120 /// Specify the syntax file which should be used (default: auto-detect)
94121 pub fn language(&mut self, language: &'a str) -> &mut Self {
95122 self.config.language = Some(language);