From 6ed8a04c24e75f51a8e014fed7cb87651d1fcb90 Mon Sep 17 00:00:00 2001 From: emma Date: Tue, 11 Apr 2023 19:15:38 -0400 Subject: [PATCH] It now opens to an empty buffer when there is no stdin --- Cargo.lock | 1 + Cargo.toml | 1 + src/main.rs | 7 ++++++- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index cab7774..eb031fb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -62,6 +62,7 @@ version = "0.1.0" dependencies = [ "arg", "crossterm", + "libc", "ropey", "yacexits", ] diff --git a/Cargo.toml b/Cargo.toml index 9dc3429..9ec3ecf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,5 +7,6 @@ license = "AGPL-3.0-or-later" [dependencies] arg = "0.4.1" crossterm = "0.26" +libc = "0.2.141" ropey = "1.6" yacexits = "0.1.5" diff --git a/src/main.rs b/src/main.rs index 0ef1d36..883ac31 100644 --- a/src/main.rs +++ b/src/main.rs @@ -357,7 +357,12 @@ fn main() -> Result<()> { let mut buf = Vec::new(); match argv.get(1).map(|s| s.as_str()) { - Some("-") | None => unsafe { File::from_raw_fd(0) }, // stdin as a file + Some("-") | None => { + let stdin = 0; // get stdin as a file descriptor + if unsafe { libc::isatty(stdin) } == 0 { + unsafe { File::from_raw_fd(stdin) } + } else { File::open("/dev/null").unwrap() } + }, Some(path) => { std::fs::File::open(path).unwrap_or_else(|_| { eprintln!(