mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-09-13 18:47:58 +00:00
dircolors: split the code to remove the clippy warning
This commit is contained in:
parent
1db2e2356a
commit
591cdc1d31
1 changed files with 67 additions and 55 deletions
|
@ -8,7 +8,6 @@
|
|||
use std::borrow::Borrow;
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
//use std::io::IsTerminal;
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::path::Path;
|
||||
|
||||
|
@ -360,8 +359,6 @@ enum ParseState {
|
|||
}
|
||||
|
||||
use uucore::{format_usage, parse_glob};
|
||||
|
||||
#[allow(clippy::cognitive_complexity)]
|
||||
fn parse<T>(user_input: T, fmt: &OutputFmt, fp: &str) -> Result<String, String>
|
||||
where
|
||||
T: IntoIterator,
|
||||
|
@ -372,11 +369,12 @@ where
|
|||
|
||||
result.push_str(&prefix);
|
||||
|
||||
// Get environment variables once at the start
|
||||
let term = env::var("TERM").unwrap_or_else(|_| "none".to_owned());
|
||||
let term = term.as_str();
|
||||
let colorterm = env::var("COLORTERM").unwrap_or_default();
|
||||
|
||||
let mut state = ParseState::Global;
|
||||
let mut saw_colorterm_match = false;
|
||||
|
||||
for (num, line) in user_input.into_iter().enumerate() {
|
||||
let num = num + 1;
|
||||
|
@ -396,64 +394,38 @@ where
|
|||
num
|
||||
));
|
||||
}
|
||||
let lower = key.to_lowercase();
|
||||
|
||||
if lower == "term" || lower == "colorterm" {
|
||||
let should_match = if lower == "colorterm" {
|
||||
let lower = key.to_lowercase();
|
||||
match lower.as_str() {
|
||||
"term" => {
|
||||
if term.fnmatch(val) {
|
||||
state = ParseState::Matched;
|
||||
} else if state == ParseState::Global {
|
||||
state = ParseState::Pass;
|
||||
}
|
||||
}
|
||||
"colorterm" => {
|
||||
// For COLORTERM ?*, only match if COLORTERM is non-empty
|
||||
if val == "?*" {
|
||||
let matches = if val == "?*" {
|
||||
!colorterm.is_empty()
|
||||
} else {
|
||||
colorterm.fnmatch(val)
|
||||
}
|
||||
} else {
|
||||
term.fnmatch(val)
|
||||
};
|
||||
|
||||
if should_match {
|
||||
if matches {
|
||||
state = ParseState::Matched;
|
||||
} else if state != ParseState::Matched {
|
||||
saw_colorterm_match = true;
|
||||
} else if !saw_colorterm_match && state == ParseState::Global {
|
||||
state = ParseState::Pass;
|
||||
}
|
||||
} else {
|
||||
}
|
||||
_ => {
|
||||
if state == ParseState::Matched {
|
||||
// prevent subsequent mismatched TERM from
|
||||
// cancelling the input
|
||||
state = ParseState::Continue;
|
||||
}
|
||||
if state != ParseState::Pass {
|
||||
let search_key = lower.as_str();
|
||||
|
||||
if key.starts_with('.') {
|
||||
if *fmt == OutputFmt::Display {
|
||||
result.push_str(format!("\x1b[{val}m*{key}\t{val}\x1b[0m\n").as_str());
|
||||
} else {
|
||||
result.push_str(format!("*{key}={val}:").as_str());
|
||||
}
|
||||
} else if key.starts_with('*') {
|
||||
if *fmt == OutputFmt::Display {
|
||||
result.push_str(format!("\x1b[{val}m{key}\t{val}\x1b[0m\n").as_str());
|
||||
} else {
|
||||
result.push_str(format!("{key}={val}:").as_str());
|
||||
}
|
||||
} else if lower == "options" || lower == "color" || lower == "eightbit" {
|
||||
// Slackware only. Ignore
|
||||
} else if let Some((_, s)) = FILE_ATTRIBUTE_CODES
|
||||
.iter()
|
||||
.find(|&&(key, _)| key == search_key)
|
||||
{
|
||||
if *fmt == OutputFmt::Display {
|
||||
result.push_str(format!("\x1b[{val}m{s}\t{val}\x1b[0m\n").as_str());
|
||||
} else {
|
||||
result.push_str(format!("{s}={val}:").as_str());
|
||||
}
|
||||
} else {
|
||||
return Err(format!(
|
||||
"{}:{}: unrecognized keyword {}",
|
||||
fp.maybe_quote(),
|
||||
num,
|
||||
key
|
||||
));
|
||||
append_entry(&mut result, fmt, key, &lower, val)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -468,6 +440,46 @@ where
|
|||
Ok(result)
|
||||
}
|
||||
|
||||
fn append_entry(
|
||||
result: &mut String,
|
||||
fmt: &OutputFmt,
|
||||
key: &str,
|
||||
lower: &str,
|
||||
val: &str,
|
||||
) -> Result<(), String> {
|
||||
if key.starts_with(['.', '*']) {
|
||||
let entry = if key.starts_with('.') {
|
||||
format!("*{key}")
|
||||
} else {
|
||||
key.to_string()
|
||||
};
|
||||
let disp = if *fmt == OutputFmt::Display {
|
||||
format!("\x1b[{val}m{entry}\t{val}\x1b[0m\n")
|
||||
} else {
|
||||
format!("{entry}={val}:")
|
||||
};
|
||||
result.push_str(&disp);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
match lower {
|
||||
"options" | "color" | "eightbit" => Ok(()), // Slackware only, ignore
|
||||
_ => {
|
||||
if let Some((_, s)) = FILE_ATTRIBUTE_CODES.iter().find(|&&(key, _)| key == lower) {
|
||||
let disp = if *fmt == OutputFmt::Display {
|
||||
format!("\x1b[{val}m{s}\t{val}\x1b[0m\n")
|
||||
} else {
|
||||
format!("{s}={val}:")
|
||||
};
|
||||
result.push_str(&disp);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(format!("unrecognized keyword {key}"))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Escape single quotes because they are not allowed between single quotes in shell code, and code
|
||||
/// enclosed by single quotes is what is returned by `parse()`.
|
||||
///
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue