rust/rhg/src/ui.rs
author Arseniy Alekseyev <aalekseyev@janestreet.com>
Tue, 20 Sep 2022 18:16:50 -0400
changeset 49512 6939d5ed20e0
parent 48770 f19be290756a
child 49513 467d9df98c68
permissions -rw-r--r--
rhg: central treatment of PLAIN and PLAINEXCEPT
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;
45984
fada33872b5b rhg: use `format_bytes!` for error messages
Raphaël Gomès <rgomes@octobus.net>
parents: 45528
diff changeset
     3
use format_bytes::format_bytes;
48733
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
     4
use format_bytes::write_bytes;
48730
1aaf11e35aec rhg: Pass a &Config to Ui::new
Simon Sapin <simon.sapin@octobus.net>
parents: 48729
diff changeset
     5
use hg::config::Config;
48731
f591b377375f rhg: Make Ui::new falliable, add Ui::new_infallible
Simon Sapin <simon.sapin@octobus.net>
parents: 48730
diff changeset
     6
use hg::errors::HgError;
48729
99b1dfc06571 rhg: Add support for HGPLAINEXPECT
Simon Sapin <simon.sapin@octobus.net>
parents: 48513
diff changeset
     7
use hg::utils::files::get_bytes_from_os_string;
45528
66756b34c06e rhg: add a `DebugData` `Command` to prepare the `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45440
diff changeset
     8
use std::borrow::Cow;
48176
38deb65d4441 rhg: add ui.plain() and check it before showing relative paths in status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46591
diff changeset
     9
use std::env;
45049
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    10
use std::io;
45362
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    11
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
    12
45362
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    13
pub struct Ui {
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    14
    stdout: std::io::Stdout,
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    15
    stderr: std::io::Stderr,
48733
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    16
    colors: Option<ColorConfig>,
45362
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    17
}
45049
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    18
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    19
/// 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
    20
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
    21
    /// 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
    22
    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
    23
    /// 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
    24
    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
    25
}
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    26
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    27
/// 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
    28
impl Ui {
48733
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    29
    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
    30
        Ok(Ui {
48733
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    31
            // 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
    32
            stdout: std::io::stdout(),
48733
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    33
48731
f591b377375f rhg: Make Ui::new falliable, add Ui::new_infallible
Simon Sapin <simon.sapin@octobus.net>
parents: 48730
diff changeset
    34
            stderr: std::io::stderr(),
48733
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    35
            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
    36
        })
f591b377375f rhg: Make Ui::new falliable, add Ui::new_infallible
Simon Sapin <simon.sapin@octobus.net>
parents: 48730
diff changeset
    37
    }
f591b377375f rhg: Make Ui::new falliable, add Ui::new_infallible
Simon Sapin <simon.sapin@octobus.net>
parents: 48730
diff changeset
    38
f591b377375f rhg: Make Ui::new falliable, add Ui::new_infallible
Simon Sapin <simon.sapin@octobus.net>
parents: 48730
diff changeset
    39
    /// 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
    40
    ///
f591b377375f rhg: Make Ui::new falliable, add Ui::new_infallible
Simon Sapin <simon.sapin@octobus.net>
parents: 48730
diff changeset
    41
    /// 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
    42
    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
    43
        Ui {
48733
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    44
            // 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
    45
            stdout: std::io::stdout(),
48733
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    46
45362
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    47
            stderr: std::io::stderr(),
48733
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    48
            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
    49
        }
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    50
    }
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    51
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    52
    /// 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
    53
    /// operations.
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
    54
    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
    55
        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
    56
    }
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    57
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    58
    /// 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
    59
    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
    60
        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
    61
45439
fbc373b7cbc3 rhg: fix `clippy` warnings
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45367
diff changeset
    62
        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
    63
45439
fbc373b7cbc3 rhg: fix `clippy` warnings
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45367
diff changeset
    64
        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
    65
    }
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    66
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    67
    /// 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
    68
    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
    69
        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
    70
45439
fbc373b7cbc3 rhg: fix `clippy` warnings
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45367
diff changeset
    71
        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
    72
45439
fbc373b7cbc3 rhg: fix `clippy` warnings
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45367
diff changeset
    73
        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
    74
    }
48176
38deb65d4441 rhg: add ui.plain() and check it before showing relative paths in status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46591
diff changeset
    75
48733
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    76
    /// 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
    77
    ///
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    78
    /// 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
    79
    /// 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
    80
    pub fn write_stdout_labelled(
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    81
        &self,
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    82
        bytes: &[u8],
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    83
        label: &str,
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    84
    ) -> Result<(), UiError> {
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    85
        if let Some(colors) = &self.colors {
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    86
            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
    87
                if !effects.is_empty() {
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    88
                    return self
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    89
                        .write_stdout_with_effects(bytes, effects)
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    90
                        .or_else(handle_stdout_error);
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    91
                }
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    92
            }
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    93
        }
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    94
        self.write_stdout(bytes)
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
    fn write_stdout_with_effects(
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    98
        &self,
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
    99
        bytes: &[u8],
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   100
        effects: &[Effect],
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   101
    ) -> io::Result<()> {
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   102
        let stdout = &mut self.stdout.lock();
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   103
        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
   104
            // `line` does not include the newline delimiter
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   105
            if !first {
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   106
                stdout.write_all(b"\n")?;
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   107
            }
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   108
            if line.is_empty() {
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   109
                return Ok(());
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   110
            }
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   111
            /// 0x1B == 27 == 0o33
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   112
            const ASCII_ESCAPE: &[u8] = b"\x1b";
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   113
            write_bytes!(stdout, b"{}[0", ASCII_ESCAPE)?;
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   114
            for effect in effects {
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   115
                write_bytes!(stdout, b";{}", effect)?;
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   116
            }
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   117
            write_bytes!(stdout, b"m")?;
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   118
            stdout.write_all(line)?;
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   119
            write_bytes!(stdout, b"{}[0m", ASCII_ESCAPE)
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
        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
   122
        if let Some(first) = lines.next() {
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   123
            write_line(first, true)?;
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   124
            for line in lines {
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   125
                write_line(line, false)?
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   126
            }
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   127
        }
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   128
        stdout.flush()
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   129
    }
48729
99b1dfc06571 rhg: Add support for HGPLAINEXPECT
Simon Sapin <simon.sapin@octobus.net>
parents: 48513
diff changeset
   130
}
99b1dfc06571 rhg: Add support for HGPLAINEXPECT
Simon Sapin <simon.sapin@octobus.net>
parents: 48513
diff changeset
   131
49512
6939d5ed20e0 rhg: central treatment of PLAIN and PLAINEXCEPT
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48770
diff changeset
   132
/// Return whether plain mode is active.
6939d5ed20e0 rhg: central treatment of PLAIN and PLAINEXCEPT
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48770
diff changeset
   133
///
6939d5ed20e0 rhg: central treatment of PLAIN and PLAINEXCEPT
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48770
diff changeset
   134
/// Plain mode means that all configuration variables which affect
6939d5ed20e0 rhg: central treatment of PLAIN and PLAINEXCEPT
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48770
diff changeset
   135
/// the behavior and output of Mercurial should be
6939d5ed20e0 rhg: central treatment of PLAIN and PLAINEXCEPT
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48770
diff changeset
   136
/// ignored. Additionally, the output should be stable,
6939d5ed20e0 rhg: central treatment of PLAIN and PLAINEXCEPT
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48770
diff changeset
   137
/// reproducible and suitable for use in scripts or applications.
6939d5ed20e0 rhg: central treatment of PLAIN and PLAINEXCEPT
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48770
diff changeset
   138
///
6939d5ed20e0 rhg: central treatment of PLAIN and PLAINEXCEPT
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48770
diff changeset
   139
/// The only way to trigger plain mode is by setting either the
6939d5ed20e0 rhg: central treatment of PLAIN and PLAINEXCEPT
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48770
diff changeset
   140
/// `HGPLAIN' or `HGPLAINEXCEPT' environment variables.
6939d5ed20e0 rhg: central treatment of PLAIN and PLAINEXCEPT
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48770
diff changeset
   141
