Imported std::io::ErrorKind::* in rbcat.rs for prettier error matching
Implemented proper error handling for rbls, added support for multiple files and directories, added alphabetical sort
This commit is contained in:
parent
f780929dbf
commit
bf36e496c4
2 changed files with 83 additions and 29 deletions
|
@ -1,5 +1,5 @@
|
||||||
use std::{env, fs};
|
use std::{env, fs};
|
||||||
use std::io::{self, BufRead};
|
use std::io::{self, BufRead, ErrorKind::*};
|
||||||
|
|
||||||
fn main() -> Result<(), io::Error> {
|
fn main() -> Result<(), io::Error> {
|
||||||
if env::args().len() < 2 {
|
if env::args().len() < 2 {
|
||||||
|
@ -15,8 +15,8 @@ fn main() -> Result<(), io::Error> {
|
||||||
Ok(contents) => print!("{}", contents),
|
Ok(contents) => print!("{}", contents),
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
let msg = match error.kind() {
|
let msg = match error.kind() {
|
||||||
io::ErrorKind::PermissionDenied => "Permission denied",
|
PermissionDenied => "Permission denied",
|
||||||
io::ErrorKind::NotFound => "No such file or directory",
|
NotFound => "No such file or directory",
|
||||||
_ => "Unexpected I/O error"
|
_ => "Unexpected I/O error"
|
||||||
};
|
};
|
||||||
eprintln!("rbcat: {}: {}", file_name, msg);
|
eprintln!("rbcat: {}: {}", file_name, msg);
|
||||||
|
|
106
src/bin/rbls.rs
106
src/bin/rbls.rs
|
@ -1,27 +1,57 @@
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use console::Term;
|
use std::io::{self, ErrorKind::*};
|
||||||
use std::io::{self, Write};
|
|
||||||
use std::fs::{self, DirEntry, Metadata};
|
use std::fs::{self, DirEntry, Metadata};
|
||||||
|
|
||||||
fn main() -> Result<(), io::Error> {
|
// TODO: implement more options (-l, -i, etc.)
|
||||||
|
fn main() {
|
||||||
let cli = Cli::parse();
|
let cli = Cli::parse();
|
||||||
let file_infos = get_files_metadata(&cli.files[0])?;
|
let mut non_dirs = Vec::<FileInfo>::new();
|
||||||
let file_names = format_file_names(file_infos, cli.all, cli.almost_all);
|
let mut dirs = Vec::<DirInfo>::new();
|
||||||
let term = Term::stdout();
|
for file_name in &cli.files {
|
||||||
let output = if term.is_term() { format_term(file_names, term.size().1) }
|
let file_infos = match get_file_infos(&file_name) {
|
||||||
else { file_names.join(" ") };
|
Ok(file_infos) => file_infos,
|
||||||
let mut stdout = io::stdout().lock();
|
Err(err) => {
|
||||||
write!(stdout, "{}", output)?;
|
// NotADirectory is unstable feature, look at error code instead
|
||||||
writeln!(stdout)?;
|
if err.raw_os_error().unwrap() == 20 {
|
||||||
return Ok(());
|
let metadata = fs::metadata(&file_name).unwrap();
|
||||||
|
non_dirs.push(FileInfo::new(file_name, metadata));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let msg = match err.kind() {
|
||||||
|
PermissionDenied => "Permission denied",
|
||||||
|
NotFound => "No such file or directory",
|
||||||
|
_ => "Unexpected I/O error"
|
||||||
|
};
|
||||||
|
eprintln!("rbls: {}: {}", file_name, msg);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
dirs.push(DirInfo::new(file_name, file_infos));
|
||||||
|
}
|
||||||
|
if !non_dirs.is_empty() {
|
||||||
|
let non_dirs_filtered = transform_file_infos(non_dirs, &cli);
|
||||||
|
let non_dirs_output = format_output(non_dirs_filtered, &cli);
|
||||||
|
println!("{}", non_dirs_output);
|
||||||
|
}
|
||||||
|
let dirs_output = if dirs.len() > 1 {
|
||||||
|
dirs.into_iter()
|
||||||
|
.map(|dir| {
|
||||||
|
let files_filtered = transform_file_infos(dir.file_infos, &cli);
|
||||||
|
return dir.name + ":\n" + &format_output(files_filtered, &cli);
|
||||||
|
})
|
||||||
|
.collect::<Vec<String>>()
|
||||||
|
.join("\n\n")
|
||||||
|
} else {
|
||||||
|
let dir = dirs.remove(0);
|
||||||
|
let files_filtered = transform_file_infos(dir.file_infos, &cli);
|
||||||
|
format_output(files_filtered, &cli)
|
||||||
|
};
|
||||||
|
println!("{}", dirs_output);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_files_metadata(file_name: &str) -> Result<Vec<FileInfo>, io::Error> {
|
fn get_file_infos(file_name: impl AsRef<str>) -> io::Result<Vec<FileInfo>> {
|
||||||
let Ok(contents) = fs::read_dir(file_name)
|
let file_name = file_name.as_ref();
|
||||||
else {
|
let contents = fs::read_dir(file_name)?;
|
||||||
let file_info = FileInfo::new(file_name.to_string(), fs::metadata(file_name)?);
|
|
||||||
return Ok(vec![file_info]);
|
|
||||||
};
|
|
||||||
let mut file_infos = vec![
|
let mut file_infos = vec![
|
||||||
FileInfo::new(".".to_string(), fs::metadata(".")?),
|
FileInfo::new(".".to_string(), fs::metadata(".")?),
|
||||||
FileInfo::new("..".to_string(), fs::metadata("..")?),
|
FileInfo::new("..".to_string(), fs::metadata("..")?),
|
||||||
|
@ -32,15 +62,19 @@ fn get_files_metadata(file_name: &str) -> Result<Vec<FileInfo>, io::Error> {
|
||||||
return Ok(file_infos);
|
return Ok(file_infos);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn format_file_names(file_infos: Vec<FileInfo>, show_all: bool, show_almost_all: bool) -> Vec<String> {
|
fn transform_file_infos(file_infos: Vec<FileInfo>, options: &Cli) -> Vec<String> {
|
||||||
return file_infos.into_iter()
|
let mut filtered_file_infos = file_infos.into_iter()
|
||||||
.filter(|file_info| {
|
.filter(|file_info| {
|
||||||
if show_all { return true; }
|
if options.all { return true; }
|
||||||
if show_almost_all {
|
if options.almost_all {
|
||||||
return file_info.name != "." && file_info.name != "..";
|
return file_info.name != "." && file_info.name != "..";
|
||||||
}
|
}
|
||||||
return !file_info.name.starts_with(".");
|
return !file_info.name.starts_with(".");
|
||||||
})
|
})
|
||||||
|
.collect::<Vec<FileInfo>>();
|
||||||
|
// sort alphabetically
|
||||||
|
filtered_file_infos.sort_by(|fi1, fi2| fi1.name.to_lowercase().cmp(&fi2.name.to_lowercase()));
|
||||||
|
return filtered_file_infos.into_iter()
|
||||||
.map(|file_info| {
|
.map(|file_info| {
|
||||||
let mut file_name = file_info.name;
|
let mut file_name = file_info.name;
|
||||||
if file_info.metadata.is_dir() { file_name.push('/') }
|
if file_info.metadata.is_dir() { file_name.push('/') }
|
||||||
|
@ -49,7 +83,13 @@ fn format_file_names(file_infos: Vec<FileInfo>, show_all: bool, show_almost_all:
|
||||||
.collect();
|
.collect();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn format_term(mut file_names: Vec<String>, term_width: u16) -> String {
|
fn format_output(file_names: Vec<String>, options: &Cli) -> String {
|
||||||
|
let term = console::Term::stdout();
|
||||||
|
if term.is_term() { return format_tabular(file_names, options, term.size().1); }
|
||||||
|
else { return file_names.join(" "); }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn format_tabular(mut file_names: Vec<String>, options: &Cli, term_width: u16) -> String {
|
||||||
let max_file_name_len = file_names.iter().map(|n| n.len()).max().unwrap();
|
let max_file_name_len = file_names.iter().map(|n| n.len()).max().unwrap();
|
||||||
let elem_width = max_file_name_len + 2;
|
let elem_width = max_file_name_len + 2;
|
||||||
let row_elems_amount = (term_width as usize / elem_width).max(1);
|
let row_elems_amount = (term_width as usize / elem_width).max(1);
|
||||||
|
@ -60,12 +100,20 @@ fn format_term(mut file_names: Vec<String>, term_width: u16) -> String {
|
||||||
return file_names.join("").trim_end().to_string();
|
return file_names.join("").trim_end().to_string();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct DirInfo {
|
||||||
|
name: String, file_infos: Vec<FileInfo>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DirInfo {
|
||||||
|
fn new(name: impl Into<String>, file_infos: Vec<FileInfo>) -> Self { Self { name: name.into(), file_infos } }
|
||||||
|
}
|
||||||
|
|
||||||
struct FileInfo {
|
struct FileInfo {
|
||||||
name: String, metadata: Metadata
|
name: String, metadata: Metadata
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FileInfo {
|
impl FileInfo {
|
||||||
fn new(name: String, metadata: Metadata) -> Self { Self { name, metadata } }
|
fn new(name: impl Into<String>, metadata: Metadata) -> Self { Self { name: name.into(), metadata } }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<DirEntry> for FileInfo {
|
impl TryFrom<DirEntry> for FileInfo {
|
||||||
|
@ -77,7 +125,7 @@ impl TryFrom<DirEntry> for FileInfo {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<Result<DirEntry, io::Error>> for FileInfo {
|
impl TryFrom<io::Result<DirEntry>> for FileInfo {
|
||||||
type Error = io::Error;
|
type Error = io::Error;
|
||||||
|
|
||||||
fn try_from(de: Result<DirEntry, io::Error>) -> Result<Self, Self::Error> { Self::try_from(de?) }
|
fn try_from(de: Result<DirEntry, io::Error>) -> Result<Self, Self::Error> { Self::try_from(de?) }
|
||||||
|
@ -93,5 +141,11 @@ struct Cli {
|
||||||
all: bool,
|
all: bool,
|
||||||
|
|
||||||
#[arg(short = 'A', long, help = "Same as --all, but doesn't list implied . and ..")]
|
#[arg(short = 'A', long, help = "Same as --all, but doesn't list implied . and ..")]
|
||||||
almost_all: bool
|
almost_all: bool,
|
||||||
|
|
||||||
|
#[arg(short, long, help = "Use a long listing format")]
|
||||||
|
long: bool,
|
||||||
|
|
||||||
|
#[arg(short, long, help = "Print the index number of each file")]
|
||||||
|
inode: bool
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue