rust/rhg/src/ui.rs
author Arseniy Alekseyev <aalekseyev@janestreet.com>
Mon, 29 May 2023 16:53:18 +0100
changeset 50539 74e4dbb0fcd5
parent 49981 364e78389653
child 51120 532e74ad3ff6
permissions -rw-r--r--
rhg: make `rhg files` work if `ui.relative-files=true` is specified
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48733
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
     1
use crate::color::ColorConfig;
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
     2
use crate::color::Effect;
49981
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
     3
use crate::error::CommandError;
45984
fada33872b5b rhg: use `format_bytes!` for error messages
Raphaël Gomès <rgomes@octobus.net>
parents: 45528
diff changeset
     4
use format_bytes::format_bytes;
48733
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
     5
use format_bytes::write_bytes;
48730
1aaf11e35aec rhg: Pass a &Config to Ui::new
Simon Sapin <simon.sapin@octobus.net>
parents: 48729
diff changeset
     6
use hg::config::Config;
49513
467d9df98c68 rhg: centralize PlainInfo
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49512
diff changeset
     7
use hg::config::PlainInfo;
48731
f591b377375f rhg: Make Ui::new falliable, add Ui::new_infallible
Simon Sapin <simon.sapin@octobus.net>
parents: 48730
diff changeset
     8
use hg::errors::HgError;
49981
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
     9
use hg::repo::Repo;
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    10
use hg::sparse;
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    11
use hg::utils::files::get_bytes_from_path;
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
    12