///
6939d5ed20e0 rhg: central treatment of PLAIN and PLAINEXCEPT
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48770
diff changeset
   142
/// The return value can either be
6939d5ed20e0 rhg: central treatment of PLAIN and PLAINEXCEPT
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48770
diff changeset
   143
/// - False if HGPLAIN is not set, or feature is in HGPLAINEXCEPT
6939d5ed20e0 rhg: central treatment of PLAIN and PLAINEXCEPT
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48770
diff changeset
   144
/// - False if feature is disabled by default and not included in HGPLAIN
6939d5ed20e0 rhg: central treatment of PLAIN and PLAINEXCEPT
Arseniy Alekseyev <aalekseyev@janestreet.com>
parents: 48770
diff changeset
   145
/// - True otherwise
48733
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   146
pub fn plain(opt_feature: Option<&str>) -> bool {
48729
99b1dfc06571 rhg: Add support for HGPLAINEXPECT
Simon Sapin <simon.sapin@octobus.net>
parents: 48513
diff changeset
   147
    if let Some(except) = env::var_os("HGPLAINEXCEPT") {
99b1dfc06571 rhg: Add support for HGPLAINEXPECT
Simon Sapin <simon.sapin@octobus.net>
parents: 48513
diff changeset
   148
        opt_feature.map_or(true, |feature| {
99b1dfc06571 rhg: Add support for HGPLAINEXPECT
Simon Sapin <simon.sapin@octobus.net>
parents: 48513
diff changeset
   149
            get_bytes_from_os_string(except)
99b1dfc06571 rhg: Add support for HGPLAINEXPECT
Simon Sapin <simon.sapin@octobus.net>
parents: 48513
diff changeset
   150
                .split(|&byte| byte == b',')
99b1dfc06571 rhg: Add support for HGPLAINEXPECT
Simon Sapin <simon.sapin@octobus.net>
parents: 48513
diff changeset
   151
                .all(|exception| exception != feature.as_bytes())
99b1dfc06571 rhg: Add support for HGPLAINEXPECT
Simon Sapin <simon.sapin@octobus.net>
parents: 48513
diff changeset
   152
        })
99b1dfc06571 rhg: Add support for HGPLAINEXPECT
Simon Sapin <simon.sapin@octobus.net>
parents: 48513
diff changeset
   153
    } else {
48176
38deb65d4441 rhg: add ui.plain() and check it before showing relative paths in status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46591
diff changeset
   154
        env::var_os("HGPLAIN").is_some()
38deb65d4441 rhg: add ui.plain() and check it before showing relative paths in status
Pulkit Goyal <7895pulkit@gmail.com>
parents: 46591
diff changeset
   155
    }
45049
513b3ef277a3 rhg: add RootCommand using hg-core FindRoot operation to prepare `hg root`
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
   156
}
45362
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   157
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   158
/// 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
   159
