mirror of
https://github.com/sharkdp/bat
synced 2024-11-15 00:27:10 +00:00
Reduce nesting in if blocks by short circuiting
This commit is contained in:
parent
9124271eaf
commit
372e42f350
5 changed files with 80 additions and 84 deletions
|
@ -255,57 +255,56 @@ impl HighlightingAssets {
|
||||||
) -> Result<SyntaxReferenceInSet> {
|
) -> Result<SyntaxReferenceInSet> {
|
||||||
if let Some(language) = language {
|
if let Some(language) = language {
|
||||||
let syntax_set = self.get_syntax_set_by_name(language)?;
|
let syntax_set = self.get_syntax_set_by_name(language)?;
|
||||||
syntax_set
|
return syntax_set
|
||||||
.find_syntax_by_token(language)
|
.find_syntax_by_token(language)
|
||||||
.map(|syntax| SyntaxReferenceInSet { syntax, syntax_set })
|
.map(|syntax| SyntaxReferenceInSet { syntax, syntax_set })
|
||||||
.ok_or_else(|| Error::UnknownSyntax(language.to_owned()))
|
.ok_or_else(|| Error::UnknownSyntax(language.to_owned()));
|
||||||
} else {
|
}
|
||||||
let line_syntax = self.get_first_line_syntax(&mut input.reader)?;
|
|
||||||
|
|
||||||
// Get the path of the file:
|
let line_syntax = self.get_first_line_syntax(&mut input.reader)?;
|
||||||
// If this was set by the metadata, that will take priority.
|
|
||||||
// If it wasn't, it will use the real file path (if available).
|
|
||||||
let path_str =
|
|
||||||
input
|
|
||||||
.metadata
|
|
||||||
.user_provided_name
|
|
||||||
.as_ref()
|
|
||||||
.or_else(|| match input.kind {
|
|
||||||
OpenedInputKind::OrdinaryFile(ref path) => Some(path),
|
|
||||||
_ => None,
|
|
||||||
});
|
|
||||||
|
|
||||||
if let Some(path_str) = path_str {
|
// Get the path of the file:
|
||||||
// If a path was provided, we try and detect the syntax based on extension mappings.
|
// If this was set by the metadata, that will take priority.
|
||||||
let path = Path::new(path_str);
|
// If it wasn't, it will use the real file path (if available).
|
||||||
let absolute_path = PathAbs::new(path)
|
let path_str = input
|
||||||
.ok()
|
.metadata
|
||||||
.map(|p| p.as_path().to_path_buf())
|
.user_provided_name
|
||||||
.unwrap_or_else(|| path.to_owned());
|
.as_ref()
|
||||||
|
.or_else(|| match input.kind {
|
||||||
|
OpenedInputKind::OrdinaryFile(ref path) => Some(path),
|
||||||
|
_ => None,
|
||||||
|
});
|
||||||
|
|
||||||
match mapping.get_syntax_for(absolute_path) {
|
if let Some(path_str) = path_str {
|
||||||
Some(MappingTarget::MapToUnknown) => line_syntax
|
// If a path was provided, we try and detect the syntax based on extension mappings.
|
||||||
.ok_or_else(|| Error::UndetectedSyntax(path.to_string_lossy().into())),
|
let path = Path::new(path_str);
|
||||||
|
let absolute_path = PathAbs::new(path)
|
||||||
|
.ok()
|
||||||
|
.map(|p| p.as_path().to_path_buf())
|
||||||
|
.unwrap_or_else(|| path.to_owned());
|
||||||
|
|
||||||
Some(MappingTarget::MapTo(syntax_name)) => {
|
match mapping.get_syntax_for(absolute_path) {
|
||||||
let syntax_set = self.get_syntax_set()?;
|
Some(MappingTarget::MapToUnknown) => line_syntax
|
||||||
syntax_set
|
.ok_or_else(|| Error::UndetectedSyntax(path.to_string_lossy().into())),
|
||||||
.find_syntax_by_name(syntax_name)
|
|
||||||
.map(|syntax| SyntaxReferenceInSet { syntax, syntax_set })
|
|
||||||
.ok_or_else(|| Error::UnknownSyntax(syntax_name.to_owned()))
|
|
||||||
}
|
|
||||||
|
|
||||||
None => {
|
Some(MappingTarget::MapTo(syntax_name)) => {
|
||||||
let file_name = path.file_name().unwrap_or_default();
|
let syntax_set = self.get_syntax_set()?;
|
||||||
self.get_extension_syntax(file_name)?
|
syntax_set
|
||||||
.or(line_syntax)
|
.find_syntax_by_name(syntax_name)
|
||||||
.ok_or_else(|| Error::UndetectedSyntax(path.to_string_lossy().into()))
|
.map(|syntax| SyntaxReferenceInSet { syntax, syntax_set })
|
||||||
}
|
.ok_or_else(|| Error::UnknownSyntax(syntax_name.to_owned()))
|
||||||
|
}
|
||||||
|
|
||||||
|
None => {
|
||||||
|
let file_name = path.file_name().unwrap_or_default();
|
||||||
|
self.get_extension_syntax(file_name)?
|
||||||
|
.or(line_syntax)
|
||||||
|
.ok_or_else(|| Error::UndetectedSyntax(path.to_string_lossy().into()))
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// If a path wasn't provided, we fall back to the detect first-line syntax.
|
|
||||||
line_syntax.ok_or_else(|| Error::UndetectedSyntax("[unknown]".into()))
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// If a path wasn't provided, we fall back to the detect first-line syntax.
|
||||||
|
line_syntax.ok_or_else(|| Error::UndetectedSyntax("[unknown]".into()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -100,17 +100,14 @@ pub fn get_languages(config: &Config) -> Result<String> {
|
||||||
// Also skip if the 'extension' contains another real extension, likely
|
// Also skip if the 'extension' contains another real extension, likely
|
||||||
// that is a full match file name like 'CMakeLists.txt' and 'Cargo.lock'
|
// that is a full match file name like 'CMakeLists.txt' and 'Cargo.lock'
|
||||||
if extension.starts_with('.') || Path::new(extension).extension().is_some() {
|
if extension.starts_with('.') || Path::new(extension).extension().is_some() {
|
||||||
true
|
return true;
|
||||||
} else {
|
|
||||||
let test_file = Path::new("test").with_extension(extension);
|
|
||||||
let syntax_in_set = assets
|
|
||||||
.get_syntax_for_file_name(test_file, &config.syntax_mapping)
|
|
||||||
.unwrap(); // safe since .get_syntaxes() above worked
|
|
||||||
match syntax_in_set {
|
|
||||||
Some(syntax_in_set) => syntax_in_set.syntax.name == lang_name,
|
|
||||||
None => false,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let test_file = Path::new("test").with_extension(extension);
|
||||||
|
let syntax_in_set = assets
|
||||||
|
.get_syntax_for_file_name(test_file, &config.syntax_mapping)
|
||||||
|
.unwrap(); // safe since .get_syntaxes() above worked
|
||||||
|
matches!(syntax_in_set, Some(syntax_in_set) if syntax_in_set.syntax.name == lang_name)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
20
src/input.rs
20
src/input.rs
|
@ -256,18 +256,18 @@ impl<'a> InputReader<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn read_line(&mut self, buf: &mut Vec<u8>) -> io::Result<bool> {
|
pub(crate) fn read_line(&mut self, buf: &mut Vec<u8>) -> io::Result<bool> {
|
||||||
if self.first_line.is_empty() {
|
if !self.first_line.is_empty() {
|
||||||
let res = self.inner.read_until(b'\n', buf).map(|size| size > 0)?;
|
|
||||||
|
|
||||||
if self.content_type == Some(ContentType::UTF_16LE) {
|
|
||||||
self.inner.read_until(0x00, buf).ok();
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(res)
|
|
||||||
} else {
|
|
||||||
buf.append(&mut self.first_line);
|
buf.append(&mut self.first_line);
|
||||||
Ok(true)
|
return Ok(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let res = self.inner.read_until(b'\n', buf).map(|size| size > 0)?;
|
||||||
|
|
||||||
|
if self.content_type == Some(ContentType::UTF_16LE) {
|
||||||
|
let _ = self.inner.read_until(0x00, buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(res)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
12
src/less.rs
12
src/less.rs
|
@ -11,13 +11,13 @@ pub fn retrieve_less_version(less_path: &dyn AsRef<OsStr>) -> Option<usize> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_less_version(output: &[u8]) -> Option<usize> {
|
fn parse_less_version(output: &[u8]) -> Option<usize> {
|
||||||
if output.starts_with(b"less ") {
|
if !output.starts_with(b"less ") {
|
||||||
let version = std::str::from_utf8(&output[5..]).ok()?;
|
return None;
|
||||||
let end = version.find(|c: char| !c.is_ascii_digit())?;
|
|
||||||
version[..end].parse::<usize>().ok()
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let version = std::str::from_utf8(&output[5..]).ok()?;
|
||||||
|
let end = version.find(|c: char| !c.is_ascii_digit())?;
|
||||||
|
version[..end].parse::<usize>().ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -226,29 +226,29 @@ impl<'a> InteractivePrinter<'a> {
|
||||||
|
|
||||||
fn create_fake_panel(&self, text: &str) -> String {
|
fn create_fake_panel(&self, text: &str) -> String {
|
||||||
if self.panel_width == 0 {
|
if self.panel_width == 0 {
|
||||||
"".to_string()
|
return "".to_string();
|
||||||
|
}
|
||||||
|
|
||||||
|
let text_truncated: String = text.chars().take(self.panel_width - 1).collect();
|
||||||
|
let text_filled: String = format!(
|
||||||
|
"{}{}",
|
||||||
|
text_truncated,
|
||||||
|
" ".repeat(self.panel_width - 1 - text_truncated.len())
|
||||||
|
);
|
||||||
|
if self.config.style_components.grid() {
|
||||||
|
format!("{} │ ", text_filled)
|
||||||
} else {
|
} else {
|
||||||
let text_truncated: String = text.chars().take(self.panel_width - 1).collect();
|
text_filled
|
||||||
let text_filled: String = format!(
|
|
||||||
"{}{}",
|
|
||||||
text_truncated,
|
|
||||||
" ".repeat(self.panel_width - 1 - text_truncated.len())
|
|
||||||
);
|
|
||||||
if self.config.style_components.grid() {
|
|
||||||
format!("{} │ ", text_filled)
|
|
||||||
} else {
|
|
||||||
text_filled
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn preprocess(&self, text: &str, cursor: &mut usize) -> String {
|
fn preprocess(&self, text: &str, cursor: &mut usize) -> String {
|
||||||
if self.config.tab_width > 0 {
|
if self.config.tab_width > 0 {
|
||||||
expand_tabs(text, self.config.tab_width, cursor)
|
return expand_tabs(text, self.config.tab_width, cursor);
|
||||||
} else {
|
|
||||||
*cursor += text.len();
|
|
||||||
text.to_string()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*cursor += text.len();
|
||||||
|
text.to_string()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue