Skip to content

Commit

Permalink
Fix ANSI color codes are printed when not a tty
Browse files Browse the repository at this point in the history
Fixes #1095.
  • Loading branch information
svenstaro committed Jul 5, 2023
1 parent 91d5bd1 commit e228fee
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 39 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
<!-- next-header -->

## [Unreleased] - ReleaseDate
- Fix ANSI color codes are printed when not a tty [#1095](https://github.com/svenstaro/miniserve/pull/1095)
- Allow parameters to be provided via environment variables [#1160](https://github.com/svenstaro/miniserve/pull/1160)

## [0.23.2] - 2023-04-28
Expand Down
54 changes: 29 additions & 25 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ chrono-humanize = "0.2"
clap = { version = "4", features = ["derive", "cargo", "wrap_help", "deprecated", "env"] }
clap_complete = "4"
clap_mangen = "0.2"
colored = "2"
comrak = { version = "0.18", default-features = false }
fast_qr = { version = "0.9", features = ["svg"] }
futures = "0.3"
if-addrs = "0.10"
hex = "0.4"
http = "0.2"
httparse = "1"
if-addrs = "0.10"
libflate = "1"
log = "0.4"
maud = "0.25"
Expand All @@ -55,7 +56,6 @@ socket2 = "0.5"
strum = { version = "0.25", features = ["derive"] }
tar = "0.4"
thiserror = "1"
yansi = "0.5"
zip = { version = "0.6.5", default-features = false }

[features]
Expand Down
26 changes: 14 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use actix_web::{
use actix_web_httpauth::middleware::HttpAuthentication;
use anyhow::Result;
use clap::{crate_version, CommandFactory, Parser};
use colored::*;
use fast_qr::QRBuilder;
use log::{error, warn};
use yansi::{Color, Paint};

mod archive;
mod args;
Expand Down Expand Up @@ -57,10 +57,6 @@ fn main() -> Result<()> {

#[actix_web::main(miniserve)]
async fn run(miniserve_config: MiniserveConfig) -> Result<(), ContextualError> {
if cfg!(windows) && !Paint::enable_windows_ascii() {
Paint::disable();
}

let log_level = if miniserve_config.verbose {
simplelog::LevelFilter::Info
} else {
Expand All @@ -69,9 +65,15 @@ async fn run(miniserve_config: MiniserveConfig) -> Result<(), ContextualError> {

simplelog::TermLogger::init(
log_level,
simplelog::Config::default(),
simplelog::ConfigBuilder::new()
.set_time_format_rfc2822()
.build(),
simplelog::TerminalMode::Mixed,
simplelog::ColorChoice::Auto,
if io::stdout().is_terminal() {
simplelog::ColorChoice::Auto
} else {
simplelog::ColorChoice::Never
},
)
.or_else(|_| simplelog::SimpleLogger::init(log_level, simplelog::Config::default()))
.expect("Couldn't initialize logger");
Expand Down Expand Up @@ -102,7 +104,7 @@ async fn run(miniserve_config: MiniserveConfig) -> Result<(), ContextualError> {

println!(
"{name} v{version}",
name = Paint::new("miniserve").bold(),
name = "miniserve".bold(),
version = crate_version!()
);
if !miniserve_config.path_explicitly_chosen {
Expand Down Expand Up @@ -176,7 +178,7 @@ async fn run(miniserve_config: MiniserveConfig) -> Result<(), ContextualError> {

let display_sockets = socket_addresses
.iter()
.map(|sock| Color::Green.paint(sock.to_string()).bold().to_string())
.map(|sock| sock.to_string().green().bold().to_string())
.collect::<Vec<_>>();

let srv = actix_web::HttpServer::new(move || {
Expand Down Expand Up @@ -220,13 +222,13 @@ async fn run(miniserve_config: MiniserveConfig) -> Result<(), ContextualError> {

println!("Bound to {}", display_sockets.join(", "));

println!("Serving path {}", Color::Yellow.paint(path_string).bold());
println!("Serving path {}", path_string.yellow().bold());

println!(
"Available at (non-exhaustive list):\n {}\n",
display_urls
.iter()
.map(|url| Color::Green.paint(url).bold().to_string())
.map(|url| url.green().bold().to_string())
.collect::<Vec<_>>()
.join("\n "),
);
Expand All @@ -239,7 +241,7 @@ async fn run(miniserve_config: MiniserveConfig) -> Result<(), ContextualError> {
{
match QRBuilder::new(url.clone()).ecl(consts::QR_EC_LEVEL).build() {
Ok(qr) => {
println!("QR code for {}:", Color::Green.paint(url).bold());
println!("QR code for {}:", url.green().bold());
qr.print();
}
Err(e) => {
Expand Down

0 comments on commit e228fee

Please sign in to comment.