diff options
author | root <github@bigly.dog> | 2024-03-22 13:48:48 -0700 |
---|---|---|
committer | root <github@bigly.dog> | 2024-03-22 13:53:46 -0700 |
commit | 1c59c24e119ab91db75c66bfb7a5ffd6a6416891 (patch) | |
tree | 1e344875728e9c018bdbe5d5f03d773940b7cb52 | |
parent | beb8ac5b17d703cc1c9daf761bf6713491338e95 (diff) |
utf8
-rw-r--r-- | src/argparse.rs | 12 | ||||
-rw-r--r-- | src/displace.rs | 4 | ||||
-rw-r--r-- | src/fs_pipe.rs | 22 | ||||
-rw-r--r-- | src/fzf.rs | 18 | ||||
-rw-r--r-- | src/input.rs | 26 | ||||
-rw-r--r-- | src/main.rs | 22 | ||||
-rw-r--r-- | src/subprocess.rs | 20 | ||||
-rw-r--r-- | src/types.rs | 12 |
8 files changed, 68 insertions, 68 deletions
diff --git a/src/argparse.rs b/src/argparse.rs index 55f868a..ca850f2 100644 --- a/src/argparse.rs +++ b/src/argparse.rs @@ -1,5 +1,5 @@ use { - super::{subprocess::SubprocCommand, types::Fail}, + super::{subprocess::SubprocCommand, types::Die}, aho_corasick::{AhoCorasick, AhoCorasickBuilder}, clap::Parser, regex::{Regex, RegexBuilder}, @@ -155,14 +155,14 @@ fn p_auto_flags(exact: bool, pattern: &str) -> Vec<String> { flags } -fn p_aho_corasick(pattern: &str, flags: Vec<String>) -> Result<AhoCorasick, Fail> { +fn p_aho_corasick(pattern: &str, flags: Vec<String>) -> Result<AhoCorasick, Die> { let mut ac = AhoCorasickBuilder::new(); for flag in flags { match flag.as_str() { "i" => ac.ascii_case_insensitive(true), "I" => ac.ascii_case_insensitive(false), _ => { - return Err(Fail::ArgumentError(format!( + return Err(Die::ArgumentError(format!( "Invalid regex flag, see `--help` :: {flag}" ))) } @@ -171,7 +171,7 @@ fn p_aho_corasick(pattern: &str, flags: Vec<String>) -> Result<AhoCorasick, Fail Ok(ac.build([pattern])?) } -fn p_regex(pattern: &str, flags: Vec<String>) -> Result<Regex, Fail> { +fn p_regex(pattern: &str, flags: Vec<String>) -> Result<Regex, Die> { let mut re = RegexBuilder::new(pattern); for flag in flags { match flag.as_str() { @@ -186,7 +186,7 @@ fn p_regex(pattern: &str, flags: Vec<String>) -> Result<Regex, Fail> { "x" => re.ignore_whitespace(true), "X" => re.ignore_whitespace(false), _ => { - return Err(Fail::ArgumentError(format!( + return Err(Die::ArgumentError(format!( "Invalid regex flag, see `--help` :: {flag}" ))) } @@ -243,7 +243,7 @@ fn p_pager(pager: &Option<String>) -> Option<SubprocCommand> { }) } -pub fn parse_opts(mode: Mode, args: Arguments) -> Result<Options, Fail> { +pub fn parse_opts(mode: Mode, args: Arguments) -> Result<Options, Die> { let mut flagset = p_auto_flags(args.exact, &args.pattern); flagset.extend( args diff --git a/src/displace.rs b/src/displace.rs index 12ef06e..4c19349 100644 --- a/src/displace.rs +++ b/src/displace.rs @@ -3,7 +3,7 @@ use { argparse::{Action, Engine, Options}, fs_pipe::{slurp, spit}, input::LineIn, - types::Fail, + types::Die, udiff::{apply_patches, patches, pure_diffs, udiff}, }, ansi_term::Colour, @@ -27,7 +27,7 @@ impl LineIn { } } -pub async fn displace(opts: &Arc<Options>, input: LineIn) -> Result<OsString, Fail> { +pub async fn displace(opts: &Arc<Options>, input: LineIn) -> Result<OsString, Die> { let path = input.path().clone(); let name = opts .cwd diff --git a/src/fs_pipe.rs b/src/fs_pipe.rs index 8863e9a..5972275 100644 --- a/src/fs_pipe.rs +++ b/src/fs_pipe.rs @@ -1,5 +1,5 @@ use { - super::types::Fail, + super::types::Die, std::{borrow::ToOwned, fs::Metadata, io::ErrorKind, path::Path}, tokio::{ fs::{rename, File, OpenOptions}, @@ -13,22 +13,22 @@ pub struct Slurpee { pub content: String, } -pub async fn slurp(path: &Path) -> Result<Slurpee, Fail> { +pub async fn slurp(path: &Path) -> Result<Slurpee, Die> { let mut fd = File::open(path) .await - .map_err(|e| Fail::IO(path.to_owned(), e.kind()))?; + .map_err(|e| Die::IO(path.to_owned(), e.kind()))?; let meta = fd .metadata() .await - .map_err(|e| Fail::IO(path.to_owned(), e.kind()))?; + .map_err(|e| Die::IO(path.to_owned(), e.kind()))?; let content = if meta.is_file() { let mut s = String::default(); match fd.read_to_string(&mut s).await { Ok(_) => s, Err(err) if err.kind() == ErrorKind::InvalidData => s, - Err(err) => return Err(Fail::IO(path.to_owned(), err.kind())), + Err(err) => return Err(Die::IO(path.to_owned(), err.kind())), } } else { String::default() @@ -37,7 +37,7 @@ pub async fn slurp(path: &Path) -> Result<Slurpee, Fail> { Ok(Slurpee { meta, content }) } -pub async fn spit(canonical: &Path, meta: &Metadata, text: &str) -> Result<(), Fail> { +pub async fn spit(canonical: &Path, meta: &Metadata, text: &str) -> Result<(), Die> { let uuid = Uuid::new_v4().as_simple().to_string(); let mut file_name = canonical .file_name() @@ -52,25 +52,25 @@ pub async fn spit(canonical: &Path, meta: &Metadata, text: &str) -> Result<(), F .write(true) .open(&tmp) .await - .map_err(|e| Fail::IO(tmp.clone(), e.kind()))?; + .map_err(|e| Die::IO(tmp.clone(), e.kind()))?; fd.set_permissions(meta.permissions()) .await - .map_err(|e| Fail::IO(tmp.clone(), e.kind()))?; + .map_err(|e| Die::IO(tmp.clone(), e.kind()))?; let mut writer = BufWriter::new(fd); writer .write_all(text.as_bytes()) .await - .map_err(|e| Fail::IO(tmp.clone(), e.kind()))?; + .map_err(|e| Die::IO(tmp.clone(), e.kind()))?; writer .flush() .await - .map_err(|e| Fail::IO(tmp.clone(), e.kind()))?; + .map_err(|e| Die::IO(tmp.clone(), e.kind()))?; rename(&tmp, &canonical) .await - .map_err(|e| Fail::IO(canonical.to_owned(), e.kind()))?; + .map_err(|e| Die::IO(canonical.to_owned(), e.kind()))?; Ok(()) } @@ -2,7 +2,7 @@ use { super::{ argparse::Mode, subprocess::{stream_subproc, SubprocCommand}, - types::Fail, + types::Die, }, futures::stream::{BoxStream, Stream, StreamExt}, std::{ @@ -16,7 +16,7 @@ use { which::which, }; -async fn reset_term() -> Result<(), Fail> { +async fn reset_term() -> Result<(), Die> { if let Ok(path) = which("tput") { let status = Command::new(&path) .kill_on_drop(true) @@ -24,7 +24,7 @@ async fn reset_term() -> Result<(), Fail> { .arg("reset") .status() .await - .map_err(|e| Fail::IO(path, e.kind()))?; + .map_err(|e| Die::IO(path, e.kind()))?; if status.success() { return Ok(()); @@ -36,19 +36,19 @@ async fn reset_term() -> Result<(), Fail> { .stdin(Stdio::null()) .status() .await - .map_err(|e| Fail::IO(path, e.kind()))?; + .map_err(|e| Die::IO(path, e.kind()))?; if status.success() { return Ok(()); } } - Err(Fail::IO(PathBuf::from("reset"), ErrorKind::NotFound)) + Err(Die::IO(PathBuf::from("reset"), ErrorKind::NotFound)) } pub fn stream_fzf_proc<'a>( bin: PathBuf, args: Vec<String>, - stream: impl Stream<Item = Result<OsString, Fail>> + Unpin + Send + 'a, -) -> Box<dyn Stream<Item = Result<(), Fail>> + Send + 'a> { + stream: impl Stream<Item = Result<OsString, Die>> + Unpin + Send + 'a, +) -> Box<dyn Stream<Item = Result<(), Die>> + Send + 'a> { let execute = format!("abort+execute:{}\x04{{+f}}", Mode::PATCH); let mut arguments = vec![ "--read0".to_owned(), @@ -76,7 +76,7 @@ pub fn stream_fzf_proc<'a>( |path| format!("{}", path.display()), ), ); - fzf_env.insert("LC_ALL".to_owned(), "C".to_owned()); + fzf_env.insert("LC_ALL".to_owned(), "C-UTF8".to_owned()); let cmd = SubprocCommand { prog: bin, @@ -86,7 +86,7 @@ pub fn stream_fzf_proc<'a>( let stream = BoxStream::from(stream_subproc(cmd, stream)).then(|line| async { match line { Ok(o) => Ok(o), - Err(Fail::BadExit(_, 130)) => Err(Fail::Interrupt), + Err(Die::BadExit(_, 130)) => Err(Die::Interrupt), e => { let _ = reset_term().await; e diff --git a/src/input.rs b/src/input.rs index d7f5e58..82937fb 100644 --- a/src/input.rs +++ b/src/input.rs @@ -1,7 +1,7 @@ use { super::{ argparse::{Arguments, Mode}, - types::Fail, + types::Die, udiff::DiffRange, }, futures::{ @@ -30,10 +30,10 @@ pub enum LineIn { #[derive(Debug)] struct DiffLine(PathBuf, DiffRange); -fn p_line(line: &str) -> Result<DiffLine, Fail> { - let f = Fail::ArgumentError(String::default()); +fn p_line(line: &str) -> Result<DiffLine, Die> { + let f = Die::ArgumentError(String::default()); let preg = "\n\n\n\n@@ -(\\d+),(\\d+) \\+(\\d+),(\\d+) @@$"; - let re = Regex::new(preg).map_err(Fail::RegexError)?; + let re = Regex::new(preg).map_err(Die::RegexError)?; let captures = re.captures(line).ok_or_else(|| f.clone())?; let before_start = captures @@ -69,12 +69,12 @@ fn p_line(line: &str) -> Result<DiffLine, Fail> { Ok(DiffLine(path, range)) } -async fn stream_patch(patches: &Path) -> Box<dyn Stream<Item = Result<LineIn, Fail>> + Send> { +async fn stream_patch(patches: &Path) -> Box<dyn Stream<Item = Result<LineIn, Die>> + Send> { let patches = patches.to_owned(); let fd = match File::open(&patches).await { Err(e) => { - let err = Fail::IO(patches.clone(), e.kind()); + let err = Die::IO(patches.clone(), e.kind()); return Box::new(once(ready(Err(err)))); } Ok(fd) => fd, @@ -87,7 +87,7 @@ async fn stream_patch(patches: &Path) -> Box<dyn Stream<Item = Result<LineIn, Fa move |mut s| async move { let mut buf = Vec::default(); match s.0.read_until(b'\0', &mut buf).await { - Err(err) => Err(Fail::IO(s.1.clone(), err.kind())), + Err(err) => Err(Die::IO(s.1.clone(), err.kind())), Ok(0) if s.3.is_empty() => Ok(None), Ok(0) => { let path = s.2; @@ -99,7 +99,7 @@ async fn stream_patch(patches: &Path) -> Box<dyn Stream<Item = Result<LineIn, Fa Ok(_) => { buf.pop(); let line = - String::from_utf8(buf).map_err(|_| Fail::IO(s.1.clone(), ErrorKind::InvalidData))?; + String::from_utf8(buf).map_err(|_| Die::IO(s.1.clone(), ErrorKind::InvalidData))?; let parsed = p_line(&line)?; if parsed.0 == s.2 { s.3.insert(parsed.1); @@ -143,7 +143,7 @@ fn u8_pathbuf(v8: Vec<u8>) -> PathBuf { } } -fn stream_stdin(use_nul: bool) -> impl Stream<Item = Result<LineIn, Fail>> { +fn stream_stdin(use_nul: bool) -> impl Stream<Item = Result<LineIn, Die>> { let delim = if use_nul { b'\0' } else { b'\n' }; let reader = BufReader::new(stdin()); let seen = HashSet::new(); @@ -151,14 +151,14 @@ fn stream_stdin(use_nul: bool) -> impl Stream<Item = Result<LineIn, Fail>> { let stream = try_unfold((reader, seen), move |mut s| async move { let mut buf = Vec::default(); match s.0.read_until(delim, &mut buf).await { - Err(e) => Err(Fail::IO(PathBuf::from("/dev/stdin"), e.kind())), + Err(e) => Err(Die::IO(PathBuf::from("/dev/stdin"), e.kind())), Ok(0) => Ok(None), Ok(_) => { buf.pop(); let path = u8_pathbuf(buf); match canonicalize(&path).await { Err(e) if e.kind() == ErrorKind::NotFound => Ok(Some((None, s))), - Err(e) => Err(Fail::IO(path, e.kind())), + Err(e) => Err(Die::IO(path, e.kind())), Ok(canonical) => Ok(Some({ if s.1.insert(canonical.clone()) { (Some(LineIn::Entire(canonical)), s) @@ -177,10 +177,10 @@ fn stream_stdin(use_nul: bool) -> impl Stream<Item = Result<LineIn, Fail>> { pub async fn stream_in( mode: &Mode, args: &Arguments, -) -> Box<dyn Stream<Item = Result<LineIn, Fail>> + Send> { +) -> Box<dyn Stream<Item = Result<LineIn, Die>> + Send> { match mode { Mode::Initial if io::stdin().is_terminal() => { - let err = Fail::ArgumentError("/dev/stdin connected to tty".to_owned()); + let err = Die::ArgumentError("/dev/stdin connected to tty".to_owned()); Box::new(once(ready(Err(err)))) } Mode::Initial => Box::new(stream_stdin(args.read0)), diff --git a/src/main.rs b/src/main.rs index 18ddadb..768cb01 100644 --- a/src/main.rs +++ b/src/main.rs @@ -37,13 +37,13 @@ use { }, subprocess::{stream_into, stream_subproc}, tokio::{io, runtime::Builder, signal::ctrl_c}, - types::Fail, + types::Die, }; fn stream_sink<'a>( opts: &Options, - stream: impl Stream<Item = Result<OsString, Fail>> + Unpin + Send + 'a, -) -> Box<dyn Stream<Item = Result<(), Fail>> + Send + 'a> { + stream: impl Stream<Item = Result<OsString, Die>> + Unpin + Send + 'a, +) -> Box<dyn Stream<Item = Result<(), Die>> + Send + 'a> { match (&opts.action, &opts.printer) { (Action::FzfPreview(fzf_p, fzf_a), _) => stream_fzf_proc(fzf_p.clone(), fzf_a.clone(), stream), (_, Printer::Pager(cmd)) => stream_subproc(cmd.clone(), stream), @@ -54,31 +54,31 @@ fn stream_sink<'a>( } } -async fn consume(stream: impl Stream<Item = Result<(), Fail>> + Send + Unpin) -> Result<(), Fail> { +async fn consume(stream: impl Stream<Item = Result<(), Die>> + Send + Unpin) -> Result<(), Die> { let int = once(async { match ctrl_c().await { - Err(e) => Fail::IO(PathBuf::from("sigint"), e.kind()), - Ok(()) => Fail::Interrupt, + Err(e) => Die::IO(PathBuf::from("sigint"), e.kind()), + Ok(()) => Die::Interrupt, } }); let out = select( stream .filter_map(|row| async { row.err() }) - .chain(once(ready(Fail::EOF))), + .chain(once(ready(Die::Eof))), int, ); let mut out = pin!(out); loop { match out.next().await { - None | Some(Fail::EOF) => break, - Some(Fail::Interrupt) => return Err(Fail::Interrupt), + None | Some(Die::Eof) => break, + Some(Die::Interrupt) => return Err(Die::Interrupt), Some(e) => eprintln!("{}", Colour::Red.paint(format!("{e}"))), } } Ok(()) } -async fn run(threads: usize) -> Result<(), Fail> { +async fn run(threads: usize) -> Result<(), Die> { let (mode, args) = parse_args(); let input_stream = stream_in(&mode, &args).await; let opts = parse_opts(mode, args)?; @@ -104,7 +104,7 @@ fn main() -> impl Termination { match rt.block_on(run(threads)).err() { None => ExitCode::SUCCESS, - Some(Fail::Interrupt) => ExitCode::from(130), + Some(Die::Interrupt) => ExitCode::from(130), Some(e) => { eprintln!("{}", Colour::Red.paint(format!("{e}"))); ExitCode::FAILURE diff --git a/src/subprocess.rs b/src/subprocess.rs index e4cfecc..1dd6167 100644 --- a/src/subprocess.rs +++ b/src/subprocess.rs @@ -1,5 +1,5 @@ use { - super::types::Fail, + super::types::Die, futures::{ future::ready, stream::{once, select, try_unfold, Stream, StreamExt}, @@ -21,8 +21,8 @@ pub struct SubprocCommand { pub fn stream_into( path: PathBuf, writer: impl AsyncWrite + Send + Unpin, - stream: impl Stream<Item = Result<OsString, Fail>> + Send + Unpin, -) -> impl Stream<Item = Result<(), Fail>> + Send + stream: impl Stream<Item = Result<OsString, Die>> + Send + Unpin, +) -> impl Stream<Item = Result<(), Die>> + Send where { let buf = BufWriter::new(writer); @@ -32,7 +32,7 @@ where s.1 .shutdown() .await - .map_err(|e| Fail::IO(s.2.clone(), e.kind()))?; + .map_err(|e| Die::IO(s.2.clone(), e.kind()))?; Ok(None) } Some(Err(e)) => { @@ -53,7 +53,7 @@ where s.1 .write_all(bytes) .await - .map_err(|e| Fail::IO(s.2.clone(), e.kind()))?; + .map_err(|e| Die::IO(s.2.clone(), e.kind()))?; Ok(Some(((), s))) } } @@ -62,8 +62,8 @@ where pub fn stream_subproc<'a>( cmd: SubprocCommand, - stream: impl Stream<Item = Result<OsString, Fail>> + Unpin + Send + 'a, -) -> Box<dyn Stream<Item = Result<(), Fail>> + Send + 'a> { + stream: impl Stream<Item = Result<OsString, Die>> + Unpin + Send + 'a, +) -> Box<dyn Stream<Item = Result<(), Die>> + Send + 'a> { let subprocess = Command::new(&cmd.prog) .kill_on_drop(true) .args(&cmd.args) @@ -73,7 +73,7 @@ pub fn stream_subproc<'a>( match subprocess { Err(e) => { - let err = Fail::IO(cmd.prog, e.kind()); + let err = Die::IO(cmd.prog, e.kind()); Box::new(once(ready(Err(err)))) } Ok(mut child) => { @@ -81,11 +81,11 @@ pub fn stream_subproc<'a>( let out = stream_into(cmd.prog.clone(), stdin, stream); let die = once(async move { match child.wait().await { - Err(e) => Err(Fail::IO(cmd.prog, e.kind())), + Err(e) => Err(Die::IO(cmd.prog, e.kind())), Ok(status) if status.success() => Ok(()), Ok(status) => { let code = status.code().unwrap_or(1); - Err(Fail::BadExit(cmd.prog, code)) + Err(Die::BadExit(cmd.prog, code)) } } }); diff --git a/src/types.rs b/src/types.rs index fb4f529..8343481 100644 --- a/src/types.rs +++ b/src/types.rs @@ -14,8 +14,8 @@ use { }; #[derive(Clone, Debug)] -pub enum Fail { - EOF, +pub enum Die { + Eof, Interrupt, RegexError(RegexError), BuildError(BuildError), @@ -24,21 +24,21 @@ pub enum Fail { BadExit(PathBuf, i32), } -impl Error for Fail {} +impl Error for Die {} -impl Display for Fail { +impl Display for Die { fn fmt(&self, f: &mut Formatter) -> fmt::Result { write!(f, "Error: {self:?}") } } -impl From<RegexError> for Fail { +impl From<RegexError> for Die { fn from(e: RegexError) -> Self { Self::RegexError(e) } } -impl From<BuildError> for Fail { +impl From<BuildError> for Die { fn from(e: BuildError) -> Self { Self::BuildError(e) } |