Skip to content

fix: ignore broken pipes when writing to terminal #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 4 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ better-panic = "0.3.0"
clap = { version = "4.5.17", features = ["derive"] }
clap-verbosity-flag = "2.2.2"
clap_complete = "4.5.26"
colored_markup = "0.1.1"
colored = "3.0.0"
csv = "1.3.0"
csvlens = "0.10.1"
edit = "0.1.5"
Expand Down
105 changes: 61 additions & 44 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! It supports filtering repositories based on their state and provides utilities to execute commands across repositories.

use anyhow::{anyhow, Context, Result};
use colored_markup::{println_markup, StyleSheet};
use colored::Colorize;
use csv::WriterBuilder;
use fern::colors::{Color, ColoredLevelConfig};
use inquire::Confirm;
Expand All @@ -15,14 +15,12 @@ use std::collections::{HashMap, HashSet};
use std::env;
use std::fmt;
use std::fs;
use std::io;
use std::io::Read;
use std::io::{self, Read, Write};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::SystemTime;
use tabled::{Table, Tabled};
use walkdir::WalkDir;

/// Represents an entry for a single Git repository.
#[derive(Debug, Deserialize, Serialize)]
pub struct RepositoryEntry {
Expand Down Expand Up @@ -257,29 +255,12 @@ pub struct Multigit {
pub config: Config,

pub directory: Option<PathBuf>,

/// The stylesheet used for colored output.
pub style_sheet: StyleSheet<'static>,
}

impl Multigit {
/// Creates a new instance of `Multigit`.
pub fn new(config: Config, directory: Option<PathBuf>) -> Result<Self> {
let style_sheet = StyleSheet::parse(
"
repository { foreground: cyan; }
status { foreground: yellow; }
command { foreground: green; }
divider { foreground: red; }
",
)
.unwrap();

anyhow::Ok(Self {
config,
directory,
style_sheet,
})
anyhow::Ok(Self { config, directory })
}

/// Retrieves all repositories, optionally filtering them.
Expand Down Expand Up @@ -457,11 +438,15 @@ impl Multigit {

if !detailed {
for row in rows {
println_markup!(
&self.style_sheet,
"<repository>{}</repository>",
row.path.display()
);
match writeln!(io::stdout(), "{}", row.path.display().to_string().cyan()) {
Ok(()) => {}
// Ignore broken pipe errors
Err(e) if e.kind() == io::ErrorKind::BrokenPipe => {}
Err(e) => {
eprintln!("Error: {e}");
std::process::exit(1);
}
}
}
} else if *csv {
let mut wtr = WriterBuilder::new().from_writer(vec![]);
Expand Down Expand Up @@ -559,12 +544,20 @@ impl Multigit {
status_string.push_str(" [conflicted]");
}

println_markup!(
&self.style_sheet,
"<repository>{}</repository><status>{}</status>",
repository.path.to_str().unwrap(),
status_string
);
match writeln!(
io::stdout(),
"{}{}",
repository.path.to_str().unwrap().cyan(),
status_string.yellow()
) {
Ok(()) => {}
// Ignore broken pipe errors
Err(e) if e.kind() == io::ErrorKind::BrokenPipe => {}
Err(e) => {
eprintln!("Error: {e}");
std::process::exit(1);
}
}
}
anyhow::Ok(())
})
Expand All @@ -582,11 +575,19 @@ impl Multigit {
}
}
for repository in paths_to_open.iter() {
println_markup!(
&self.style_sheet,
match writeln!(
io::stdout(),
"Opening git ui for {}",
repository.path.to_str().unwrap()
);
repository.path.to_str().unwrap().cyan()
) {
Ok(()) => {}
// Ignore broken pipe errors
Err(e) if e.kind() == io::ErrorKind::BrokenPipe => {}
Err(e) => {
eprintln!("Error: {e}");
std::process::exit(1);
}
}
open_in_git_ui(&repository.path)?;
}
anyhow::Ok(())
Expand Down Expand Up @@ -622,15 +623,31 @@ impl Multigit {

self.process_repositories(repositories, |repository| {
if !first_repository {
println_markup!(&self.style_sheet, "\n<divider>{}</divider>\n", divider);
match writeln!(io::stdout(), "\n{}\n", divider.red()) {
Ok(()) => {}
// Ignore broken pipe errors
Err(e) if e.kind() == io::ErrorKind::BrokenPipe => {}
Err(e) => {
eprintln!("Error: {e}");
std::process::exit(1);
}
}
}
first_repository = false;
println_markup!(
&self.style_sheet,
"Running `<command>{}</command>` in <repository>{}</repository>\n",
git_command,
repository.path.to_str().unwrap()
);
match writeln!(
io::stdout(),
"Running `{}` in {}\n",
git_command.green(),
repository.path.to_str().unwrap().cyan()
) {
Ok(()) => {}
// Ignore broken pipe errors
Err(e) if e.kind() == io::ErrorKind::BrokenPipe => {}
Err(e) => {
eprintln!("Error: {e}");
std::process::exit(1);
}
}
let mut args = vec![git_command];
args.extend(passthrough.iter().map(|s| s.as_str()));
let mut command = std::process::Command::new("git");
Expand Down