19
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Goでエスケープシーケンスを使ってコマンドプロンプトに色を出す

Last updated at Posted at 2014-05-13

コマンドプロンプトで簡単に色を出したい

Windowsのコマンドプロンプトで色を出したいならコンソールAPIのSetConsoleTextAttributeを使えばいいのですがエスケープシーケンスと比べると手間がかかります。
そこで、エスケープシーケンスの一部をパースして色を出すGoのライブラリを作りました。

サンプル1

使い方はとても簡単で、ansicolor.NewAnsiColorWriter(os.Stdout)で生成したWriterにFprintf系の関数でエスケープシーケンスの文字列を出力するだけです。
またWindows以外では引数のio.Writerに対してWrite関数を呼ぶだけのラッパーなのでOSの違いを意識する必要はありません。

main.go
package main

import (
    "fmt"
    "os"

    "github.com/shiena/ansicolor"
)

func main() {
    w := ansicolor.NewAnsiColorWriter(os.Stdout)
    text := "%sforeground %sbold%s %sbackground%s\n"
    fmt.Fprintf(w, text, "\x1b[31m", "\x1b[1m", "\x1b[22m", "\x1b[41;32m", "\x1b[0m")
    fmt.Fprintf(w, text, "\x1b[32m", "\x1b[1m", "\x1b[22m", "\x1b[42;31m", "\x1b[0m")
    fmt.Fprintf(w, text, "\x1b[33m", "\x1b[1m", "\x1b[22m", "\x1b[43;34m", "\x1b[0m")
    fmt.Fprintf(w, text, "\x1b[34m", "\x1b[1m", "\x1b[22m", "\x1b[44;33m", "\x1b[0m")
    fmt.Fprintf(w, text, "\x1b[35m", "\x1b[1m", "\x1b[22m", "\x1b[45;36m", "\x1b[0m")
    fmt.Fprintf(w, text, "\x1b[36m", "\x1b[1m", "\x1b[22m", "\x1b[46;35m", "\x1b[0m")
    fmt.Fprintf(w, text, "\x1b[37m", "\x1b[1m", "\x1b[22m", "\x1b[47;30m", "\x1b[0m")
}

サンプル2

github.com/wsxiaoys/terminal/colorと組合せると更に簡単になります。

main.go
package main

import (
	"os"

	"github.com/shiena/ansicolor"
	"github.com/wsxiaoys/terminal/color"
)

func main() {
	w := ansicolor.NewAnsiColorWriter(os.Stdout)
	color.Fprintf(w, "@{r}foreground @{r!}bold@{|} @{gR}background@{|}\n")
	color.Fprintf(w, "@{g}foreground @{g!}bold@{|} @{rG}background@{|}\n")
	color.Fprintf(w, "@{y}foreground @{y!}bold@{|} @{bY}background@{|}\n")
	color.Fprintf(w, "@{b}foreground @{b!}bold@{|} @{yB}background@{|}\n")
	color.Fprintf(w, "@{m}foreground @{m!}bold@{|} @{cM}background@{|}\n")
	color.Fprintf(w, "@{c}foreground @{c!}bold@{|} @{mC}background@{|}\n")
	color.Fprintf(w, "@{w}foreground @{w!}bold@{|} @{kW}background@{|}\n")
}

スクリーンショット

サンプルを実行したスクリーンショットです。

screenshot

参考

エスケープシーケンスのパースについてANSICONとwacが参考になりました。

GoからSetConsoleTextAttributeやその他のコンソールAPIを呼ぶ方法はgo-colortext packageが参考になりました。

これらのソフトウェアがなければ作ることができなかったと思います。

2014/05/16追記

作成の動機はThe Platinum Searcherのissue

The Platinum Searcherissue#23で他のコマンドで色を出せているから対応してほしいと要望があり、興味が湧いたので調べたのがきっかけでした。
すると参考に挙げた材料が揃ってしまったので作らない訳にはいきません。
そして出来上がったものは汎用的になったので別パッケージに切り出しansicolorパッケージとして、これを組み込んだPRで取り込んでもらえました。
そのおかげで最新版ではWindowsでもカラフルな検索結果を表示できるので皆さんも使いましょう!

19
20
2

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
19
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?