Skip to content

Commit

Permalink
fix: handle broken pipe errors gracefully (#2110)
Browse files Browse the repository at this point in the history
Co-authored-by: sxyazi <[email protected]>
  • Loading branch information
wfxr and sxyazi authored Dec 30, 2024
1 parent fd21452 commit 00e8adc
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cspell.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"flagWords":[],"words":["Punct","KEYMAP","splitn","crossterm","YAZI","unar","peekable","ratatui","syntect","pbpaste","pbcopy","oneshot","Posix","Lsar","XADDOS","zoxide","cands","Deque","precache","imageops","IFBLK","IFCHR","IFDIR","IFIFO","IFLNK","IFMT","IFSOCK","IRGRP","IROTH","IRUSR","ISGID","ISUID","ISVTX","IWGRP","IWOTH","IWUSR","IXGRP","IXOTH","IXUSR","libc","winsize","TIOCGWINSZ","xpixel","ypixel","ioerr","appender","Catppuccin","macchiato","gitmodules","Dotfiles","bashprofile","vimrc","flac","webp","exiftool","mediainfo","ripgrep","nvim","indexmap","indexmap","unwatch","canonicalize","serde","fsevent","Ueberzug","iterm","wezterm","sixel","chafa","ueberzugpp","️ Überzug","️ Überzug","Konsole","Alacritty","Überzug","pkgs","paru","unarchiver","pdftoppm","poppler","prebuild","singlefile","jpegopt","EXIF","rustfmt","mktemp","nanos","xclip","xsel","natord","Mintty","nixos","nixpkgs","SIGTSTP","SIGCONT","SIGCONT","mlua","nonstatic","userdata","metatable","natsort","backstack","luajit","Succ","Succ","cand","fileencoding","foldmethod","lightgreen","darkgray","lightred","lightyellow","lightcyan","nushell","msvc","aarch","linemode","sxyazi","rsplit","ZELLIJ","bitflags","bitflags","USERPROFILE","Neovim","vergen","gitcl","Renderable","preloaders","prec","Upserting","prio","Ghostty","Catmull","Lanczos","cmds","unyank","scrolloff","headsup","unsub","uzers","scopeguard","SPDLOG","globset","filetime","magick","magick","prefetcher","Prework","prefetchers","PREWORKERS","conds","translit","rxvt","Urxvt","realpath","realname","REPARSE","hardlink","hardlinking","nlink","nlink","linemodes","SIGSTOP","sevenzip","rsplitn","replacen","DECSET","DECRQM","repeek","cwds","tcsi","Hyprland","Wayfire","SWAYSOCK","btime","nsec","codegen","gethostname","fchmod","fdfind","Rustc","rustc","Sysinfo","ffprobe","vframes","luma","obase"],"version":"0.2","language":"en"}
{"flagWords":[],"words":["Punct","KEYMAP","splitn","crossterm","YAZI","unar","peekable","ratatui","syntect","pbpaste","pbcopy","oneshot","Posix","Lsar","XADDOS","zoxide","cands","Deque","precache","imageops","IFBLK","IFCHR","IFDIR","IFIFO","IFLNK","IFMT","IFSOCK","IRGRP","IROTH","IRUSR","ISGID","ISUID","ISVTX","IWGRP","IWOTH","IWUSR","IXGRP","IXOTH","IXUSR","libc","winsize","TIOCGWINSZ","xpixel","ypixel","ioerr","appender","Catppuccin","macchiato","gitmodules","Dotfiles","bashprofile","vimrc","flac","webp","exiftool","mediainfo","ripgrep","nvim","indexmap","indexmap","unwatch","canonicalize","serde","fsevent","Ueberzug","iterm","wezterm","sixel","chafa","ueberzugpp","️ Überzug","️ Überzug","Konsole","Alacritty","Überzug","pkgs","paru","unarchiver","pdftoppm","poppler","prebuild","singlefile","jpegopt","EXIF","rustfmt","mktemp","nanos","xclip","xsel","natord","Mintty","nixos","nixpkgs","SIGTSTP","SIGCONT","SIGCONT","mlua","nonstatic","userdata","metatable","natsort","backstack","luajit","Succ","Succ","cand","fileencoding","foldmethod","lightgreen","darkgray","lightred","lightyellow","lightcyan","nushell","msvc","aarch","linemode","sxyazi","rsplit","ZELLIJ","bitflags","bitflags","USERPROFILE","Neovim","vergen","gitcl","Renderable","preloaders","prec","Upserting","prio","Ghostty","Catmull","Lanczos","cmds","unyank","scrolloff","headsup","unsub","uzers","scopeguard","SPDLOG","globset","filetime","magick","magick","prefetcher","Prework","prefetchers","PREWORKERS","conds","translit","rxvt","Urxvt","realpath","realname","REPARSE","hardlink","hardlinking","nlink","nlink","linemodes","SIGSTOP","sevenzip","rsplitn","replacen","DECSET","DECRQM","repeek","cwds","tcsi","Hyprland","Wayfire","SWAYSOCK","btime","nsec","codegen","gethostname","fchmod","fdfind","Rustc","rustc","Sysinfo","ffprobe","vframes","luma","obase","outln","errln","tmtheme"],"version":"0.2","language":"en"}
34 changes: 27 additions & 7 deletions yazi-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,40 @@ yazi_macro::mod_pub!(package);