pub struct StdoutBuffer<W: Write> {
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   160
    buf: io::BufWriter<W>,
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
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   163
impl<W: Write> StdoutBuffer<W> {
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   164
    pub fn new(writer: W) -> Self {
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   165
        let buf = io::BufWriter::new(writer);
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   166
        Self { buf }
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   167
    }
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   168
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   169
    /// Write bytes to stdout buffer
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   170
    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
   171
        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
   172
    }
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   173
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   174
    /// Flush bytes to stdout
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   175
    pub fn flush(&mut self) -> Result<(), UiError> {
45439
fbc373b7cbc3 rhg: fix `clippy` warnings
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45367
diff changeset
   176
        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
   177
    }
eb55274d3650 rhg: add buffered stdout writing possibility
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45049
diff changeset
   178
}
45366
10c36ead86f8 rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45362
diff changeset
   179
10c36ead86f8 rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45362
diff changeset
   180
/// 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
   181
/// signal that failure, otherwise just bail.
10c36ead86f8 rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45362
diff changeset
   182
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
   183
    if let ErrorKind::BrokenPipe = error.kind() {
10c36ead86f8 rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45362
diff changeset
   184
        // This makes `| head` work for example
10c36ead86f8 rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45362
diff changeset
   185
        return Ok(());
10c36ead86f8 rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45362
diff changeset
   186
    }
