LoginSignup
0
1

More than 5 years have passed since last update.

Fabric のエラーや警告の色を変える

Last updated at Posted at 2017-01-19

Fabric は env.colorize_errors = True でエラーや警告に色がつくんだけど、ターミナルの色が赤だったりするとかぶってしまうので背景色つけたりしたい。(ちなみに自分は黒背景なので困らないんだけど)

ということで、 fabric.colors を上書きする。(将来のアップデートで期待した動作にならなくなったりするかもしれないのであまりいいやり方ではないかも)

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

エスケープシーケンスで \033[1;40;31 は bold, 黒背景, 赤文字で、 fabric.utils.abortfabric.colors.red を呼び出しているので fabric.colors.red\033[1;40;31%s\033[0;m を組み立てる関数になればいい。

これを読み込む

fabfile.py
# -*- coding: utf-8 -*-

from fabric.api import *

from colors import *

env.colorize_errors = True

@task
def abort_task():
    abort("タスク失敗! >_<")

これで赤背景でも文字が読める。

Screen Shot 2017-01-19 at 01.50.25.png

この方法を使えば色出力関数を自作できる。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1