mirror of
https://github.com/sharkdp/bat.git
synced 2025-03-01 08:18:25 +00:00
Applied linter fixes
This commit is contained in:
parent
a21ae614e6
commit
82f14121bd
26
src/app.rs
26
src/app.rs
@ -120,7 +120,7 @@ impl App {
|
|||||||
|
|
||||||
// Read arguments from bats config file
|
// Read arguments from bats config file
|
||||||
let mut args = get_args_from_env_var()
|
let mut args = get_args_from_env_var()
|
||||||
.unwrap_or_else(|| get_args_from_config_file())
|
.unwrap_or_else(get_args_from_config_file)
|
||||||
.chain_err(|| "Could not parse configuration file")?;
|
.chain_err(|| "Could not parse configuration file")?;
|
||||||
|
|
||||||
// Put the zero-th CLI argument (program name) first
|
// Put the zero-th CLI argument (program name) first
|
||||||
@ -152,12 +152,10 @@ impl App {
|
|||||||
} else {
|
} else {
|
||||||
PagingMode::Never
|
PagingMode::Never
|
||||||
}
|
}
|
||||||
|
} else if self.interactive_output {
|
||||||
|
PagingMode::QuitIfOneScreen
|
||||||
} else {
|
} else {
|
||||||
if self.interactive_output {
|
PagingMode::Never
|
||||||
PagingMode::QuitIfOneScreen
|
|
||||||
} else {
|
|
||||||
PagingMode::Never
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -166,7 +164,7 @@ impl App {
|
|||||||
|
|
||||||
if let Some(values) = self.matches.values_of("map-syntax") {
|
if let Some(values) = self.matches.values_of("map-syntax") {
|
||||||
for from_to in values {
|
for from_to in values {
|
||||||
let parts: Vec<_> = from_to.split(":").collect();
|
let parts: Vec<_> = from_to.split(':').collect();
|
||||||
|
|
||||||
if parts.len() != 2 {
|
if parts.len() != 2 {
|
||||||
return Err("Invalid syntax mapping. The format of the -m/--map-syntax option is 'from:to'.".into());
|
return Err("Invalid syntax mapping. The format of the -m/--map-syntax option is 'from:to'.".into());
|
||||||
@ -213,11 +211,11 @@ impl App {
|
|||||||
.matches
|
.matches
|
||||||
.value_of("terminal-width")
|
.value_of("terminal-width")
|
||||||
.and_then(|w| {
|
.and_then(|w| {
|
||||||
if w.starts_with("+") || w.starts_with("-") {
|
if w.starts_with('+') || w.starts_with('-') {
|
||||||
// Treat argument as a delta to the current terminal width
|
// Treat argument as a delta to the current terminal width
|
||||||
w.parse().ok().map(|delta: i16| {
|
w.parse().ok().map(|delta: i16| {
|
||||||
let old_width: u16 = Term::stdout().size().1;
|
let old_width: u16 = Term::stdout().size().1;
|
||||||
let new_width: i32 = old_width as i32 + delta as i32;
|
let new_width: i32 = i32::from(old_width) + i32::from(delta);
|
||||||
|
|
||||||
if new_width <= 0 {
|
if new_width <= 0 {
|
||||||
old_width as usize
|
old_width as usize
|
||||||
@ -252,14 +250,14 @@ impl App {
|
|||||||
.value_of("theme")
|
.value_of("theme")
|
||||||
.map(String::from)
|
.map(String::from)
|
||||||
.or_else(|| env::var("BAT_THEME").ok())
|
.or_else(|| env::var("BAT_THEME").ok())
|
||||||
.unwrap_or(String::from(BAT_THEME_DEFAULT)),
|
.unwrap_or_else(|| String::from(BAT_THEME_DEFAULT)),
|
||||||
line_ranges: LineRanges::from(
|
line_ranges: LineRanges::from(
|
||||||
transpose(
|
transpose(
|
||||||
self.matches
|
self.matches
|
||||||
.values_of("line-range")
|
.values_of("line-range")
|
||||||
.map(|vs| vs.map(LineRange::from).collect()),
|
.map(|vs| vs.map(LineRange::from).collect()),
|
||||||
)?
|
)?
|
||||||
.unwrap_or(vec![]),
|
.unwrap_or_else(|| vec![]),
|
||||||
),
|
),
|
||||||
output_components,
|
output_components,
|
||||||
syntax_mapping,
|
syntax_mapping,
|
||||||
@ -306,7 +304,7 @@ impl App {
|
|||||||
let env_style_components: Option<Vec<OutputComponent>> =
|
let env_style_components: Option<Vec<OutputComponent>> =
|
||||||
transpose(env::var("BAT_STYLE").ok().map(|style_str| {
|
transpose(env::var("BAT_STYLE").ok().map(|style_str| {
|
||||||
style_str
|
style_str
|
||||||
.split(",")
|
.split(',')
|
||||||
.map(|x| OutputComponent::from_str(&x))
|
.map(|x| OutputComponent::from_str(&x))
|
||||||
.collect::<Result<Vec<OutputComponent>>>()
|
.collect::<Result<Vec<OutputComponent>>>()
|
||||||
}))?;
|
}))?;
|
||||||
@ -315,13 +313,13 @@ impl App {
|
|||||||
.value_of("style")
|
.value_of("style")
|
||||||
.map(|styles| {
|
.map(|styles| {
|
||||||
styles
|
styles
|
||||||
.split(",")
|
.split(',')
|
||||||
.map(|style| style.parse::<OutputComponent>())
|
.map(|style| style.parse::<OutputComponent>())
|
||||||
.filter_map(|style| style.ok())
|
.filter_map(|style| style.ok())
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
})
|
})
|
||||||
.or(env_style_components)
|
.or(env_style_components)
|
||||||
.unwrap_or(vec![OutputComponent::Full])
|
.unwrap_or_else(|| vec![OutputComponent::Full])
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|style| style.components(self.interactive_output))
|
.map(|style| style.components(self.interactive_output))
|
||||||
.fold(HashSet::new(), |mut acc, components| {
|
.fold(HashSet::new(), |mut acc, components| {
|
||||||
|
@ -40,7 +40,7 @@ impl HighlightingAssets {
|
|||||||
let theme_dir = source_dir.join("themes");
|
let theme_dir = source_dir.join("themes");
|
||||||
|
|
||||||
let res = theme_set.add_from_folder(&theme_dir);
|
let res = theme_set.add_from_folder(&theme_dir);
|
||||||
if !res.is_ok() {
|
if res.is_err() {
|
||||||
println!(
|
println!(
|
||||||
"No themes were found in '{}', using the default set",
|
"No themes were found in '{}', using the default set",
|
||||||
theme_dir.to_string_lossy()
|
theme_dir.to_string_lossy()
|
||||||
@ -191,8 +191,8 @@ impl HighlightingAssets {
|
|||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
let syntax = ext_syntax.or(line_syntax);
|
|
||||||
syntax
|
ext_syntax.or(line_syntax)
|
||||||
}
|
}
|
||||||
(None, InputFile::StdIn) => String::from_utf8(reader.first_line.clone())
|
(None, InputFile::StdIn) => String::from_utf8(reader.first_line.clone())
|
||||||
.ok()
|
.ok()
|
||||||
|
@ -109,7 +109,7 @@ pub fn build_app(interactive_output: bool) -> ClapApp<'static, 'static> {
|
|||||||
.overrides_with("number")
|
.overrides_with("number")
|
||||||
// Cannot use clap's built in validation because we have to turn off clap's delimiters
|
// Cannot use clap's built in validation because we have to turn off clap's delimiters
|
||||||
.validator(|val| {
|
.validator(|val| {
|
||||||
let mut invalid_vals = val.split(",").filter(|style| {
|
let mut invalid_vals = val.split(',').filter(|style| {
|
||||||
!&[
|
!&[
|
||||||
"auto", "full", "plain", "changes", "header", "grid", "numbers",
|
"auto", "full", "plain", "changes", "header", "grid", "numbers",
|
||||||
]
|
]
|
||||||
|
@ -13,7 +13,7 @@ pub fn config_file() -> PathBuf {
|
|||||||
.ok()
|
.ok()
|
||||||
.map(PathBuf::from)
|
.map(PathBuf::from)
|
||||||
.filter(|config_path| config_path.is_file())
|
.filter(|config_path| config_path.is_file())
|
||||||
.unwrap_or(PROJECT_DIRS.config_dir().join("config"))
|
.unwrap_or_else(|| PROJECT_DIRS.config_dir().join("config"))
|
||||||
}
|
}
|
||||||
|
|
||||||
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> {
|
||||||
@ -22,19 +22,19 @@ pub fn get_args_from_config_file() -> Result<Vec<OsString>, shell_words::ParseEr
|
|||||||
.ok()
|
.ok()
|
||||||
.map(|content| get_args_from_str(&content)),
|
.map(|content| get_args_from_str(&content)),
|
||||||
)?
|
)?
|
||||||
.unwrap_or(vec![]))
|
.unwrap_or_else(|| vec![]))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_args_from_env_var() -> Option<Result<Vec<OsString>, shell_words::ParseError>> {
|
pub fn get_args_from_env_var() -> Option<Result<Vec<OsString>, shell_words::ParseError>> {
|
||||||
env::var("BAT_OPTS").ok().map(|s| get_args_from_str(&s))
|
env::var("BAT_OPTS").ok().map(|s| get_args_from_str(&s))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_args_from_str<'a>(content: &'a str) -> Result<Vec<OsString>, shell_words::ParseError> {
|
fn get_args_from_str(content: &str) -> Result<Vec<OsString>, shell_words::ParseError> {
|
||||||
let args_per_line = content
|
let args_per_line = content
|
||||||
.split('\n')
|
.split('\n')
|
||||||
.map(|line| line.trim())
|
.map(|line| line.trim())
|
||||||
.filter(|line| !line.is_empty())
|
.filter(|line| !line.is_empty())
|
||||||
.filter(|line| !line.starts_with("#"))
|
.filter(|line| !line.starts_with('#'))
|
||||||
.map(|line| shell_words::split(line))
|
.map(|line| shell_words::split(line))
|
||||||
.collect::<Result<Vec<_>, _>>()?;
|
.collect::<Result<Vec<_>, _>>()?;
|
||||||
|
|
||||||
|
@ -88,7 +88,7 @@ impl<'b> Controller<'b> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_file_ranges<'a, P: Printer>(
|
fn print_file_ranges<P: Printer>(
|
||||||
&self,
|
&self,
|
||||||
printer: &mut P,
|
printer: &mut P,
|
||||||
writer: &mut Write,
|
writer: &mut Write,
|
||||||
|
@ -109,18 +109,12 @@ impl LineRanges {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn check(&self, line: usize) -> RangeCheckResult {
|
pub fn check(&self, line: usize) -> RangeCheckResult {
|
||||||
if self.ranges.is_empty() {
|
if self.ranges.is_empty() | self.ranges.iter().any(|r| r.is_inside(line)) {
|
||||||
RangeCheckResult::InRange
|
RangeCheckResult::InRange
|
||||||
|
} else if line < self.largest_upper_bound {
|
||||||
|
RangeCheckResult::OutsideRange
|
||||||
} else {
|
} else {
|
||||||
if self.ranges.iter().any(|r| r.is_inside(line)) {
|
RangeCheckResult::AfterLastRange
|
||||||
RangeCheckResult::InRange
|
|
||||||
} else {
|
|
||||||
if line < self.largest_upper_bound {
|
|
||||||
RangeCheckResult::OutsideRange
|
|
||||||
} else {
|
|
||||||
RangeCheckResult::AfterLastRange
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -67,7 +67,7 @@ mod errors {
|
|||||||
|
|
||||||
pub fn handle_error(error: &Error) {
|
pub fn handle_error(error: &Error) {
|
||||||
match error {
|
match error {
|
||||||
&Error(ErrorKind::Io(ref io_error), _)
|
Error(ErrorKind::Io(ref io_error), _)
|
||||||
if io_error.kind() == super::io::ErrorKind::BrokenPipe =>
|
if io_error.kind() == super::io::ErrorKind::BrokenPipe =>
|
||||||
{
|
{
|
||||||
super::process::exit(0);
|
super::process::exit(0);
|
||||||
|
@ -51,7 +51,7 @@ impl OutputType {
|
|||||||
|
|
||||||
let pager = pager_from_config
|
let pager = pager_from_config
|
||||||
.or(pager_from_env)
|
.or(pager_from_env)
|
||||||
.unwrap_or(String::from("less"));
|
.unwrap_or_else(|| String::from("less"));
|
||||||
|
|
||||||
let pagerflags =
|
let pagerflags =
|
||||||
shell_words::split(&pager).chain_err(|| "Could not parse pager command.")?;
|
shell_words::split(&pager).chain_err(|| "Could not parse pager command.")?;
|
||||||
|
@ -64,7 +64,7 @@ impl Printer for SimplePrinter {
|
|||||||
line_buffer: &[u8],
|
line_buffer: &[u8],
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
if !out_of_range {
|
if !out_of_range {
|
||||||
handle.write(line_buffer)?;
|
handle.write_all(line_buffer)?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -270,7 +270,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if self.config.show_nonprintable {
|
if self.config.show_nonprintable {
|
||||||
line = replace_nonprintable(&mut line, self.config.tab_width);
|
line = replace_nonprintable(&line, self.config.tab_width);
|
||||||
}
|
}
|
||||||
|
|
||||||
let regions = {
|
let regions = {
|
||||||
@ -355,11 +355,11 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if line.bytes().next_back() != Some(b'\n') {
|
if line.bytes().next_back() != Some(b'\n') {
|
||||||
write!(handle, "\n")?;
|
writeln!(handle)?;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for &(style, region) in regions.iter() {
|
for &(style, region) in regions.iter() {
|
||||||
let mut ansi_iterator = AnsiCodeIterator::new(region);
|
let ansi_iterator = AnsiCodeIterator::new(region);
|
||||||
let mut ansi_prefix: String = String::new();
|
let mut ansi_prefix: String = String::new();
|
||||||
for chunk in ansi_iterator {
|
for chunk in ansi_iterator {
|
||||||
match chunk {
|
match chunk {
|
||||||
@ -472,7 +472,7 @@ impl<'a> Printer for InteractivePrinter<'a> {
|
|||||||
ansi_style.paint(" ".repeat(cursor_max - cursor))
|
ansi_style.paint(" ".repeat(cursor_max - cursor))
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
write!(handle, "\n")?;
|
writeln!(handle)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -21,8 +21,8 @@ pub enum OutputWrap {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl OutputComponent {
|
impl OutputComponent {
|
||||||
pub fn components(&self, interactive_terminal: bool) -> &'static [OutputComponent] {
|
pub fn components(self, interactive_terminal: bool) -> &'static [OutputComponent] {
|
||||||
match *self {
|
match self {
|
||||||
OutputComponent::Auto => {
|
OutputComponent::Auto => {
|
||||||
if interactive_terminal {
|
if interactive_terminal {
|
||||||
OutputComponent::Full.components(interactive_terminal)
|
OutputComponent::Full.components(interactive_terminal)
|
||||||
|
@ -1,14 +1,15 @@
|
|||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone, Default)]
|
||||||
pub struct SyntaxMapping(HashMap<String, String>);
|
pub struct SyntaxMapping(HashMap<String, String>);
|
||||||
|
|
||||||
impl SyntaxMapping {
|
impl SyntaxMapping {
|
||||||
pub fn new() -> SyntaxMapping {
|
pub fn new() -> SyntaxMapping {
|
||||||
SyntaxMapping(HashMap::new())
|
Default::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn insert(&mut self, from: impl Into<String>, to: impl Into<String>) -> Option<String> {
|
pub fn insert(&mut self, from: impl Into<String>, to: impl Into<String>) -> Option<String> {
|
||||||
self.0.insert(from.into(), to.into())
|
self.0.insert(from.into(), to.into())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user