Fix NO_COLOR environment variable support (#3278)

* Fix NO_COLOR environment variable support

Implement proper NO_COLOR support by adding a nocolor function and
configuration trigger that dynamically replaces the colorize function's
behavior when colors are disabled. This approach avoids performance
overhead by modifying function behavior at runtime rather than adding
conditional checks to each colorize call.

Fixes #3142

* fixes
pull/3355/head
Disconnect3d 2 months ago committed by GitHub
parent 0f8f7ede8e
commit 735c65a22d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -11,6 +11,7 @@ from typing import Dict
from typing import List
from typing import NamedTuple
import pwndbg
from pwndbg.lib.config import Parameter
from . import theme
@ -133,6 +134,10 @@ def colorize(x: str, color: str) -> str:
return color + terminateWith(str(x), color) + NORMAL
def nocolor(x: str, color: str) -> str:
return x
# Taken from https://stackoverflow.com/a/14693789
ansi_escape_8bit = re.compile(
r"(?:\x1B[@-Z\\-_]|[\x80-\x9A\x9C-\x9F]|(?:\x1B\[|\x9B)[0-?]*[ -/]*[@-~])"
@ -150,6 +155,17 @@ disable_colors = theme.add_param(
)
@pwndbg.config.trigger(disable_colors)
def _disable_colors_trigger():
if disable_colors:
if not hasattr(colorize, "original_code"):
colorize.original_code = colorize.__code__
colorize.__code__ = nocolor.__code__
else:
if hasattr(colorize, "original_code"):
colorize.__code__ = colorize.original_code
def generateColorFunctionInner(
old: Callable[[object], str], new: Callable[[str], str]
) -> Callable[[object], str]:

Loading…
Cancel
Save