Add an exit() method to ExitCode
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tavian Barnes · committed by David Peter · parent cab31e2 · Browse files · 4 files·+14 added−12 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+14 added−12 removed
- other:+14 added−12 removed
Provenance
- Commit
2b1bf47- Tree
7fea65d- Author
Tavian Barnes <tavianator@tavianator.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
cab31e2
4 files changed+14 added−12 removed
modified:src/exit_codes.rs+7 added
1+use std::process;2+3 #[derive(Debug, Clone, Copy, PartialEq)]4 pub enum ExitCode {5 Success,6 HasResults(bool),15 unmodified lines22 impl ExitCode {23 fn is_error(self) -> bool {24 i32::from(self) != 025 }26+27+ /// Exit the process with the appropriate code.28+ pub fn exit(self) -> ! {29+ process::exit(self.into())30+ }31 }32 33 pub fn merge_exitcodes(results: impl IntoIterator<Item = ExitCode>) -> ExitCode {34 if results.into_iter().any(ExitCode::is_error) {47 unmodified linesmodified:src/main.rs+2 added-3 removed
10 unmodified lines11 mod walk;12 13 use std::env;14 use std::path::{Path, PathBuf};15-use std::process;15 use std::sync::Arc;16 use std::time;17 18 use anyhow::{anyhow, Context, Result};33 unmodified lines52 fn main() {53 let result = run();54 match result {55 Ok(exit_code) => {57- process::exit(exit_code.into());56+ exit_code.exit();57 }58 Err(err) => {59 eprintln!("[fd error]: {:#}", err);61- process::exit(ExitCode::GeneralError.into());60+ ExitCode::GeneralError.exit();61 }62 }63 }64 501 unmodified linesmodified:src/output.rs+3 added-4 removed
1 use std::borrow::Cow;2 use std::io::{self, StdoutLock, Write};3 use std::path::Path;4-use std::process;4 use std::sync::atomic::{AtomicBool, Ordering};5 use std::sync::Arc;6 7 use lscolors::{Indicator, LsColors, Style};28 unmodified lines36 37 if let Err(e) = r {38 if e.kind() == ::std::io::ErrorKind::BrokenPipe {39 // Exit gracefully in case of a broken pipe (e.g. 'fd ... | head -n 3').41- process::exit(0);40+ ExitCode::Success.exit();41 } else {42 print_error(format!("Could not write to output: {}", e));44- process::exit(ExitCode::GeneralError.into());43+ ExitCode::GeneralError.exit();44 }45 }46 }47 45 unmodified lines93 writeln!(stdout)?;94 }95 96 if wants_to_quit.load(Ordering::Relaxed) {98- process::exit(ExitCode::KilledBySigint.into());97+ ExitCode::KilledBySigint.exit();98 }99 100 Ok(())101 }43 unmodified linesmodified:src/walk.rs+2 added-5 removed
1 unmodified line2 use std::ffi::OsStr;3 use std::fs::{FileType, Metadata};4 use std::io;5 use std::path::{Path, PathBuf};6-use std::process;6 use std::sync::atomic::{AtomicBool, Ordering};7 use std::sync::mpsc::{channel, Receiver, Sender};8 use std::sync::{Arc, Mutex};9 use std::thread;125 unmodified lines135 let wants_to_quit = Arc::new(AtomicBool::new(false));136 if config.ls_colors.is_some() && config.command.is_none() {137 let wq = Arc::clone(&wants_to_quit);138 ctrlc::set_handler(move || {140- if wq.load(Ordering::Relaxed) {139+ if wq.fetch_or(true, Ordering::Relaxed) {140 // Ctrl-C has been pressed twice, exit NOW142- process::exit(ExitCode::KilledBySigint.into());143- } else {144- wq.store(true, Ordering::Relaxed);141+ ExitCode::KilledBySigint.exit();142 }143 })144 .unwrap();145 }377 unmodified lines