use hg::PatternFileWarning;
45528
66756b34c06e rhg: add a `DebugData` `Command` to prepare the `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45440
diff changeset
    13
use std::borrow::Cow;
45049
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    14
use std::io;
45362
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    15
use std::io::{ErrorKind, Write};
45049
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    16
45362
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    17
pub struct Ui {
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    18
    stdout: std::io::Stdout,
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    19
    stderr: std::io::Stderr,
48733
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    20
    colors: Option<ColorConfig>,
45362
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    21
}
45049
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    22
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    23
/// The kind of user interface error
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    24
pub enum UiError {
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    25
    /// The standard output stream cannot be written to
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    26
    StdoutError(io::Error),
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    27
    /// The standard error stream cannot be written to
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    28
    StderrError(io::Error),
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    29
}
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    30
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    31
/// The commandline user interface
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    32
impl Ui {
48733
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    33
    pub fn new(config: &Config) -> Result<Self, HgError> {
48731
f591b377375f rhg: Make Ui::new falliable, add Ui::new_infallible
Simon Sapin <simon.sapin@octobus.net>
parents: 48730
diff changeset
    34
        Ok(Ui {
48733
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    35
            // If using something else, also adapt `isatty()` below.
48731
f591b377375f rhg: Make Ui::new falliable, add Ui::new_infallible
Simon Sapin <simon.sapin@octobus.net>
parents: 48730
diff changeset
    36
            stdout: std::io::stdout(),
48733
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    37
48731
f591b377375f rhg: Make Ui::new falliable, add Ui::new_infallible
Simon Sapin <simon.sapin@octobus.net>
parents: 48730
diff changeset
    38
            stderr: std::io::stderr(),
48733
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    39
            colors: ColorConfig::new(config)?,
48731
f591b377375f rhg: Make Ui::new falliable, add Ui::new_infallible
Simon Sapin <simon.sapin@octobus.net>
parents: 48730
diff changeset
    40
        })
f591b377375f rhg: Make Ui::new falliable, add Ui::new_infallible
Simon Sapin <simon.sapin@octobus.net>
parents: 48730
diff changeset
    41
    }
f591b377375f rhg: Make Ui::new falliable, add Ui::new_infallible
Simon Sapin <simon.sapin@octobus.net>
parents: 48730
diff changeset
    42
f591b377375f rhg: Make Ui::new falliable, add Ui::new_infallible
Simon Sapin <simon.sapin@octobus.net>
parents: 48730
diff changeset
    43
    /// Default to no color if color configuration errors.
f591b377375f rhg: Make Ui::new falliable, add Ui::new_infallible
Simon Sapin <simon.sapin@octobus.net>
parents: 48730
diff changeset
    44
    ///
f591b377375f rhg: Make Ui::new falliable, add Ui::new_infallible
Simon Sapin <simon.sapin@octobus.net>
parents: 48730
diff changeset
    45
    /// Useful when we’re already handling another error.
48733
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    46
    pub fn new_infallible(config: &Config) -> Self {
45362
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    47
        Ui {
48733
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    48
            // If using something else, also adapt `isatty()` below.
45362
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    49
            stdout: std::io::stdout(),
48733
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    50
45362
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    51
            stderr: std::io::stderr(),
48733
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    52
            colors: ColorConfig::new(config).unwrap_or(None),
45362
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    53
        }
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    54
    }
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    55
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    56
    /// Returns a buffered handle on stdout for faster batch printing
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    57
    /// operations.
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    58
    pub fn stdout_buffer(&self) -> StdoutBuffer<std::io::StdoutLock> {
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    59
        StdoutBuffer::new(self.stdout.lock())
45049
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    60
    }
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    61
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    62
    /// Write bytes to stdout
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    63
    pub fn write_stdout(&self, bytes: &[u8]) -> Result<(), UiError> {
45362
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    64
        let mut stdout = self.stdout.lock();
45049
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    65
45439
fbc373b7cbc3 rhg: fix `clippy` warnings
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45367
diff changeset
    66
        stdout.write_all(bytes).or_else(handle_stdout_error)?;
45049
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    67
45439
fbc373b7cbc3 rhg: fix `clippy` warnings
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45367
diff changeset
    68
        stdout.flush().or_else(handle_stdout_error)
45049
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    69
    }
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    70
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    71
    /// Write bytes to stderr
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    72
    pub fn write_stderr(&self, bytes: &[u8]) -> Result<(), UiError> {
45362
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    73
        let mut stderr = self.stderr.lock();
45049
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    74
45439
fbc373b7cbc3 rhg: fix `clippy` warnings
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45367
diff changeset
    75
        stderr.write_all(bytes).or_else(handle_stderr_error)?;
45049
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    76
45439
fbc373b7cbc3 rhg: fix `clippy` warnings
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45367
diff changeset
    77
        stderr.flush().or_else(handle_stderr_error)
45049
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    78
    }
48176
38deb65d4441 rhg: add ui.plain() and check it before showing relative paths in status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46591
diff changeset
    79
48733
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    80
    /// Write bytes to stdout with the given label
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    81
    ///
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    82
    /// Like the optional `label` parameter in `mercurial/ui.py`,
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    83
    /// this label influences the color used for this output.
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    84
    pub fn write_stdout_labelled(
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    85
        &self,
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    86
        bytes: &[u8],
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    87
        label: &str,
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    88
    ) -> Result<(), UiError> {
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    89
        if let Some(colors) = &self.colors {
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    90
            if let Some(effects) = colors.styles.get(label.as_bytes()) {
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    91
                if !effects.is_empty() {
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    92
                    return self
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    93
                        .write_stdout_with_effects(bytes, effects)
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    94
                        .or_else(handle_stdout_error);
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    95
                }
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    96
            }
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    97
        }
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    98
        self.write_stdout(bytes)
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    99
    }
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   100
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   101
    fn write_stdout_with_effects(
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   102
        &self,
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   103
        bytes: &[u8],
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   104
        effects: &[Effect],
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   105
    ) -> io::Result<()> {
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   106
        let stdout = &mut self.stdout.lock();
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   107
        let mut write_line = |line: &[u8], first: bool| {
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   108
            // `line` does not include the newline delimiter
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   109
            if !first {
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   110
                stdout.write_all(b"\n")?;
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   111
            }
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   112
            if line.is_empty() {
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   113
                return Ok(());
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   114
            }
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   115
            /// 0x1B == 27 == 0o33
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   116
            const ASCII_ESCAPE: &[u8] = b"\x1b";
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   117
            write_bytes!(stdout, b"{}[0", ASCII_ESCAPE)?;
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   118
            for effect in effects {
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   119
                write_bytes!(stdout, b";{}", effect)?;
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   120
            }
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   121
            write_bytes!(stdout, b"m")?;
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   122
            stdout.write_all(line)?;
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   123
            write_bytes!(stdout, b"{}[0m", ASCII_ESCAPE)
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   124
        };
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   125
        let mut lines = bytes.split(|&byte| byte == b'\n');
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   126
        if let Some(first) = lines.next() {
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   127
            write_line(first, true)?;
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   128
            for line in lines {
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   129
                write_line(line, false)?
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   130
            }
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   131
        }
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   132
        stdout.flush()
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   133
    }
48729
99b1dfc06571 rhg: Add support for HGPLAINEXPECT
Simon Sapin <simon.sapin@octobus.net>
parents: 48513
diff changeset
   134
}
99b1dfc06571 rhg: Add support for HGPLAINEXPECT
Simon Sapin <simon.sapin@octobus.net>
parents: 48513
diff changeset
   135
49513
467d9df98c68 rhg: centralize PlainInfo
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49512
diff changeset
   136
// TODO: pass the PlainInfo to call sites directly and
467d9df98c68 rhg: centralize PlainInfo
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49512
diff changeset
   137
// delete this function
48733
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   138
pub fn plain(opt_feature: Option<&str>) -> bool {
49513
467d9df98c68 rhg: centralize PlainInfo
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49512
diff changeset
   139
    let plain_info = PlainInfo::from_env();
467d9df98c68 rhg: centralize PlainInfo
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49512
diff changeset
   140
    match opt_feature {
467d9df98c68 rhg: centralize PlainInfo
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49512
diff changeset
   141
        None => plain_info.is_plain(),
467d9df98c68 rhg: centralize PlainInfo
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49512
diff changeset
   142
        Some(feature) => plain_info.is_feature_plain(feature),
48176
38deb65d4441 rhg: add ui.plain() and check it before showing relative paths in status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46591
diff changeset
   143
    }
45049
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   144
}
45362
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   145
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   146
/// A buffered stdout writer for faster batch printing operations.
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   147
pub struct StdoutBuffer<W: Write> {
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   148
    buf: io::BufWriter<W>,
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   149
}
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   150
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   151
impl<W: Write> StdoutBuffer<W> {
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   152
    pub fn new(writer: W) -> Self {
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   153
        let buf = io::BufWriter::new(writer);
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   154
        Self { buf }
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   155
    }
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   156
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   157
    /// Write bytes to stdout buffer
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   158
    pub fn write_all(&mut self, bytes: &[u8]) -> Result<(), UiError> {
45439
fbc373b7cbc3 rhg: fix `clippy` warnings
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45367
diff changeset
   159
        self.buf.write_all(bytes).or_else(handle_stdout_error)
45362
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   160
    }
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   161
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   162
    /// Flush bytes to stdout
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   163
    pub fn flush(&mut self) -> Result<(), UiError> {
45439
fbc373b7cbc3 rhg: fix `clippy` warnings
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45367
diff changeset
   164
        self.buf.flush().or_else(handle_stdout_error)
45362
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   165
    }
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   166
}
45366
10c36ead86f8 rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45362
diff changeset
   167
10c36ead86f8 rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45362
diff changeset
   168
/// Sometimes writing to stdout is not possible, try writing to stderr to
10c36ead86f8 rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45362
diff changeset
   169
/// signal that failure, otherwise just bail.
10c36ead86f8 rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45362
diff changeset
   170
fn handle_stdout_error(error: io::Error) -> Result<(), UiError> {
10c36ead86f8 rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45362
diff changeset
   171
    if let ErrorKind::BrokenPipe = error.kind() {
10c36ead86f8 rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45362
diff changeset
   172
        // This makes `| head` work for example
10c36ead86f8 rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45362
diff changeset
   173
        return Ok(());
10c36ead86f8 rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45362
diff changeset
   174
    }
10c36ead86f8 rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45362
diff changeset
   175
    let mut stderr = io::stderr();
10c36ead86f8 rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45362
diff changeset
   176
10c36ead86f8 rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45362
diff changeset
   177
    stderr
45984
fada33872b5b rhg: use `format_bytes!` for error messages
Raphaël Gomès <rgomes@octobus.net>
parents: 45528
diff changeset
   178
        .write_all(&format_bytes!(
fada33872b5b rhg: use `format_bytes!` for error messages
Raphaël Gomès <rgomes@octobus.net>
parents: 45528
diff changeset
   179
            b"abort: {}\n",
fada33872b5b rhg: use `format_bytes!` for error messages
Raphaël Gomès <rgomes@octobus.net>
parents: 45528
diff changeset
   180
            error.to_string().as_bytes()
fada33872b5b rhg: use `format_bytes!` for error messages
Raphaël Gomès <rgomes@octobus.net>
parents: 45528
diff changeset
   181
        ))
45439
fbc373b7cbc3 rhg: fix `clippy` warnings
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45367
diff changeset
   182
        .map_err(UiError::StderrError)?;
45366
10c36ead86f8 rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45362
diff changeset
   183
45439
fbc373b7cbc3 rhg: fix `clippy` warnings
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45367
diff changeset
   184
    stderr.flush().map_err(UiError::StderrError)?;
45366
10c36ead86f8 rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45362
diff changeset
   185
10c36ead86f8 rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45362
diff changeset
   186
    Err(UiError::StdoutError(error))
10c36ead86f8 rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45362
diff changeset
   187
}
45367
53af26aa5951 rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45366
diff changeset
   188
53af26aa5951 rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45366
diff changeset
   189
/// Sometimes writing to stderr is not possible.
53af26aa5951 rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45366
diff changeset
   190
fn handle_stderr_error(error: io::Error) -> Result<(), UiError> {
53af26aa5951 rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45366
diff changeset
   191
    // A broken pipe should not result in a error
53af26aa5951 rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45366
diff changeset
   192
    // like with `| head` for example
53af26aa5951 rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45366
diff changeset
   193
    if let ErrorKind::BrokenPipe = error.kind() {
53af26aa5951 rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45366
diff changeset
   194
        return Ok(());
53af26aa5951 rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45366
diff changeset
   195
    }
53af26aa5951 rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45366
diff changeset
   196
    Err(UiError::StdoutError(error))
53af26aa5951 rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45366
diff changeset
   197
}
45528
66756b34c06e rhg: add a `DebugData` `Command` to prepare the `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45440
diff changeset
   198
