mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-27 19:17:43 +00:00
commit
583ed341a7
2 changed files with 21 additions and 16 deletions
|
@ -35,21 +35,14 @@ enum OutputFmt {
|
||||||
Unknown,
|
Unknown,
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! disp_err {
|
|
||||||
($($args:tt)+) => ({
|
|
||||||
pipe_write!(&mut ::std::io::stderr(), "{}: ", NAME);
|
|
||||||
pipe_writeln!(&mut ::std::io::stderr(), $($args)+);
|
|
||||||
pipe_writeln!(&mut ::std::io::stderr(), "Try '{} --help' for more information.", NAME);
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn guess_syntax() -> OutputFmt {
|
fn guess_syntax() -> OutputFmt {
|
||||||
|
use std::path;
|
||||||
match env::var("SHELL") {
|
match env::var("SHELL") {
|
||||||
Ok(s) => {
|
Ok(s) => {
|
||||||
if s.is_empty() {
|
if s.is_empty() {
|
||||||
return OutputFmt::Unknown;
|
return OutputFmt::Unknown;
|
||||||
}
|
}
|
||||||
if let Some(last) = s.rsplit('/').next() {
|
if let Some(last) = s.rsplit(path::MAIN_SEPARATOR).next() {
|
||||||
if last == "csh" || last == "tcsh" {
|
if last == "csh" || last == "tcsh" {
|
||||||
OutputFmt::CShell
|
OutputFmt::CShell
|
||||||
} else {
|
} else {
|
||||||
|
@ -79,7 +72,10 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
||||||
|
|
||||||
let matches = match opts.parse(&args[1..]) {
|
let matches = match opts.parse(&args[1..]) {
|
||||||
Ok(m) => m,
|
Ok(m) => m,
|
||||||
Err(f) => crash!(1, "Invalid options\n{}", f),
|
Err(f) => {
|
||||||
|
disp_err!("{}", f);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if matches.opt_present("help") {
|
if matches.opt_present("help") {
|
||||||
|
@ -175,13 +171,14 @@ For details on the format of these files, run 'dircolors --print-database'.",
|
||||||
}
|
}
|
||||||
|
|
||||||
trait StrUtils {
|
trait StrUtils {
|
||||||
|
/// Remove comments and trim whitespaces
|
||||||
fn purify(&self) -> &Self;
|
fn purify(&self) -> &Self;
|
||||||
|
/// Like split_whitespace() but only produce 2 components
|
||||||
fn split_two(&self) -> (&str, &str);
|
fn split_two(&self) -> (&str, &str);
|
||||||
fn fnmatch(&self, pattern: &str) -> bool;
|
fn fnmatch(&self, pattern: &str) -> bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StrUtils for str {
|
impl StrUtils for str {
|
||||||
/// Remove comments and trim whitespaces
|
|
||||||
fn purify(&self) -> &Self {
|
fn purify(&self) -> &Self {
|
||||||
let mut line = self;
|
let mut line = self;
|
||||||
for (n, c) in self.chars().enumerate() {
|
for (n, c) in self.chars().enumerate() {
|
||||||
|
@ -204,7 +201,6 @@ impl StrUtils for str {
|
||||||
line.trim()
|
line.trim()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Like split_whitespace() but only produce 2 components
|
|
||||||
fn split_two(&self) -> (&str, &str) {
|
fn split_two(&self) -> (&str, &str) {
|
||||||
if let Some(b) = self.find(char::is_whitespace) {
|
if let Some(b) = self.find(char::is_whitespace) {
|
||||||
let key = &self[..b];
|
let key = &self[..b];
|
||||||
|
@ -342,7 +338,7 @@ fn parse<T>(lines: T, fmt: OutputFmt, fp: &str) -> Result<String, String>
|
||||||
#[test]
|
#[test]
|
||||||
fn test_shell_syntax() {
|
fn test_shell_syntax() {
|
||||||
use std::env;
|
use std::env;
|
||||||
let last = env!("SHELL");
|
let last = env::var("SHELL");
|
||||||
env::set_var("SHELL", "/path/csh");
|
env::set_var("SHELL", "/path/csh");
|
||||||
assert_eq!(OutputFmt::CShell, guess_syntax());
|
assert_eq!(OutputFmt::CShell, guess_syntax());
|
||||||
env::set_var("SHELL", "csh");
|
env::set_var("SHELL", "csh");
|
||||||
|
@ -351,8 +347,6 @@ fn test_shell_syntax() {
|
||||||
assert_eq!(OutputFmt::Shell, guess_syntax());
|
assert_eq!(OutputFmt::Shell, guess_syntax());
|
||||||
env::set_var("SHELL", "bash");
|
env::set_var("SHELL", "bash");
|
||||||
assert_eq!(OutputFmt::Shell, guess_syntax());
|
assert_eq!(OutputFmt::Shell, guess_syntax());
|
||||||
env::set_var("SHELL", "bash");
|
|
||||||
assert_eq!(OutputFmt::Shell, guess_syntax());
|
|
||||||
env::set_var("SHELL", "/asd/bar");
|
env::set_var("SHELL", "/asd/bar");
|
||||||
assert_eq!(OutputFmt::Shell, guess_syntax());
|
assert_eq!(OutputFmt::Shell, guess_syntax());
|
||||||
env::set_var("SHELL", "foo");
|
env::set_var("SHELL", "foo");
|
||||||
|
@ -362,7 +356,9 @@ fn test_shell_syntax() {
|
||||||
env::remove_var("SHELL");
|
env::remove_var("SHELL");
|
||||||
assert_eq!(OutputFmt::Unknown, guess_syntax());
|
assert_eq!(OutputFmt::Unknown, guess_syntax());
|
||||||
|
|
||||||
env::set_var("SHELL", last);
|
if let Ok(s) = last {
|
||||||
|
env::set_var("SHELL", s);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
|
@ -43,6 +43,15 @@ macro_rules! show_info(
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! disp_err(
|
||||||
|
($($args:tt)+) => ({
|
||||||
|
pipe_write!(&mut ::std::io::stderr(), "{}: ", executable!());
|
||||||
|
pipe_writeln!(&mut ::std::io::stderr(), $($args)+);
|
||||||
|
pipe_writeln!(&mut ::std::io::stderr(), "Try '{} --help' for more information.", executable!());
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! eprint(
|
macro_rules! eprint(
|
||||||
($($args:tt)+) => (pipe_write!(&mut ::std::io::stderr(), $($args)+))
|
($($args:tt)+) => (pipe_write!(&mut ::std::io::stderr(), $($args)+))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue