comparison rust/rhg/src/commands/status.rs @ 48734:3e2b4bb286e7

rhg: Colorize `rhg status` output when appropriate Differential Revision: https://phab.mercurial-scm.org/D12168
author Simon Sapin <simon.sapin@octobus.net>
date Tue, 08 Feb 2022 14:20:58 +0100
parents 99b1dfc06571
children 29eb80d190b2
comparison
equal deleted inserted replaced
48733:39c447e03dbc 48734:3e2b4bb286e7
324 } else { 324 } else {
325 None 325 None
326 }, 326 },
327 }; 327 };
328 if display_states.modified { 328 if display_states.modified {
329 output.display(b"M", ds_status.modified)?; 329 output.display(b"M ", "status.modified", ds_status.modified)?;
330 } 330 }
331 if display_states.added { 331 if display_states.added {
332 output.display(b"A", ds_status.added)?; 332 output.display(b"A ", "status.added", ds_status.added)?;
333 } 333 }
334 if display_states.removed { 334 if display_states.removed {
335 output.display(b"R", ds_status.removed)?; 335 output.display(b"R ", "status.removed", ds_status.removed)?;
336 } 336 }
337 if display_states.deleted { 337 if display_states.deleted {
338 output.display(b"!", ds_status.deleted)?; 338 output.display(b"! ", "status.deleted", ds_status.deleted)?;
339 } 339 }
340 if display_states.unknown { 340 if display_states.unknown {
341 output.display(b"?", ds_status.unknown)?; 341 output.display(b"? ", "status.unknown", ds_status.unknown)?;
342 } 342 }
343 if display_states.ignored { 343 if display_states.ignored {
344 output.display(b"I", ds_status.ignored)?; 344 output.display(b"I ", "status.ignored", ds_status.ignored)?;
345 } 345 }
346 if display_states.clean { 346 if display_states.clean {
347 output.display(b"C", ds_status.clean)?; 347 output.display(b"C ", "status.clean", ds_status.clean)?;
348 } 348 }
349 349
350 let mut dirstate_write_needed = ds_status.dirty; 350 let mut dirstate_write_needed = ds_status.dirty;
351 let filesystem_time_at_status_start = 351 let filesystem_time_at_status_start =
352 ds_status.filesystem_time_at_status_start; 352 ds_status.filesystem_time_at_status_start;
446 // Probably more elegant to use a Deref or Borrow trait rather than 446 // Probably more elegant to use a Deref or Borrow trait rather than
447 // harcode HgPathBuf, but probably not really useful at this point 447 // harcode HgPathBuf, but probably not really useful at this point
448 fn display( 448 fn display(
449 &self, 449 &self,
450 status_prefix: &[u8], 450 status_prefix: &[u8],
451 label: &'static str,
451 mut paths: Vec<StatusPath<'_>>, 452 mut paths: Vec<StatusPath<'_>>,
452 ) -> Result<(), CommandError> { 453 ) -> Result<(), CommandError> {
453 paths.sort_unstable(); 454 paths.sort_unstable();
455 // TODO: get the stdout lock once for the whole loop instead of in each write
454 for StatusPath { path, copy_source } in paths { 456 for StatusPath { path, copy_source } in paths {
455 let relative; 457 let relative;
456 let path = if let Some(relativize) = &self.relativize { 458 let path = if let Some(relativize) = &self.relativize {
457 relative = relativize.relativize(&path); 459 relative = relativize.relativize(&path);
458 &*relative 460 &*relative
459 } else { 461 } else {
460 path.as_bytes() 462 path.as_bytes()
461 }; 463 };
462 // TODO optim, probably lots of unneeded copies here, especially 464 // TODO: Add a way to use `write_bytes!` instead of `format_bytes!`
463 // if out stream is buffered 465 // in order to stream to stdout instead of allocating an
464 if self.no_status { 466 // itermediate `Vec<u8>`.
465 self.ui.write_stdout(&format_bytes!(b"{}\n", path))? 467 if !self.no_status {
466 } else { 468 self.ui.write_stdout_labelled(status_prefix, label)?
467 self.ui.write_stdout(&format_bytes!( 469 }
468 b"{} {}\n", 470 self.ui
469 status_prefix, 471 .write_stdout_labelled(&format_bytes!(b"{}\n", path), label)?;
470 path
471 ))?
472 }
473 if let Some(source) = copy_source { 472 if let Some(source) = copy_source {
474 self.ui.write_stdout(&format_bytes!( 473 let label = "status.copied";
475 b" {}\n", 474 self.ui.write_stdout_labelled(
476 source.as_bytes() 475 &format_bytes!(b" {}\n", source.as_bytes()),
477 ))? 476 label,
477 )?
478 } 478 }
479 } 479 }
480 Ok(()) 480 Ok(())
481 } 481 }
482 } 482 }