Fabric colors errors and warnings with ʻenv.colorize_errors = True`, but if the terminal color is red, it will be fogged, so I want to color the background. (By the way, I'm on a black background so I'm not in trouble)
So, overwrite fabric.colors
. (It may not be the way you expected in a future update, so it may not be a good idea)
colors.py
# -*- coding: utf-8 -*-
import fabric.colors
def _wrap_with(color, background):
def inner(text, bold=False):
c = color
if background:
c = "%s;%s" % (background, c)
if bold:
c = "1;%s" % c
return "\033[%sm%s\033[0;m" % (c, text)
return inner
red = _wrap_with('31', '40')
green = _wrap_with('32', '40')
yellow = _wrap_with('33', '40')
blue = _wrap_with('34', '40')
magenta = _wrap_with('35', '40')
cyan = _wrap_with('36', '40')
white = _wrap_with('37', '40')
fabric.colors.red = red
fabric.colors.green = green
fabric.colors.yellow = yellow
fabric.colors.blue = blue
fabric.colors.magenta = magenta
fabric.colors.cyan = cyan
fabric.colors.white = white
In the escape sequence, \ 033 [1; 40; 31
is bold, black background, red letters, and fabric.utils.abort
calls fabric.colors.red
, so fabric.colors.red
Should be a function that constructs \ 033 [1; 40; 31% s \ 033 [0; m
".
Read this
fabfile.py
# -*- coding: utf-8 -*-
from fabric.api import *
from colors import *
env.colorize_errors = True
@task
def abort_task():
abort("Task failed!>_<")
Now you can read the characters even on a red background.
You can create your own color output function using this method.
Recommended Posts