yazi_macro::mod_flat!(args);

use std::process::ExitCode;

use clap::Parser;
use yazi_macro::{errln, outln};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
async fn main() -> ExitCode {
match run().await {
Ok(()) => ExitCode::SUCCESS,
Err(e) => {
for cause in e.chain() {
if let Some(ioerr) = cause.downcast_ref::<std::io::Error>() {
if ioerr.kind() == std::io::ErrorKind::BrokenPipe {
return ExitCode::from(0);
}
}
}
errln!("{:#}", e).ok();
ExitCode::FAILURE
}
}
}

async fn run() -> anyhow::Result<()> {
yazi_shared::init();
yazi_fs::init();

if std::env::args_os().nth(1).is_some_and(|s| s == "-V" || s == "--version") {
println!(
outln!(
"Ya {} ({} {})",
env!("CARGO_PKG_VERSION"),
env!("VERGEN_GIT_SHA"),
env!("VERGEN_BUILD_DATE")
);
)?;
return Ok(());
}

Expand All @@ -26,7 +46,7 @@ async fn main() -> anyhow::Result<()> {
if let Err(e) =
yazi_dds::Client::shot("dds-emit", CommandPub::receiver()?, &cmd.body()?).await
{
eprintln!("Cannot emit command: {e}");
errln!("Cannot emit command: {e}")?;
std::process::exit(1);
}
}
Expand All @@ -35,7 +55,7 @@ async fn main() -> anyhow::Result<()> {
yazi_boot::init_default();
yazi_dds::init();
if let Err(e) = yazi_dds::Client::shot("dds-emit", cmd.receiver, &cmd.body()?).await {
eprintln!("Cannot emit command: {e}");
errln!("Cannot emit command: {e}")?;
std::process::exit(1);
}
}
Expand All @@ -61,7 +81,7 @@ async fn main() -> anyhow::Result<()> {
yazi_dds::init();
if let Err(e) = yazi_dds::Client::shot(&cmd.kind, CommandPub::receiver()?, &cmd.body()?).await
{
eprintln!("Cannot send message: {e}");
errln!("Cannot send message: {e}")?;
std::process::exit(1);
}
}
Expand All @@ -70,7 +90,7 @@ async fn main() -> anyhow::Result<()> {
yazi_boot::init_default();
yazi_dds::init();
if let Err(e) = yazi_dds::Client::shot(&cmd.kind, cmd.receiver, &cmd.body()?).await {
eprintln!("Cannot send message: {e}");
errln!("Cannot send message: {e}")?;
std::process::exit(1);
}
}
Expand Down
3 changes: 2 additions & 1 deletion yazi-cli/src/package/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::path::PathBuf;
use anyhow::{Context, Result, bail};
use tokio::fs;
use yazi_fs::{Xdg, maybe_exists, must_exists, remove_dir_clean};
use yazi_macro::outln;

use super::Package;

Expand Down Expand Up @@ -48,7 +49,7 @@ For safety, please manually delete it from your plugin/flavor directory and re-r

Self::deploy_assets(from.join("assets"), to.join("assets")).await?;

println!("Done!");
outln!("Done!")?;
Ok(())
}

Expand Down
7 changes: 4 additions & 3 deletions yazi-cli/src/package/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use anyhow::{Context, Result, bail};
use tokio::fs;
use toml_edit::{Array, DocumentMut, InlineTable, Item, Value};
use yazi_fs::Xdg;
use yazi_macro::outln;

use super::Package;

Expand Down Expand Up @@ -78,13 +79,13 @@ impl Package {
};

let deps = deps.as_array().context("`deps` must be an array")?;
println!("{section}s:");
outln!("{section}s:")?;

for dep in deps {
let Some(dep) = dep.as_inline_table() else { continue };
match (dep.get("use").and_then(Value::as_str), dep.get("rev").and_then(Value::as_str)) {
(Some(use_), None) => println!("\t{use_}"),
(Some(use_), Some(rev)) => println!("\t{use_} ({rev})"),
(Some(use_), None) => outln!("\t{use_}")?,
(Some(use_), Some(rev)) => outln!("\t{use_} ({rev})")?,
_ => {}
}
}
Expand Down
1 change: 1 addition & 0 deletions yazi-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ mod asset;
mod event;
mod module;
mod platform;
mod stdio;
15 changes: 15 additions & 0 deletions yazi-macro/src/stdio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#[macro_export]
macro_rules! outln {
($($tt:tt)*) => {{
use std::io::Write;
writeln!(std::io::stdout(), $($tt)*)
}}
}

#[macro_export]
macro_rules! errln {
($($tt:tt)*) => {{
use std::io::Write;
writeln!(std::io::stderr(), $($tt)*)
}}
}

0 comments on commit 00e8adc

Please sign in to comment.