66756b34c06e rhg: add a `DebugData` `Command` to prepare the `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45440
diff changeset
   199
/// Encode rust strings according to the user system.
66756b34c06e rhg: add a `DebugData` `Command` to prepare the `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45440
diff changeset
   200
pub fn utf8_to_local(s: &str) -> Cow<[u8]> {
66756b34c06e rhg: add a `DebugData` `Command` to prepare the `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45440
diff changeset
   201
    // TODO encode for the user's system //
66756b34c06e rhg: add a `DebugData` `Command` to prepare the `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45440
diff changeset
   202
    let bytes = s.as_bytes();
66756b34c06e rhg: add a `DebugData` `Command` to prepare the `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45440
diff changeset
   203
    Cow::Borrowed(bytes)
66756b34c06e rhg: add a `DebugData` `Command` to prepare the `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45440
diff changeset
   204
}
48733
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   205
48770
f19be290756a rhg: signal when falling back in logs
Raphaël Gomès <rgomes@octobus.net>
parents: 48734
diff changeset
   206
/// Decode user system bytes to Rust string.
f19be290756a rhg: signal when falling back in logs
Raphaël Gomès <rgomes@octobus.net>
parents: 48734
diff changeset
   207
pub fn local_to_utf8(s: &[u8]) -> Cow<str> {
f19be290756a rhg: signal when falling back in logs
Raphaël Gomès <rgomes@octobus.net>
parents: 48734
diff changeset
   208
    // TODO decode from the user's system
f19be290756a rhg: signal when falling back in logs
Raphaël Gomès <rgomes@octobus.net>
parents: 48734
diff changeset
   209
    String::from_utf8_lossy(s)
f19be290756a rhg: signal when falling back in logs
Raphaël Gomès <rgomes@octobus.net>
parents: 48734
diff changeset
   210
}
f19be290756a rhg: signal when falling back in logs
Raphaël Gomès <rgomes@octobus.net>
parents: 48734
diff changeset
   211
