summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsharkdp <davidpeter@web.de>2020-04-22 22:41:25 +0200
committerDavid Peter <sharkdp@users.noreply.github.com>2020-04-22 23:55:28 +0200
commit261a7ea1549cbcd01986f891bbaf1b5936e7641d (patch)
treed0aae0d40c6ccb9bec3098f473f2e241c61719df
parent53a973e9dd9ecf102d7bcc345846f2632585159a (diff)
Add *_with_name methods
-rw-r--r--examples/yaml.rs4
-rw-r--r--src/assets.rs3
-rw-r--r--src/bin/bat/app.rs11
-rw-r--r--src/input.rs3
-rw-r--r--src/pretty_printer.rs29
5 files changed, 37 insertions, 13 deletions
diff --git a/examples/yaml.rs b/examples/yaml.rs
index 0bfc94db..b504e424 100644
--- a/examples/yaml.rs
+++ b/examples/yaml.rs
@@ -27,7 +27,9 @@ fn main() {
PrettyPrinter::new()
.language("yaml")
.line_numbers(true)
- .input_from_bytes(&bytes)
+ .grid(true)
+ .header(true)
+ .input_from_bytes_with_name(&bytes, "person.yaml")
.print()
.unwrap();
}
diff --git a/src/assets.rs b/src/assets.rs
index 11756672..7831b56c 100644
--- a/src/assets.rs
+++ b/src/assets.rs
@@ -311,8 +311,7 @@ mod tests {
}
fn syntax_for_stdin_with_content(&self, file_name: &str, content: &[u8]) -> String {
- let mut input = Input::stdin();
- input.set_provided_name(Some(OsStr::new(file_name)));
+ let input = Input::stdin().with_name(Some(OsStr::new(file_name)));
let mut opened_input = input.open(content).unwrap();
let syntax = self
diff --git a/src/bin/bat/app.rs b/src/bin/bat/app.rs
index b47553f3..f16e8fb6 100644
--- a/src/bin/bat/app.rs
+++ b/src/bin/bat/app.rs
@@ -246,8 +246,7 @@ impl App {
let files: Option<Vec<&OsStr>> = self.matches.values_of_os("FILE").map(|vs| vs.collect());
if files.is_none() {
- let mut input = Input::stdin();
- input.set_provided_name(filenames_or_none.nth(0).unwrap_or(None));
+ let input = Input::stdin().with_name(filenames_or_none.nth(0).unwrap_or(None));
return Ok(vec![input]);
}
let files_or_none: Box<dyn Iterator<Item = _>> = match files {
@@ -259,13 +258,9 @@ impl App {
for (filepath, provided_name) in files_or_none.zip(filenames_or_none) {
if let Some(filepath) = filepath {
if filepath.to_str().unwrap_or_default() == "-" {
- let mut input = Input::stdin();
- input.set_provided_name(provided_name);
- file_input.push(input);
+ file_input.push(Input::stdin().with_name(provided_name));
} else {
- let mut input = Input::ordinary_file(filepath);
- input.set_provided_name(provided_name);
- file_input.push(input);
+ file_input.push(Input::ordinary_file(filepath).with_name(provided_name));
}
}
}
diff --git a/src/input.rs b/src/input.rs
index f155c614..929a6524 100644
--- a/src/input.rs
+++ b/src/input.rs
@@ -82,8 +82,9 @@ impl<'a> Input<'a> {
}
}
- pub fn set_provided_name(&mut self, provided_name: Option<&OsStr>) {
+ pub fn with_name(mut self, provided_name: Option<&OsStr>) -> Self {
self.metadata.user_provided_name = provided_name.map(|n| n.to_owned());
+ self
}
pub(crate) fn open<R: BufRead + 'a>(self, stdin: R) -> Result<OpenedInput<'a>> {
diff --git a/src/pretty_printer.rs b/src/pretty_printer.rs
index 807ddb18..4370c0d9 100644
--- a/src/pretty_printer.rs
+++ b/src/pretty_printer.rs
@@ -79,17 +79,44 @@ impl<'a> PrettyPrinter<'a> {
self
}
- /// Use a string as an input
+ /// Add STDIN as an input (with customized name)
+ pub fn input_stdin_with_name(&mut self, name: impl AsRef<OsStr>) -> &mut Self {
+ self.inputs
+ .push(Input::stdin().with_name(Some(name.as_ref())));
+ self
+ }
+
+ /// Add a byte string as an input
pub fn input_from_bytes(&mut self, content: &'a [u8]) -> &mut Self {
self.input_from_reader(content)
}
+ /// Add a byte string as an input (with customized name)
+ pub fn input_from_bytes_with_name(
+ &mut self,
+ content: &'a [u8],
+ name: impl AsRef<OsStr>,
+ ) -> &mut Self {
+ self.input_from_reader_with_name(content, name)
+ }
+
/// Add a custom reader as an input
pub fn input_from_reader<R: Read + 'a>(&mut self, reader: R) -> &mut Self {
self.inputs.push(Input::from_reader(Box::new(reader)));
self
}
+ /// Add a custom reader as an input (with customized name)
+ pub fn input_from_reader_with_name<R: Read + 'a>(
+ &mut self,
+ reader: R,
+ name: impl AsRef<OsStr>,
+ ) -> &mut Self {
+ self.inputs
+ .push(Input::from_reader(Box::new(reader)).with_name(Some(name.as_ref())));
+ self
+ }
+
/// Specify the syntax file which should be used (default: auto-detect)
pub fn language(&mut self, language: &'a str) -> &mut Self {
self.config.language = Some(language);