2022-06-18 21:56:31 -07:00
|
|
|
use super::*;
|
2017-06-01 18:01:35 -07:00
|
|
|
|
2020-02-10 20:07:06 -08:00
|
|
|
use ansi_term::{ANSIGenericString, Color::*, Prefix, Style, Suffix};
|
2019-04-11 15:23:14 -07:00
|
|
|
use atty::Stream;
|
2017-06-01 18:01:35 -07:00
|
|
|
|
Gargantuan refactor (#522)
- Instead of changing the current directory with `env::set_current_dir`
to be implicitly inherited by subprocesses, we now use
`Command::current_dir` to set it explicitly. This feels much better,
since we aren't dependent on the implicit state of the process's
current directory.
- Subcommand execution is much improved.
- Added a ton of tests for config parsing, config execution, working
dir, and search dir.
- Error messages are improved. Many more will be colored.
- The Config is now onwed, instead of borrowing from the arguments and
the `clap::ArgMatches` object. This is a huge ergonomic improvement,
especially in tests, and I don't think anyone will notice.
- `--edit` now uses `$VISUAL`, `$EDITOR`, or `vim`, in that order,
matching git, which I think is what most people will expect.
- Added a cute `tmptree!{}` macro, for creating temporary directories
populated with directories and files for tests.
- Admitted that grammer is LL(k) and I don't know what `k` is.
2019-11-09 21:43:20 -08:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq)]
|
2019-09-21 15:35:03 -07:00
|
|
|
pub(crate) struct Color {
|
2017-06-01 18:01:35 -07:00
|
|
|
use_color: UseColor,
|
2021-09-16 06:44:40 -07:00
|
|
|
atty: bool,
|
|
|
|
style: Style,
|
2017-06-01 18:01:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Color {
|
2020-01-15 02:16:13 -08:00
|
|
|
fn restyle(self, style: Style) -> Self {
|
|
|
|
Self { style, ..self }
|
2017-06-01 18:01:35 -07:00
|
|
|
}
|
|
|
|
|
2020-01-15 02:16:13 -08:00
|
|
|
fn redirect(self, stream: Stream) -> Self {
|
|
|
|
Self {
|
2019-04-15 22:40:02 -07:00
|
|
|
atty: atty::is(stream),
|
2017-06-01 18:01:35 -07:00
|
|
|
..self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn effective_style(&self) -> Style {
|
|
|
|
if self.active() {
|
|
|
|
self.style
|
|
|
|
} else {
|
|
|
|
Style::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-15 02:16:13 -08:00
|
|
|
pub(crate) fn auto() -> Self {
|
|
|
|
Self {
|
2017-06-01 18:01:35 -07:00
|
|
|
use_color: UseColor::Auto,
|
2021-07-19 18:10:35 -07:00
|
|
|
..Color::default()
|
2017-06-01 18:01:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-15 02:16:13 -08:00
|
|
|
pub(crate) fn always() -> Self {
|
|
|
|
Self {
|
2017-06-01 18:01:35 -07:00
|
|
|
use_color: UseColor::Always,
|
2021-07-19 18:10:35 -07:00
|
|
|
..Color::default()
|
2017-06-01 18:01:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-15 02:16:13 -08:00
|
|
|
pub(crate) fn never() -> Self {
|
|
|
|
Self {
|
2017-06-01 18:01:35 -07:00
|
|
|
use_color: UseColor::Never,
|
2021-07-19 18:10:35 -07:00
|
|
|
..Color::default()
|
2017-06-01 18:01:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-15 02:16:13 -08:00
|
|
|
pub(crate) fn stderr(self) -> Self {
|
2017-06-01 18:01:35 -07:00
|
|
|
self.redirect(Stream::Stderr)
|
|
|
|
}
|
|
|
|
|
2020-01-15 02:16:13 -08:00
|
|
|
pub(crate) fn stdout(self) -> Self {
|
2017-06-01 18:01:35 -07:00
|
|
|
self.redirect(Stream::Stdout)
|
|
|
|
}
|
|
|
|
|
2020-01-15 02:16:13 -08:00
|
|
|
pub(crate) fn doc(self) -> Self {
|
2017-06-01 18:01:35 -07:00
|
|
|
self.restyle(Style::new().fg(Blue))
|
|
|
|
}
|
|
|
|
|
2020-01-15 02:16:13 -08:00
|
|
|
pub(crate) fn error(self) -> Self {
|
2017-06-01 18:01:35 -07:00
|
|
|
self.restyle(Style::new().fg(Red).bold())
|
|
|
|
}
|
|
|
|
|
2020-01-15 02:16:13 -08:00
|
|
|
pub(crate) fn warning(self) -> Self {
|
2019-04-18 11:48:02 -07:00
|
|
|
self.restyle(Style::new().fg(Yellow).bold())
|
|
|
|
}
|
|
|
|
|
2020-01-15 02:16:13 -08:00
|
|
|
pub(crate) fn banner(self) -> Self {
|
2017-06-01 18:01:35 -07:00
|
|
|
self.restyle(Style::new().fg(Cyan).bold())
|
|
|
|
}
|
|
|
|
|
2020-01-15 02:16:13 -08:00
|
|
|
pub(crate) fn command(self) -> Self {
|
2017-06-01 18:01:35 -07:00
|
|
|
self.restyle(Style::new().bold())
|
|
|
|
}
|
|
|
|
|
2020-01-15 02:16:13 -08:00
|
|
|
pub(crate) fn parameter(self) -> Self {
|
2017-06-01 18:01:35 -07:00
|
|
|
self.restyle(Style::new().fg(Cyan))
|
|
|
|
}
|
|
|
|
|
2020-01-15 02:16:13 -08:00
|
|
|
pub(crate) fn message(self) -> Self {
|
2017-06-01 18:01:35 -07:00
|
|
|
self.restyle(Style::new().bold())
|
|
|
|
}
|
|
|
|
|
2020-01-15 02:16:13 -08:00
|
|
|
pub(crate) fn annotation(self) -> Self {
|
2017-06-01 18:01:35 -07:00
|
|
|
self.restyle(Style::new().fg(Purple))
|
|
|
|
}
|
|
|
|
|
2020-01-15 02:16:13 -08:00
|
|
|
pub(crate) fn string(self) -> Self {
|
2017-06-01 18:01:35 -07:00
|
|
|
self.restyle(Style::new().fg(Green))
|
|
|
|
}
|
|
|
|
|
2021-10-31 23:18:11 -07:00
|
|
|
pub(crate) fn diff_added(self) -> Self {
|
|
|
|
self.restyle(Style::new().fg(Green))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn diff_deleted(self) -> Self {
|
|
|
|
self.restyle(Style::new().fg(Red))
|
|
|
|
}
|
|
|
|
|
2019-09-21 15:35:03 -07:00
|
|
|
pub(crate) fn active(&self) -> bool {
|
2017-06-01 18:01:35 -07:00
|
|
|
match self.use_color {
|
|
|
|
UseColor::Always => true,
|
2018-12-08 14:29:41 -08:00
|
|
|
UseColor::Never => false,
|
|
|
|
UseColor::Auto => self.atty,
|
2017-06-01 18:01:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-21 15:35:03 -07:00
|
|
|
pub(crate) fn paint<'a>(&self, text: &'a str) -> ANSIGenericString<'a, str> {
|
2017-06-01 18:01:35 -07:00
|
|
|
self.effective_style().paint(text)
|
|
|
|
}
|
|
|
|
|
2019-09-21 15:35:03 -07:00
|
|
|
pub(crate) fn prefix(&self) -> Prefix {
|
2017-06-01 18:01:35 -07:00
|
|
|
self.effective_style().prefix()
|
|
|
|
}
|
|
|
|
|
2019-09-21 15:35:03 -07:00
|
|
|
pub(crate) fn suffix(&self) -> Suffix {
|
2017-06-01 18:01:35 -07:00
|
|
|
self.effective_style().suffix()
|
|
|
|
}
|
|
|
|
}
|
2019-04-15 22:40:02 -07:00
|
|
|
|
|
|
|
impl Default for Color {
|
2020-01-15 02:16:13 -08:00
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
Gargantuan refactor (#522)
- Instead of changing the current directory with `env::set_current_dir`
to be implicitly inherited by subprocesses, we now use
`Command::current_dir` to set it explicitly. This feels much better,
since we aren't dependent on the implicit state of the process's
current directory.
- Subcommand execution is much improved.
- Added a ton of tests for config parsing, config execution, working
dir, and search dir.
- Error messages are improved. Many more will be colored.
- The Config is now onwed, instead of borrowing from the arguments and
the `clap::ArgMatches` object. This is a huge ergonomic improvement,
especially in tests, and I don't think anyone will notice.
- `--edit` now uses `$VISUAL`, `$EDITOR`, or `vim`, in that order,
matching git, which I think is what most people will expect.
- Added a cute `tmptree!{}` macro, for creating temporary directories
populated with directories and files for tests.
- Admitted that grammer is LL(k) and I don't know what `k` is.
2019-11-09 21:43:20 -08:00
|
|
|
use_color: UseColor::Auto,
|
2021-09-16 06:44:40 -07:00
|
|
|
atty: false,
|
|
|
|
style: Style::new(),
|
2019-04-15 22:40:02 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|