48733
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   212
/// Should formatted output be used?
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   213
///
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   214
/// Note: rhg does not have the formatter mechanism yet,
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   215
/// but this is also used when deciding whether to use color.
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   216
pub fn formatted(config: &Config) -> Result<bool, HgError> {
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   217
    if let Some(formatted) = config.get_option(b"ui", b"formatted")? {
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   218
        Ok(formatted)
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   219
    } else {
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   220
        isatty(config)
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   221
    }
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   222
}
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   223
50539
74e4dbb0fcd5 rhg: make `rhg files` work if `ui.relative-files=true` is specified
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49981
diff changeset
   224
pub enum RelativePaths {
74e4dbb0fcd5 rhg: make `rhg files` work if `ui.relative-files=true` is specified
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49981
diff changeset
   225
    Legacy,
74e4dbb0fcd5 rhg: make `rhg files` work if `ui.relative-files=true` is specified
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49981
diff changeset
   226
    Bool(bool),
74e4dbb0fcd5 rhg: make `rhg files` work if `ui.relative-files=true` is specified
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49981
diff changeset
   227
}
74e4dbb0fcd5 rhg: make `rhg files` work if `ui.relative-files=true` is specified
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49981
diff changeset
   228
74e4dbb0fcd5 rhg: make `rhg files` work if `ui.relative-files=true` is specified
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49981
diff changeset
   229
pub fn relative_paths(config: &Config) -> Result<RelativePaths, HgError> {
74e4dbb0fcd5 rhg: make `rhg files` work if `ui.relative-files=true` is specified
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49981
diff changeset
   230
    Ok(match config.get(b"ui", b"relative-paths") {
74e4dbb0fcd5 rhg: make `rhg files` work if `ui.relative-files=true` is specified
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49981
diff changeset
   231
        None | Some(b"legacy") => RelativePaths::Legacy,
74e4dbb0fcd5 rhg: make `rhg files` work if `ui.relative-files=true` is specified
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49981
diff changeset
   232
        _ => RelativePaths::Bool(config.get_bool(b"ui", b"relative-paths")?),
74e4dbb0fcd5 rhg: make `rhg files` work if `ui.relative-files=true` is specified
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49981
diff changeset
   233
    })
74e4dbb0fcd5 rhg: make `rhg files` work if `ui.relative-files=true` is specified
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49981
diff changeset
   234
}
74e4dbb0fcd5 rhg: make `rhg files` work if `ui.relative-files=true` is specified
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 49981
diff changeset
   235
48733
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   236
fn isatty(config: &Config) -> Result<bool, HgError> {
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   237
    Ok(if config.get_bool(b"ui", b"nontty")? {
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   238
        false
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   239
    } else {
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   240
        atty::is(atty::Stream::Stdout)
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   241
    })
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   242
}
49981
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   243
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   244
/// Return the formatted bytestring corresponding to a pattern file warning,
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   245
/// as expected by the CLI.
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   246
pub(crate) fn format_pattern_file_warning(
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   247
    warning: &PatternFileWarning,
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   248
    repo: &Repo,
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   249
) -> Vec<u8> {
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   250
    match warning {
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   251
        PatternFileWarning::InvalidSyntax(path, syntax) => format_bytes!(
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   252
            b"{}: ignoring invalid syntax '{}'\n",
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   253
            get_bytes_from_path(path),
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   254
            &*syntax
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   255
        ),
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   256
        PatternFileWarning::NoSuchFile(path) => {
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   257
            let path = if let Ok(relative) =
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   258
                path.strip_prefix(repo.working_directory_path())
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   259
            {
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   260
                relative
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   261
            } else {
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   262
                &*path
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   263
            };
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   264
            format_bytes!(
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   265
                b"skipping unreadable pattern file '{}': \
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   266
                    No such file or directory\n",
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   267
                get_bytes_from_path(path),
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   268
            )
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   269
        }
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   270
    }
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   271
}
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   272
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   273
/// Print with `Ui` the formatted bytestring corresponding to a
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   274
/// sparse/narrow warning, as expected by the CLI.
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   275
pub(crate) fn print_narrow_sparse_warnings(
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   276
    narrow_warnings: &[sparse::SparseWarning],
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   277
    sparse_warnings: &[sparse::SparseWarning],
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   278
    ui: &Ui,
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   279
    repo: &Repo,
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   280
) -> Result<(), CommandError> {
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   281
    for warning in narrow_warnings.iter().chain(sparse_warnings) {
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   282
        match &warning {
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   283
            sparse::SparseWarning::RootWarning { context, line } => {
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   284
                let msg = format_bytes!(
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   285
                    b"warning: {} profile cannot use paths \"
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   286
                starting with /, ignoring {}\n",
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   287
                    context,
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   288
                    line
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   289
                );
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   290
                ui.write_stderr(&msg)?;
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   291
            }
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   292
            sparse::SparseWarning::ProfileNotFound { profile, rev } => {
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   293
                let msg = format_bytes!(
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   294
                    b"warning: sparse profile '{}' not found \"
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   295
                in rev {} - ignoring it\n",
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   296
                    profile,
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   297
                    rev
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   298
                );
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   299
                ui.write_stderr(&msg)?;
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   300
            }
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   301
            sparse::SparseWarning::Pattern(e) => {
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   302
                ui.write_stderr(&format_pattern_file_warning(e, repo))?;
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   303
            }
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   304
        }
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   305
    }
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   306
    Ok(())
364e78389653 rust-ui: refactor ui code for printing narrow/sparse warnings
Raphaël Gomès <rgomes@octobus.net>
parents: 49513
diff changeset
   307
}