1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-01 21:47:46 +00:00

Make a nice error when file does not exist

This commit is contained in:
Arijit Dey 2021-05-06 22:10:32 +05:30
parent 482e340e11
commit d2ab0dcded
No known key found for this signature in database
GPG key ID: D35625157C79D6B5
2 changed files with 29 additions and 12 deletions

View file

@ -19,6 +19,7 @@ clap = "2.33"
uucore = { version = ">=0.0.7", package = "uucore", path = "../../uucore" }
uucore_procs = { version = ">=0.0.5", package = "uucore_procs", path = "../../uucore_procs" }
crossterm = ">=0.19"
atty = "*"
[target.'cfg(target_os = "redox")'.dependencies]
redox_termios = "0.1"

View file

@ -24,7 +24,7 @@ extern crate nix;
use clap::{App, Arg};
use crossterm::{
event::{self, Event, KeyCode, KeyEvent, KeyModifiers},
execute,
execute, queue,
style::Attribute,
terminal,
};
@ -134,16 +134,26 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.get_matches_from(args);
let mut buff = String::new();
let mut stdout = setup_term();
if let Some(filenames) = matches.values_of(options::FILES) {
let length = filenames.len();
for (idx, fname) in filenames.clone().enumerate() {
if Path::new(fname).is_dir() {
show_usage_error!("'{}' is a directory.", fname);
let fname = Path::new(fname);
if fname.is_dir() {
terminal::disable_raw_mode().unwrap();
show_usage_error!("'{}' is a directory.", fname.display());
return 1;
}
if !fname.exists() {
terminal::disable_raw_mode().unwrap();
eprintln!(
"{}: cannot open {}: No such file or directory",
executable!(),
fname.display()
);
return 1;
}
if filenames.len() > 1 {
buff.push_str(&MULTI_FILE_TOP_PROMPT.replace("{}", fname));
buff.push_str(&MULTI_FILE_TOP_PROMPT.replace("{}", fname.to_str().unwrap()));
}
let mut reader = BufReader::new(File::open(fname).unwrap());
reader.read_to_string(&mut buff).unwrap();
@ -151,19 +161,23 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
more(&buff, &mut stdout, last);
buff.clear();
}
reset_term(&mut stdout);
} else {
stdin().read_to_string(&mut buff).unwrap();
more(&buff, &mut stdout, true);
if atty::isnt(atty::Stream::Stdin) {
let mut stdout = setup_term();
stdin().read_to_string(&mut buff).unwrap();
more(&buff, &mut stdout, true);
} else {
show_usage_error!("bad usage");
}
}
0
}
#[cfg(not(target_os = "fuchsia"))]
fn setup_term() -> std::io::Stdout {
let mut stdout = stdout();
let stdout = stdout();
terminal::enable_raw_mode().unwrap();
// Change this to a queue if more commands are executed to avoid too many writes to the terminal
execute!(stdout, terminal::Clear(terminal::ClearType::All)).unwrap();
stdout
}
@ -177,8 +191,10 @@ fn setup_term() -> usize {
fn reset_term(stdout: &mut std::io::Stdout) {
terminal::disable_raw_mode().unwrap();
// Clear the prompt
execute!(stdout, terminal::Clear(terminal::ClearType::CurrentLine)).unwrap();
println!("\r");
queue!(stdout, terminal::Clear(terminal::ClearType::CurrentLine)).unwrap();
// Move cursor to the beginning without printing new line
print!("\r");
stdout.flush().unwrap();
}
#[cfg(target_os = "fuchsia")]