forked from DGNum/colmena
cli: Allow configuring output colorization
We now follow the <https://bixense.com/clicolors> standard.
This commit is contained in:
parent
dc57b489ea
commit
f253e6eb18
5 changed files with 59 additions and 16 deletions
13
Cargo.lock
generated
13
Cargo.lock
generated
|
@ -118,6 +118,18 @@ dependencies = [
|
||||||
"vec_map",
|
"vec_map",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "clicolors-control"
|
||||||
|
version = "1.0.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "90082ee5dcdd64dc4e9e0d37fbf3ee325419e39c0092191e0393df65518f741e"
|
||||||
|
dependencies = [
|
||||||
|
"atty",
|
||||||
|
"lazy_static",
|
||||||
|
"libc",
|
||||||
|
"winapi",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "colmena"
|
name = "colmena"
|
||||||
version = "0.3.0-pre"
|
version = "0.3.0-pre"
|
||||||
|
@ -126,6 +138,7 @@ dependencies = [
|
||||||
"async-trait",
|
"async-trait",
|
||||||
"atty",
|
"atty",
|
||||||
"clap",
|
"clap",
|
||||||
|
"clicolors-control",
|
||||||
"console 0.13.0",
|
"console 0.13.0",
|
||||||
"const_format",
|
"const_format",
|
||||||
"env_logger",
|
"env_logger",
|
||||||
|
|
|
@ -11,6 +11,7 @@ ansi-to-html = "0.1.0"
|
||||||
async-trait = "0.1.42"
|
async-trait = "0.1.42"
|
||||||
atty = "0.2"
|
atty = "0.2"
|
||||||
clap = "2.33.3"
|
clap = "2.33.3"
|
||||||
|
clicolors-control = "1"
|
||||||
console = "0.13.0"
|
console = "0.13.0"
|
||||||
const_format = "0.2.22"
|
const_format = "0.2.22"
|
||||||
env_logger = "0.8.2"
|
env_logger = "0.8.2"
|
||||||
|
|
|
@ -20,7 +20,7 @@ in rustPlatform.buildRustPackage rec {
|
||||||
src = lib.cleanSource ./.;
|
src = lib.cleanSource ./.;
|
||||||
};
|
};
|
||||||
|
|
||||||
cargoSha256 = "sha256-HGqecerb5LgnPhetqBYEmDKpJBkgzLS+iviVkDgVyGI=";
|
cargoSha256 = "sha256-YE+0jsdkzRaiUhPC71WBe/KcYERNy7nME26+ejrPRlc=";
|
||||||
|
|
||||||
postInstall = lib.optionalString (stdenv.hostPlatform == stdenv.buildPlatform) ''
|
postInstall = lib.optionalString (stdenv.hostPlatform == stdenv.buildPlatform) ''
|
||||||
mkdir completions
|
mkdir completions
|
||||||
|
|
45
src/cli.rs
45
src/cli.rs
|
@ -1,7 +1,10 @@
|
||||||
//! Global CLI Setup.
|
//! Global CLI Setup.
|
||||||
|
|
||||||
|
use std::env;
|
||||||
|
|
||||||
use clap::{App, AppSettings, Arg, ArgMatches, SubCommand};
|
use clap::{App, AppSettings, Arg, ArgMatches, SubCommand};
|
||||||
use const_format::concatcp;
|
use const_format::concatcp;
|
||||||
|
use env_logger::fmt::WriteStyle;
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
|
|
||||||
use crate::command;
|
use crate::command;
|
||||||
|
@ -104,7 +107,18 @@ pub fn build_cli(include_internal: bool) -> App<'static, 'static> {
|
||||||
.help("Show debug information for Nix commands")
|
.help("Show debug information for Nix commands")
|
||||||
.long_help("Passes --show-trace to Nix commands")
|
.long_help("Passes --show-trace to Nix commands")
|
||||||
.global(true)
|
.global(true)
|
||||||
.takes_value(false));
|
.takes_value(false))
|
||||||
|
.arg(Arg::with_name("color")
|
||||||
|
.long("color")
|
||||||
|
.help("When to colorize the output")
|
||||||
|
.long_help(r#"When to colorize the output. By default, Colmena enables colorized output when the terminal supports it.
|
||||||
|
|
||||||
|
It's also possible to specify the preference using environment variables. See <https://bixense.com/clicolors>.
|
||||||
|
"#)
|
||||||
|
.value_name("WHEN")
|
||||||
|
.possible_values(&["auto", "always", "never"])
|
||||||
|
.default_value("auto")
|
||||||
|
.global(true));
|
||||||
|
|
||||||
if include_internal {
|
if include_internal {
|
||||||
app = app.subcommand(SubCommand::with_name("gen-completions")
|
app = app.subcommand(SubCommand::with_name("gen-completions")
|
||||||
|
@ -140,6 +154,9 @@ pub async fn run() {
|
||||||
let mut app = build_cli(true);
|
let mut app = build_cli(true);
|
||||||
let matches = app.clone().get_matches();
|
let matches = app.clone().get_matches();
|
||||||
|
|
||||||
|
set_color_pref(matches.value_of("color").unwrap());
|
||||||
|
init_logging();
|
||||||
|
|
||||||
handle_command!(apply, matches);
|
handle_command!(apply, matches);
|
||||||
handle_command!("apply-local", apply_local, matches);
|
handle_command!("apply-local", apply_local, matches);
|
||||||
handle_command!(build, matches);
|
handle_command!(build, matches);
|
||||||
|
@ -221,3 +238,29 @@ fn gen_help_markdown() {
|
||||||
println!("</div></pre>");
|
println!("</div></pre>");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn set_color_pref(cli: &str) {
|
||||||
|
if cli != "auto" {
|
||||||
|
clicolors_control::set_colors_enabled(cli == "always");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn init_logging() {
|
||||||
|
if env::var("RUST_LOG").is_err() {
|
||||||
|
// HACK
|
||||||
|
env::set_var("RUST_LOG", "info")
|
||||||
|
}
|
||||||
|
|
||||||
|
// make env_logger conform to our detection logic
|
||||||
|
let style = if clicolors_control::colors_enabled() {
|
||||||
|
WriteStyle::Always
|
||||||
|
} else {
|
||||||
|
WriteStyle::Never
|
||||||
|
};
|
||||||
|
|
||||||
|
env_logger::builder()
|
||||||
|
.format_timestamp(None)
|
||||||
|
.format_module_path(false)
|
||||||
|
.write_style(style)
|
||||||
|
.init();
|
||||||
|
}
|
||||||
|
|
14
src/main.rs
14
src/main.rs
|
@ -1,7 +1,5 @@
|
||||||
#![deny(unused_must_use)]
|
#![deny(unused_must_use)]
|
||||||
|
|
||||||
use std::env;
|
|
||||||
|
|
||||||
mod nix;
|
mod nix;
|
||||||
mod cli;
|
mod cli;
|
||||||
mod command;
|
mod command;
|
||||||
|
@ -12,17 +10,5 @@ mod util;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
init_logging();
|
|
||||||
cli::run().await;
|
cli::run().await;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init_logging() {
|
|
||||||
if env::var("RUST_LOG").is_err() {
|
|
||||||
// HACK
|
|
||||||
env::set_var("RUST_LOG", "info")
|
|
||||||
}
|
|
||||||
env_logger::builder()
|
|
||||||
.format_timestamp(None)
|
|
||||||
.format_module_path(false)
|
|
||||||
.init();
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue