From b34153fead43084bb6fd4d35e0c156ecb433093f Mon Sep 17 00:00:00 2001 From: DelaySystem Date: Sat, 7 Nov 2026 19:56:39 -0800 Subject: [PATCH 1/2] feat(tui): respect NO_COLOR for banner and status output Honor the NO_COLOR convention (https://no-color.org): when set to a non-empty value, print the banner, status tags and open/closed port lines without color codes. Gives users on light terminal themes (issue #778) and tools that cannot parse escapes a readable plain output. The colored crate already honors NO_COLOR itself; the tui-level check keeps that guarantee if the color backend changes. --- src/main.rs | 14 ++++-- src/scanner/mod.rs | 6 +-- src/tui.rs | 107 +++++++++++++++++++++++++++++++++++++-------- 3 files changed, 103 insertions(+), 24 deletions(-) diff --git a/src/main.rs b/src/main.rs index ee797ca9c..4c66156a4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ use rustscan::input::{self, Config, Opts, ScriptsRequired}; use rustscan::port_strategy::PortStrategy; use rustscan::scanner::{PortStatus, Scanner}; use rustscan::scripts::{init_scripts, Script, ScriptFile}; -use rustscan::tui::println_safe; +use rustscan::tui::{colors_enabled, println_safe}; use rustscan::{detail, funny_opening, output, warning}; use colorful::{Color, Colorful}; @@ -286,12 +286,20 @@ fn print_opening(opts: &Opts) { `-' `-'`-----'`----' `-' `----' `---' `-' `-'`-' `-' The Modern Day Port Scanner."#; - println_safe(format_args!("{}", s.gradient(Color::Green).bold())); + if colors_enabled() { + println_safe(format_args!("{}", s.gradient(Color::Green).bold())); + } else { + println_safe(format_args!("{s}")); + } let info = r#"________________________________________ : http://discord.skerritt.blog : : https://github.com/RustScan/RustScan : --------------------------------------"#; - println_safe(format_args!("{}", info.gradient(Color::Yellow).bold())); + if colors_enabled() { + println_safe(format_args!("{}", info.gradient(Color::Yellow).bold())); + } else { + println_safe(format_args!("{info}")); + } funny_opening!(); let config_path = opts diff --git a/src/scanner/mod.rs b/src/scanner/mod.rs index 78e7ff090..483a1b07b 100644 --- a/src/scanner/mod.rs +++ b/src/scanner/mod.rs @@ -1,7 +1,7 @@ //! Core functionality for actual scanning behaviour. use crate::generated::get_parsed_data; use crate::port_strategy::PortStrategy; -use crate::tui::println_safe; +use crate::tui::{colors_enabled, println_safe}; use log::debug; mod socket_iterator; @@ -527,7 +527,7 @@ impl Scanner { /// Formats and prints the port status fn fmt_ports(&self, socket: SocketAddr) { if self.print_open_ports && !self.greppable { - if self.accessible { + if self.accessible || !colors_enabled() { println_safe(format_args!("Open {socket}")); } else { println_safe(format_args!("Open {}", socket.to_string().purple())); @@ -538,7 +538,7 @@ impl Scanner { /// Prints a closed port (CLI output only, never in greppable mode). fn fmt_closed_port(&self, socket: SocketAddr) { if self.print_open_ports && !self.greppable { - if self.accessible { + if self.accessible || !colors_enabled() { println_safe(format_args!("Closed {socket}")); } else { println_safe(format_args!("Closed {}", socket.to_string().red())); diff --git a/src/tui.rs b/src/tui.rs index f38fd87bb..4ca6a7fe7 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -27,16 +27,42 @@ pub fn println_safe(args: std::fmt::Arguments<'_>) { } } +/// Whether colored output is enabled. +/// +/// Honors the `NO_COLOR` convention (): when the +/// variable is present and non-empty, colors are disabled. This keeps the +/// banner and status tags readable on light terminal themes and for tools +/// that cannot parse escape codes (see issue #778). +/// +/// The environment is read on every call so embedders never observe stale +/// state; output paths are cold enough that the lookup cost does not matter. +pub fn colors_enabled() -> bool { + colors_enabled_for(std::env::var_os("NO_COLOR").as_deref()) +} + +/// Pure form of [`colors_enabled`] over the variable's value, for tests. +/// +/// `None` (unset) and `Some("")` (empty) both mean colors stay on; any other +/// value disables them. +fn colors_enabled_for(no_color: Option<&std::ffi::OsStr>) -> bool { + no_color.is_none_or(|value| value.is_empty()) +} + /// Terminal User Interface Module for RustScan /// Defines macros to use #[macro_export] macro_rules! warning { ($name:expr) => { - $crate::tui::println_safe(format_args!( - "{} {}", - ansi_term::Colour::Red.bold().paint("[!]"), - $name - )); + if $crate::tui::colors_enabled() { + $crate::tui::println_safe(format_args!( + "{} {}", + ansi_term::Colour::Red.bold().paint("[!]"), + $name + )); + } else { + // NO_COLOR: keep the tag, drop the escape codes. + $crate::tui::println_safe(format_args!("[!] {}", $name)); + } }; ($name:expr, $greppable:expr, $accessible:expr) => { // if not greppable then print, otherwise no else statement so do not print. @@ -44,12 +70,15 @@ macro_rules! warning { if $accessible { // Don't print the ascii art $crate::tui::println_safe(format_args!("{}", $name)); - } else { + } else if $crate::tui::colors_enabled() { $crate::tui::println_safe(format_args!( "{} {}", ansi_term::Colour::Red.bold().paint("[!]"), $name )); + } else { + // NO_COLOR: keep the tag, drop the escape codes. + $crate::tui::println_safe(format_args!("[!] {}", $name)); } } }; @@ -58,11 +87,16 @@ macro_rules! warning { #[macro_export] macro_rules! detail { ($name:expr) => { - $crate::tui::println_safe(format_args!( - "{} {}", - ansi_term::Colour::Blue.bold().paint("[~]"), - $name - )); + if $crate::tui::colors_enabled() { + $crate::tui::println_safe(format_args!( + "{} {}", + ansi_term::Colour::Blue.bold().paint("[~]"), + $name + )); + } else { + // NO_COLOR: keep the tag, drop the escape codes. + $crate::tui::println_safe(format_args!("[~] {}", $name)); + } }; ($name:expr, $greppable:expr, $accessible:expr) => { // if not greppable then print, otherwise no else statement so do not print. @@ -70,12 +104,15 @@ macro_rules! detail { if $accessible { // Don't print the ascii art $crate::tui::println_safe(format_args!("{}", $name)); - } else { + } else if $crate::tui::colors_enabled() { $crate::tui::println_safe(format_args!( "{} {}", ansi_term::Colour::Blue.bold().paint("[~]"), $name )); + } else { + // NO_COLOR: keep the tag, drop the escape codes. + $crate::tui::println_safe(format_args!("[~] {}", $name)); } } }; @@ -84,11 +121,16 @@ macro_rules! detail { #[macro_export] macro_rules! output { ($name:expr) => { - $crate::tui::println_safe(format_args!( - "{} {}", - ansi_term::Colour::RGB(0, 255, 9).bold().paint("[>]"), - $name - )); + if $crate::tui::colors_enabled() { + $crate::tui::println_safe(format_args!( + "{} {}", + ansi_term::Colour::RGB(0, 255, 9).bold().paint("[>]"), + $name + )); + } else { + // NO_COLOR: keep the tag, drop the escape codes. + $crate::tui::println_safe(format_args!("[>] {}", $name)); + } }; ($name:expr, $greppable:expr, $accessible:expr) => { // if not greppable then print, otherwise no else statement so do not print. @@ -96,12 +138,15 @@ macro_rules! output { if $accessible { // Don't print the ascii art $crate::tui::println_safe(format_args!("{}", $name)); - } else { + } else if $crate::tui::colors_enabled() { $crate::tui::println_safe(format_args!( "{} {}", ansi_term::Colour::RGB(0, 255, 9).bold().paint("[>]"), $name )); + } else { + // NO_COLOR: keep the tag, drop the escape codes. + $crate::tui::println_safe(format_args!("[>] {}", $name)); } } }; @@ -147,3 +192,29 @@ macro_rules! funny_opening { $crate::tui::println_safe(format_args!("{}\n", random_quote)); }; } + +#[cfg(test)] +mod tests { + use super::colors_enabled_for; + use std::ffi::OsStr; + + // `colors_enabled` itself reads the process environment, which parallel + // tests cannot safely mutate, so only the pure helper is tested here. + + #[test] + fn colors_stay_on_when_no_color_is_unset_or_empty() { + assert!(colors_enabled_for(None)); + assert!(colors_enabled_for(Some(OsStr::new("")))); + } + + #[test] + fn colors_turn_off_when_no_color_has_any_value() { + for value in ["1", "true", "0", " "] { + assert!( + !colors_enabled_for(Some(OsStr::new(value))), + "NO_COLOR={:?} should disable colors", + value + ); + } + } +} From c4a9d5249c50bee79ce0bd8103b9e01e06ef5ff3 Mon Sep 17 00:00:00 2001 From: DelaySystem Date: Sun, 8 Nov 2026 19:03:20 -0800 Subject: [PATCH 2/2] Deduplicate NO_COLOR branches behind helpers The six macro arms and two banner blocks each repeated the same colors_enabled if/else, tripping Sonar's duplication gate (17.4%). Status macros now call tui::print_tagged, banner blocks call tui::print_colored_or_plain. No behavior change. --- src/main.rs | 14 ++------ src/tui.rs | 100 ++++++++++++++++++++++++---------------------------- 2 files changed, 49 insertions(+), 65 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4c66156a4..970572cc4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ use rustscan::input::{self, Config, Opts, ScriptsRequired}; use rustscan::port_strategy::PortStrategy; use rustscan::scanner::{PortStatus, Scanner}; use rustscan::scripts::{init_scripts, Script, ScriptFile}; -use rustscan::tui::{colors_enabled, println_safe}; +use rustscan::tui::{print_colored_or_plain, println_safe}; use rustscan::{detail, funny_opening, output, warning}; use colorful::{Color, Colorful}; @@ -286,20 +286,12 @@ fn print_opening(opts: &Opts) { `-' `-'`-----'`----' `-' `----' `---' `-' `-'`-' `-' The Modern Day Port Scanner."#; - if colors_enabled() { - println_safe(format_args!("{}", s.gradient(Color::Green).bold())); - } else { - println_safe(format_args!("{s}")); - } + print_colored_or_plain(s.gradient(Color::Green).bold(), s); let info = r#"________________________________________ : http://discord.skerritt.blog : : https://github.com/RustScan/RustScan : --------------------------------------"#; - if colors_enabled() { - println_safe(format_args!("{}", info.gradient(Color::Yellow).bold())); - } else { - println_safe(format_args!("{info}")); - } + print_colored_or_plain(info.gradient(Color::Yellow).bold(), info); funny_opening!(); let config_path = opts diff --git a/src/tui.rs b/src/tui.rs index 4ca6a7fe7..3767b1195 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -48,21 +48,40 @@ fn colors_enabled_for(no_color: Option<&std::ffi::OsStr>) -> bool { no_color.is_none_or(|value| value.is_empty()) } +/// Prints one of two pre-rendered lines depending on `NO_COLOR`. +/// +/// For call sites like the startup banner where the colored and plain +/// renderings differ by more than a tag. +pub fn print_colored_or_plain(colored: impl std::fmt::Display, plain: impl std::fmt::Display) { + if colors_enabled() { + println_safe(format_args!("{colored}")); + } else { + println_safe(format_args!("{plain}")); + } +} + +/// Prints `tag name`, colored unless `NO_COLOR` is set. +/// +/// Shared by the status macros below. +pub fn print_tagged( + painted_tag: impl std::fmt::Display, + plain_tag: &str, + name: impl std::fmt::Display, +) { + if colors_enabled() { + println_safe(format_args!("{painted_tag} {name}")); + } else { + // NO_COLOR: keep the tag, drop the escape codes. + println_safe(format_args!("{plain_tag} {name}")); + } +} + /// Terminal User Interface Module for RustScan /// Defines macros to use #[macro_export] macro_rules! warning { ($name:expr) => { - if $crate::tui::colors_enabled() { - $crate::tui::println_safe(format_args!( - "{} {}", - ansi_term::Colour::Red.bold().paint("[!]"), - $name - )); - } else { - // NO_COLOR: keep the tag, drop the escape codes. - $crate::tui::println_safe(format_args!("[!] {}", $name)); - } + $crate::tui::print_tagged(ansi_term::Colour::Red.bold().paint("[!]"), "[!]", $name); }; ($name:expr, $greppable:expr, $accessible:expr) => { // if not greppable then print, otherwise no else statement so do not print. @@ -70,15 +89,8 @@ macro_rules! warning { if $accessible { // Don't print the ascii art $crate::tui::println_safe(format_args!("{}", $name)); - } else if $crate::tui::colors_enabled() { - $crate::tui::println_safe(format_args!( - "{} {}", - ansi_term::Colour::Red.bold().paint("[!]"), - $name - )); } else { - // NO_COLOR: keep the tag, drop the escape codes. - $crate::tui::println_safe(format_args!("[!] {}", $name)); + $crate::tui::print_tagged(ansi_term::Colour::Red.bold().paint("[!]"), "[!]", $name); } } }; @@ -87,16 +99,7 @@ macro_rules! warning { #[macro_export] macro_rules! detail { ($name:expr) => { - if $crate::tui::colors_enabled() { - $crate::tui::println_safe(format_args!( - "{} {}", - ansi_term::Colour::Blue.bold().paint("[~]"), - $name - )); - } else { - // NO_COLOR: keep the tag, drop the escape codes. - $crate::tui::println_safe(format_args!("[~] {}", $name)); - } + $crate::tui::print_tagged(ansi_term::Colour::Blue.bold().paint("[~]"), "[~]", $name); }; ($name:expr, $greppable:expr, $accessible:expr) => { // if not greppable then print, otherwise no else statement so do not print. @@ -104,15 +107,12 @@ macro_rules! detail { if $accessible { // Don't print the ascii art $crate::tui::println_safe(format_args!("{}", $name)); - } else if $crate::tui::colors_enabled() { - $crate::tui::println_safe(format_args!( - "{} {}", - ansi_term::Colour::Blue.bold().paint("[~]"), - $name - )); } else { - // NO_COLOR: keep the tag, drop the escape codes. - $crate::tui::println_safe(format_args!("[~] {}", $name)); + $crate::tui::print_tagged( + ansi_term::Colour::Blue.bold().paint("[~]"), + "[~]", + $name, + ); } } }; @@ -121,16 +121,11 @@ macro_rules! detail { #[macro_export] macro_rules! output { ($name:expr) => { - if $crate::tui::colors_enabled() { - $crate::tui::println_safe(format_args!( - "{} {}", - ansi_term::Colour::RGB(0, 255, 9).bold().paint("[>]"), - $name - )); - } else { - // NO_COLOR: keep the tag, drop the escape codes. - $crate::tui::println_safe(format_args!("[>] {}", $name)); - } + $crate::tui::print_tagged( + ansi_term::Colour::RGB(0, 255, 9).bold().paint("[>]"), + "[>]", + $name, + ); }; ($name:expr, $greppable:expr, $accessible:expr) => { // if not greppable then print, otherwise no else statement so do not print. @@ -138,15 +133,12 @@ macro_rules! output { if $accessible { // Don't print the ascii art $crate::tui::println_safe(format_args!("{}", $name)); - } else if $crate::tui::colors_enabled() { - $crate::tui::println_safe(format_args!( - "{} {}", - ansi_term::Colour::RGB(0, 255, 9).bold().paint("[>]"), - $name - )); } else { - // NO_COLOR: keep the tag, drop the escape codes. - $crate::tui::println_safe(format_args!("[>] {}", $name)); + $crate::tui::print_tagged( + ansi_term::Colour::RGB(0, 255, 9).bold().paint("[>]"), + "[>]", + $name, + ); } } };