10c36ead86f8 rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45362
diff changeset
   187
    let mut stderr = io::stderr();
10c36ead86f8 rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45362
diff changeset
   188
10c36ead86f8 rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45362
diff changeset
   189
    stderr
45984
fada33872b5b rhg: use `format_bytes!` for error messages
Raphaël Gomès <rgomes@octobus.net>
parents: 45528
diff changeset
   190
        .write_all(&format_bytes!(
fada33872b5b rhg: use `format_bytes!` for error messages
Raphaël Gomès <rgomes@octobus.net>
parents: 45528
diff changeset
   191
            b"abort: {}\n",
fada33872b5b rhg: use `format_bytes!` for error messages
Raphaël Gomès <rgomes@octobus.net>
parents: 45528
diff changeset
   192
            error.to_string().as_bytes()
fada33872b5b rhg: use `format_bytes!` for error messages
Raphaël Gomès <rgomes@octobus.net>
parents: 45528
diff changeset
   193
        ))
45439
fbc373b7cbc3 rhg: fix `clippy` warnings
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45367
diff changeset
   194
        .map_err(UiError::StderrError)?;
45366
10c36ead86f8 rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45362
diff changeset
   195
45439
fbc373b7cbc3 rhg: fix `clippy` warnings
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45367
diff changeset
   196
    stderr.flush().map_err(UiError::StderrError)?;
45366
10c36ead86f8 rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45362
diff changeset
   197
10c36ead86f8 rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45362
diff changeset
   198
    Err(UiError::StdoutError(error))
10c36ead86f8 rhg: extract function handle_stdout_error
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45362
diff changeset
   199
}
45367
53af26aa5951 rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45366
diff changeset
   200
53af26aa5951 rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45366
diff changeset
   201
/// 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
   202
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
   203
    // 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
   204
    // like with `| head` for example
53af26aa5951 rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45366
diff changeset
   205
    if let ErrorKind::BrokenPipe = error.kind() {
53af26aa5951 rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45366
diff changeset
   206
        return Ok(());
53af26aa5951 rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45366
diff changeset
   207
    }
53af26aa5951 rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45366
diff changeset
   208
    Err(UiError::StdoutError(error))
53af26aa5951 rhg: handle broken pipe error for stderr
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45366
diff changeset
   209
}
45528
66756b34c06e rhg: add a `DebugData` `Command` to prepare the `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45440
diff changeset
   210
66756b34c06e rhg: add a `DebugData` `Command` to prepare the `rhg debugdata` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents: 45440
diff changeset
   211
/// 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
   212
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
   213
    // 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
   214
    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
   215
    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
   216
}
48733
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   217
48770
f19be290756a rhg: signal when falling back in logs
Raphaël Gomès <rgomes@octobus.net>
parents: 48734
diff changeset
   218
/// 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
   219
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
   220
    // 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
   221
    String::from_utf8_lossy(s)
f19be290756a rhg: signal when falling back in logs
Raphaël Gomès <rgomes@octobus.net>
parents: 48734
diff changeset
   222
}
f19be290756a rhg: signal when falling back in logs
Raphaël Gomès <rgomes@octobus.net>
parents: 48734
diff changeset
   223
48733
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   224
/// Should formatted output be used?
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   225
///
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   226
/// 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
   227
/// 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
   228
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
   229
    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
   230
        Ok(formatted)
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   231
    } else {
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   232
        isatty(config)
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   233
    }
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   234
}
39c447e03dbc rhg: Add support for colored output
Simon Sapin <simon.sapin@octobus.net>
parents: 48731
diff changeset
   235
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
}