うさぎ好きエンジニアの備忘録

うさぎたちに日々癒されているエンジニアが業務で直面したもの & 個人的な学習メモを残していきます。

fabric2 で出力結果の色を変更したいという願望

fabric2で出力結果に対して色をつけたいという願望が私にはあります。

結論

今の所標準サポートはしてない。

なぜ

Fabric v1のときは以下のように from fabric.colors import cyan のように好きな色をimportすることで出力結果に色を付けることができた。

from fabric.colors import cyan

@task
def color_test():
    fabric.utils.puts(cyan("This is test output"))

が、このAPIはv2になった際に削除された模様。

That said, it seems highly plausible we’ll end up vendoring such a library in the future to offer internal color support, at which point “baked-in” color helpers would again be within easy reach.

将来的にはサポートするかも的なことを言ってるのでそれを待つのが良さげ。 ↓みたいな感じで独自モジュール作って対応するのもありな気がする。

# coding: UTF-8

BLACK     = '\033[30m'
RED       = '\033[31m'
GREEN     = '\033[32m'
YELLOW    = '\033[33m'
BLUE      = '\033[34m'
PURPLE    = '\033[35m'
CYAN      = '\033[36m'
WHITE     = '\033[37m'
END       = '\033[0m'

def red(output):
    return RED + output + END

def green(output):
    return GREEN + output + END

def yellow(output):
    return YELLOW + output + END

def blue(output):
    return BLUE + output + END

def purple(output):
    return PURPLE + output + END

def cyan(output):
    return CYAN + output + END

def white(output):
    return WHITE + output + END