mirror of
https://github.com/sharkdp/bat.git
synced 2025-02-23 05:01:40 +01:00
Fix some clippy lints
Some might actually improve perf
This commit is contained in:
parent
a4828387c1
commit
e37e9c1214
@ -180,7 +180,7 @@ impl HighlightingAssets {
|
|||||||
theme
|
theme
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
&self.theme_set.themes[self.fallback_theme.unwrap_or(Self::default_theme())]
|
&self.theme_set.themes[self.fallback_theme.unwrap_or_else(|| Self::default_theme())]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -204,7 +204,7 @@ impl HighlightingAssets {
|
|||||||
let path = Path::new(path_str);
|
let path = Path::new(path_str);
|
||||||
let line_syntax = self.get_first_line_syntax(&mut input.reader);
|
let line_syntax = self.get_first_line_syntax(&mut input.reader);
|
||||||
|
|
||||||
let absolute_path = path.canonicalize().ok().unwrap_or(path.to_owned());
|
let absolute_path = path.canonicalize().ok().unwrap_or_else(|| path.to_owned());
|
||||||
match mapping.get_syntax_for(absolute_path) {
|
match mapping.get_syntax_for(absolute_path) {
|
||||||
Some(MappingTarget::MapTo(syntax_name)) => {
|
Some(MappingTarget::MapTo(syntax_name)) => {
|
||||||
// TODO: we should probably return an error here if this syntax can not be
|
// TODO: we should probably return an error here if this syntax can not be
|
||||||
@ -221,7 +221,7 @@ impl HighlightingAssets {
|
|||||||
OpenedInputKind::StdIn | OpenedInputKind::CustomReader => {
|
OpenedInputKind::StdIn | OpenedInputKind::CustomReader => {
|
||||||
if let Some(ref name) = input.metadata.user_provided_name {
|
if let Some(ref name) = input.metadata.user_provided_name {
|
||||||
self.get_extension_syntax(&name)
|
self.get_extension_syntax(&name)
|
||||||
.or(self.get_first_line_syntax(&mut input.reader))
|
.or_else(|| self.get_first_line_syntax(&mut input.reader))
|
||||||
} else {
|
} else {
|
||||||
self.get_first_line_syntax(&mut input.reader)
|
self.get_first_line_syntax(&mut input.reader)
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ pub struct AssetsMetadata {
|
|||||||
creation_time: Option<SystemTime>,
|
creation_time: Option<SystemTime>,
|
||||||
}
|
}
|
||||||
|
|
||||||
const FILENAME: &'static str = "metadata.yaml";
|
const FILENAME: &str = "metadata.yaml";
|
||||||
|
|
||||||
impl AssetsMetadata {
|
impl AssetsMetadata {
|
||||||
pub(crate) fn new(current_version: &str) -> AssetsMetadata {
|
pub(crate) fn new(current_version: &str) -> AssetsMetadata {
|
||||||
|
@ -80,6 +80,7 @@ impl App {
|
|||||||
let paging_mode = match self.matches.value_of("paging") {
|
let paging_mode = match self.matches.value_of("paging") {
|
||||||
Some("always") => PagingMode::Always,
|
Some("always") => PagingMode::Always,
|
||||||
Some("never") => PagingMode::Never,
|
Some("never") => PagingMode::Never,
|
||||||
|
// FIXME: `_` will always cover in or patterns
|
||||||
Some("auto") | _ => {
|
Some("auto") | _ => {
|
||||||
if self.matches.occurrences_of("plain") > 1 {
|
if self.matches.occurrences_of("plain") > 1 {
|
||||||
// If we have -pp as an option when in auto mode, the pager should be disabled.
|
// If we have -pp as an option when in auto mode, the pager should be disabled.
|
||||||
@ -147,6 +148,7 @@ impl App {
|
|||||||
match self.matches.value_of("wrap") {
|
match self.matches.value_of("wrap") {
|
||||||
Some("character") => WrappingMode::Character,
|
Some("character") => WrappingMode::Character,
|
||||||
Some("never") => WrappingMode::NoWrapping,
|
Some("never") => WrappingMode::NoWrapping,
|
||||||
|
// FIXME: `_` will always cover in or patterns
|
||||||
Some("auto") | _ => {
|
Some("auto") | _ => {
|
||||||
if style_components.plain() {
|
if style_components.plain() {
|
||||||
WrappingMode::NoWrapping
|
WrappingMode::NoWrapping
|
||||||
@ -249,18 +251,18 @@ impl App {
|
|||||||
|
|
||||||
let mut filenames_or_none: Box<dyn Iterator<Item = _>> = match filenames {
|
let mut filenames_or_none: Box<dyn Iterator<Item = _>> = match filenames {
|
||||||
Some(ref filenames) => {
|
Some(ref filenames) => {
|
||||||
Box::new(filenames.into_iter().map(|name| Some(OsStr::new(*name))))
|
Box::new(filenames.iter().map(|name| Some(OsStr::new(*name))))
|
||||||
}
|
}
|
||||||
None => Box::new(std::iter::repeat(None)),
|
None => Box::new(std::iter::repeat(None)),
|
||||||
};
|
};
|
||||||
let files: Option<Vec<&OsStr>> = self.matches.values_of_os("FILE").map(|vs| vs.collect());
|
let files: Option<Vec<&OsStr>> = self.matches.values_of_os("FILE").map(|vs| vs.collect());
|
||||||
|
|
||||||
if files.is_none() {
|
if files.is_none() {
|
||||||
let input = Input::stdin().with_name(filenames_or_none.nth(0).unwrap_or(None));
|
let input = Input::stdin().with_name(filenames_or_none.next().unwrap_or(None));
|
||||||
return Ok(vec![input]);
|
return Ok(vec![input]);
|
||||||
}
|
}
|
||||||
let files_or_none: Box<dyn Iterator<Item = _>> = match files {
|
let files_or_none: Box<dyn Iterator<Item = _>> = match files {
|
||||||
Some(ref files) => Box::new(files.into_iter().map(|name| Some(*name))),
|
Some(ref files) => Box::new(files.iter().map(|name| Some(*name))),
|
||||||
None => Box::new(std::iter::repeat(None)),
|
None => Box::new(std::iter::repeat(None)),
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -274,7 +276,7 @@ impl App {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Ok(file_input);
|
Ok(file_input)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn style_components(&self) -> Result<StyleComponents> {
|
fn style_components(&self) -> Result<StyleComponents> {
|
||||||
|
@ -53,5 +53,5 @@ pub fn assets_from_cache_or_binary() -> Result<HighlightingAssets> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(HighlightingAssets::from_cache(&cache_dir).unwrap_or(HighlightingAssets::from_binary()))
|
Ok(HighlightingAssets::from_cache(&cache_dir).unwrap_or_else(|_| HighlightingAssets::from_binary()))
|
||||||
}
|
}
|
||||||
|
@ -180,7 +180,7 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
|
|||||||
t.parse::<i32>()
|
t.parse::<i32>()
|
||||||
.map_err(|_e| "must be an offset or number")
|
.map_err(|_e| "must be an offset or number")
|
||||||
.and_then(|v| if v == 0 && !is_offset {
|
.and_then(|v| if v == 0 && !is_offset {
|
||||||
Err("terminal width cannot be zero".into())
|
Err("terminal width cannot be zero")
|
||||||
} else {
|
} else {
|
||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
|
@ -35,10 +35,10 @@ pub fn generate_config_file() -> bat::error::Result<()> {
|
|||||||
match config_dir {
|
match config_dir {
|
||||||
Some(path) => fs::create_dir_all(path)?,
|
Some(path) => fs::create_dir_all(path)?,
|
||||||
None => {
|
None => {
|
||||||
return Ok(Err(format!(
|
return Err(format!(
|
||||||
"Unable to write config file to: {}",
|
"Unable to write config file to: {}",
|
||||||
config_file.to_string_lossy()
|
config_file.to_string_lossy()
|
||||||
))?)
|
).into());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -77,7 +77,7 @@ pub fn generate_config_file() -> bat::error::Result<()> {
|
|||||||
config_file.to_string_lossy()
|
config_file.to_string_lossy()
|
||||||
);
|
);
|
||||||
|
|
||||||
return Ok(());
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_args_from_config_file() -> Result<Vec<OsString>, shell_words::ParseError> {
|
pub fn get_args_from_config_file() -> Result<Vec<OsString>, shell_words::ParseError> {
|
||||||
|
@ -70,7 +70,7 @@ pub fn list_languages(config: &Config) -> Result<()> {
|
|||||||
|
|
||||||
if config.loop_through {
|
if config.loop_through {
|
||||||
for lang in languages {
|
for lang in languages {
|
||||||
write!(stdout, "{}:{}\n", lang.name, lang.file_extensions.join(","))?;
|
writeln!(stdout, "{}:{}", lang.name, lang.file_extensions.join(","))?;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let longest = languages
|
let longest = languages
|
||||||
|
@ -45,9 +45,9 @@ impl<'b> Controller<'b> {
|
|||||||
if self.config.paging_mode != PagingMode::Never {
|
if self.config.paging_mode != PagingMode::Never {
|
||||||
let call_pager = inputs.iter().any(|ref input| {
|
let call_pager = inputs.iter().any(|ref input| {
|
||||||
if let InputKind::OrdinaryFile(ref path) = input.kind {
|
if let InputKind::OrdinaryFile(ref path) = input.kind {
|
||||||
return Path::new(path).exists();
|
Path::new(path).exists()
|
||||||
} else {
|
} else {
|
||||||
return true;
|
true
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if !call_pager {
|
if !call_pager {
|
||||||
|
@ -30,7 +30,7 @@ impl LineRange {
|
|||||||
fn parse_range(range_raw: &str) -> Result<LineRange> {
|
fn parse_range(range_raw: &str) -> Result<LineRange> {
|
||||||
let mut new_range = LineRange::default();
|
let mut new_range = LineRange::default();
|
||||||
|
|
||||||
if range_raw.bytes().nth(0).ok_or("Empty line range")? == b':' {
|
if range_raw.bytes().next().ok_or("Empty line range")? == b':' {
|
||||||
new_range.upper = range_raw[1..].parse()?;
|
new_range.upper = range_raw[1..].parse()?;
|
||||||
return Ok(new_range);
|
return Ok(new_range);
|
||||||
} else if range_raw.bytes().last().ok_or("Empty line range")? == b':' {
|
} else if range_raw.bytes().last().ok_or("Empty line range")? == b':' {
|
||||||
|
@ -36,15 +36,12 @@ pub fn expand_tabs(line: &str, width: usize, cursor: &mut usize) -> String {
|
|||||||
fn try_parse_utf8_char(input: &[u8]) -> Option<(char, usize)> {
|
fn try_parse_utf8_char(input: &[u8]) -> Option<(char, usize)> {
|
||||||
let str_from_utf8 = |seq| std::str::from_utf8(seq).ok();
|
let str_from_utf8 = |seq| std::str::from_utf8(seq).ok();
|
||||||
|
|
||||||
let decoded = None
|
let decoded = input.get(0..1).and_then(str_from_utf8).map(|c| (c, 1))
|
||||||
.or(input.get(0..1).and_then(str_from_utf8).map(|c| (c, 1)))
|
.or_else(|| input.get(0..2).and_then(str_from_utf8).map(|c| (c, 2)))
|
||||||
.or(input.get(0..2).and_then(str_from_utf8).map(|c| (c, 2)))
|
.or_else(|| input.get(0..3).and_then(str_from_utf8).map(|c| (c, 3)))
|
||||||
.or(input.get(0..3).and_then(str_from_utf8).map(|c| (c, 3)))
|
.or_else(|| input.get(0..4).and_then(str_from_utf8).map(|c| (c, 4)));
|
||||||
.or(input.get(0..4).and_then(str_from_utf8).map(|c| (c, 4)));
|
|
||||||
|
|
||||||
let decoded_char = decoded.map(|(seq, n)| (seq.chars().next().unwrap(), n));
|
decoded.map(|(seq, n)| (seq.chars().next().unwrap(), n))
|
||||||
|
|
||||||
decoded_char
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn replace_nonprintable(input: &[u8], tab_width: usize) -> String {
|
pub fn replace_nonprintable(input: &[u8], tab_width: usize) -> String {
|
||||||
|
@ -202,7 +202,7 @@ impl<'a> InteractivePrinter<'a> {
|
|||||||
if self.config.style_components.grid() {
|
if self.config.style_components.grid() {
|
||||||
format!("{} │ ", text_filled)
|
format!("{} │ ", text_filled)
|
||||||
} else {
|
} else {
|
||||||
format!("{}", text_filled)
|
text_filled
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -229,10 +229,8 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
|||||||
Yellow.paint("[bat warning]"),
|
Yellow.paint("[bat warning]"),
|
||||||
input.description().full,
|
input.description().full,
|
||||||
)?;
|
)?;
|
||||||
} else {
|
} else if self.config.style_components.grid() {
|
||||||
if self.config.style_components.grid() {
|
self.print_horizontal_line(handle, '┬')?;
|
||||||
self.print_horizontal_line(handle, '┬')?;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
@ -304,9 +302,9 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
|||||||
let snip_right =
|
let snip_right =
|
||||||
" ─".repeat((self.config.term_width - panel_count - snip_left_count - title_count) / 2);
|
" ─".repeat((self.config.term_width - panel_count - snip_left_count - title_count) / 2);
|
||||||
|
|
||||||
write!(
|
writeln!(
|
||||||
handle,
|
handle,
|
||||||
"{}\n",
|
"{}",
|
||||||
self.colors
|
self.colors
|
||||||
.grid
|
.grid
|
||||||
.paint(format!("{}{}{}{}", panel, snip_left, title, snip_right))
|
.paint(format!("{}{}{}{}", panel, snip_left, title, snip_right))
|
||||||
|
@ -4,7 +4,7 @@ use bat::assets::HighlightingAssets;
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn no_duplicate_extensions() {
|
fn no_duplicate_extensions() {
|
||||||
const KNOWN_EXCEPTIONS: &[&'static str] = &[
|
const KNOWN_EXCEPTIONS: &[&str] = &[
|
||||||
// The '.h' extension currently appears in multiple syntaxes: C, C++, Objective C,
|
// The '.h' extension currently appears in multiple syntaxes: C, C++, Objective C,
|
||||||
// Objective C++
|
// Objective C++
|
||||||
"h",
|
"h",
|
||||||
|
Loading…
Reference in New Issue
Block a user