Skip to content

Latest commit

 

History

History
175 lines (124 loc) · 3.94 KB

File metadata and controls

175 lines (124 loc) · 3.94 KB

🎨 dcolor

Python 3.8+ License MIT Status

dcolor — colored terminal output with hex/rgb support

(─‿‿─)

     ____
    / __ \ ______ ____   / /____  _____
   / / / // ____// __ \ / // __ \/ ___/
  / /_/ // /____/ /_/ // // /_/ / /
 /_____/ \____/ \____//_/ \____/_/

📦 installation

pip install dcolor-drawiks

📑 quick start

from dcolor import color

print(color("hello!", "#ff0000"))
print(color("hello!", (255, 0, 0)))
print(color("hello!", "red"))
print(color("hello!", "#ff0000", "bold", "underline"))

🧩 features

  • 🎨 hex colors"#ff0000" or "#f00"
  • 🌈 rgb colors(255, 0, 0)
  • 🔢 256-color palette196 (0-255)
  • 🏷️ named colors"red", "cyan", "orange"...
  • 🎭 background colorsbg="#ff0000"
  • stylesbold, italic, underline, strike, dim, blink
  • 📱 auto terminal detection — no colors in piped output
  • 🪟 Windows — native Windows 10+ support (no dependencies)
  • 🚫 no dependencies — stdlib only

📖 usage

hex color

from dcolor import color

print(color("error", "#ff0000"))
print(color("success", "#00ff00"))
print(color("info", "#3b82f6"))

short hex also works:

print(color("text", "#f00"))  # same as #ff0000

rgb color

print(color("text", (255, 165, 0)))

256-color palette

print(color("text", 196))              # orange
print(color("text", fg=196, bg=21))    # orange on blue

colors 0-255 supported (see 256 terminal colors)

named colors

print(color("text", "red"))
print(color("text", "cyan"))
print(color("text", "orange"))

available names: black, red, green, yellow, blue, magenta, cyan, white, grey, orange, pink, purple, brown, lime, teal

styles

print(color("text", "red", "bold"))
print(color("text", "#ff0000", "bold", "underline"))
print(color("text", "cyan", "italic", "strike"))

available styles: bold, dim, italic, underline, blink, strike

background color

print(color("text", fg="#ffffff", bg="#ff0000"))  # white on red
print(color("text", bg="#00ff00"))                 # background only

style only (no color)

print(color("text", None, "bold"))
print(color("text", None, "underline", "italic"))

strip ansi codes

from dcolor import strip

raw = color("hello", "#ff0000", "bold")
clean = strip(raw)  # "hello"

auto terminal detection

by default dcolor doesn't color output if it's piped or redirected:

python script.py > log.txt          # no colors
python script.py | cat              # no colors
python script.py                    # colors (in terminal)

force colors on:

print(color("text", "#ff0000", force=True))  # always colored

on Windows 10+ (Build 10586+) works natively. on older Windows versions, escape codes render as garbage.


💡 examples

from dcolor import color

print(color("[ERROR]",   "#ff4444", "bold"), "something went wrong")
print(color("[SUCCESS]", "#44ff44", "bold"), "everything is fine")
print(color("[WARN]",    "#ffaa00", "bold"), "watch out")
print(color("[INFO]",    "#888888"),         "just fyi")

use in dlogger

from dlogger import logger
from dcolor import color

logger.info(color("payment received", "#44ff44", "bold"))
logger.error(color("connection failed", "#ff4444"))

📜 license

MIT