週刊Elixirライブラリ2015-14

Elixirのライブラリの解説を週ごとにお届けする記事です。
解説が間違っていた場合には、コメントか@hayabusa333にご連絡くださると嬉しいです。

今回はColorsについて

# Colors is 何?
Colorsは、Elixirのiexにて出力される結果に色をつけるライブラリとなります。
go-colorsが元となっています。

# 実行環境
OS:OS X Yosemite
Erlang:Eshell V6.5, OTP-Version 18
Elixir:v1.0.4

# Colorsの実行を行うための新規プロジェクトの作成

$ mix new my_colors
$ cd new my_colors

# HexにてColorsをインストールするために設定ファイルの記載を行う

$ vim mix.exs

mix.exsの内容は下記となります。

defmodule MyColors.Mixfile do
  use Mix.Project

  def project do
    [app: :my_colors,
     version: "0.0.1",
     elixir: "~> 1.0",
     build_embedded: Mix.env == :prod,
     start_permanent: Mix.env == :prod,
     deps: deps]
  end

  # Configuration for the OTP application
  #
  # Type `mix help compile.app` for more information
  def application do
    [applications: [:logger, :colors]]
  end

  # Dependencies can be Hex packages:
  #
  #   {:mydep, "~> 0.3.0"}
  #
  # Or git/path repositories:
  #
  #   {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"}
  #
  # Type `mix help deps` for more examples and options
  defp deps do
    [{:colors, "~> 1.0.1"}]
  end
end

# 依存関係の解決を行います。

$ mix deps.get
$ mix deps.compile

# Colorsを使ってみる

$ iex -S mix

iex(1)> IO.puts "Elixir" |> Colors.blue
Elixir ## 実際には青色で表示される
:ok
iex(2)> IO.puts "Elixir" |> Colors.blue |> Colors.underline    
Elixir ##実際には青色で下線が表示される
:ok
iex(3)> IO.puts "Elixir" |> Colors.blue |> Colors.underline |> Colors.magenta_bg 
Elixir ## 実際には青色で下線が表示され、バックグラウンドがマゼンタで塗られる
:ok
## 順番を入れ替えても問題はない
iex(4)> IO.puts "Elixir" |> Colors.underline |> Colors.blue |> Colors.magenta_bg                    
Elixir ## 実際には青色で下線が表示され、バックグラウンドがマゼンタで塗られる
## 色指定を二回行うと先に指定したほうが有効
iex(5)> IO.puts "Elixir" |> Colors.blue |> Colors.white
Elixir ## 実際には青色で表示
:ok

#実行可能な書式

協調文字 結果
bold 太字
italic イタリック
underline 下線
inverse 写像
strikethrough 取り消し線
文字色 結果
white 白に近い灰色
grey 灰色
black
blue
cyan シアン
green
magenta マゼンタ
red
yellow
背景色 結果
white_bg 白に近い灰色
grey_bg 灰色
black_bg
blue_bg
cyan_bg シアン
green_bg
magenta_bg マゼンタ
red_bg
yellow_bg

# まとめ
今回はElixirにて表示される結果の文字色を変えるライブラリを紹介させていただきました。
何かしら協調したい、または結果として確認してもらい場所などに使用すると良いでしょうか。色々と使用できると思うので使用してはいかがでしょうか。

それでは皆さま、良いElixirライフを