Mercurial > public > mercurial-scm > hg-stable
diff rust/rhg/src/commands/status.rs @ 48492:9b0e1f64656f
rhg: refactor relativize_path into a struct + method
? instead of a function that takes an iterator and a callback.
Differential Revision: https://phab.mercurial-scm.org/D11898
author | Simon Sapin <simon.sapin@octobus.net> |
---|---|
date | Fri, 10 Dec 2021 16:57:39 +0100 |
parents | 2afaa0145584 |
children | 473af5cbc209 |
line wrap: on
line diff
--- a/rust/rhg/src/commands/status.rs Fri Dec 10 16:31:16 2021 +0100 +++ b/rust/rhg/src/commands/status.rs Fri Dec 10 16:57:39 2021 +0100 @@ -7,7 +7,7 @@ use crate::error::CommandError; use crate::ui::Ui; -use crate::utils::path_utils::relativize_paths; +use crate::utils::path_utils::RelativizePaths; use clap::{Arg, SubCommand}; use format_bytes::format_bytes; use hg; @@ -261,9 +261,12 @@ .unwrap_or(config.get_bool(b"ui", b"relative-paths")?); let output = DisplayStatusPaths { ui, - repo, no_status, - relative_paths, + relativize: if relative_paths { + Some(RelativizePaths::new(repo)?) + } else { + None + }, }; if display_states.modified { output.display(b"M", ds_status.modified)?; @@ -379,9 +382,8 @@ struct DisplayStatusPaths<'a> { ui: &'a Ui, - repo: &'a Repo, no_status: bool, - relative_paths: bool, + relativize: Option<RelativizePaths>, } impl DisplayStatusPaths<'_> { @@ -393,27 +395,24 @@ mut paths: Vec<HgPathCow>, ) -> Result<(), CommandError> { paths.sort_unstable(); - let print_path = |path: &[u8]| { + for path in paths { + let relative; + let path = if let Some(relativize) = &self.relativize { + relative = relativize.relativize(&path); + &*relative + } else { + path.as_bytes() + }; // TODO optim, probably lots of unneeded copies here, especially // if out stream is buffered if self.no_status { - self.ui.write_stdout(&format_bytes!(b"{}\n", path)) + self.ui.write_stdout(&format_bytes!(b"{}\n", path))? } else { self.ui.write_stdout(&format_bytes!( b"{} {}\n", status_prefix, path - )) - } - }; - - if self.relative_paths { - relativize_paths(self.repo, paths.iter().map(Ok), |path| { - print_path(&path) - })?; - } else { - for path in paths { - print_path(path.as_bytes())? + ))? } } Ok(())