6
6

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.

コマンドの出力結果に色を付ける

Last updated at Posted at 2014-12-12

目的

gitbundleなどの

  • コンソールに出力する場合は色をつける
  • パイプなど、結果を利用する場合は色を付けない

という挙動を実現する

方法

Rubyの場合

コード

hello.rb
# !/usr/bin/env ruby

message = 'hello, world'

if STDOUT.tty?
  puts "\033[32m#{message}\033[0m"
else
  puts message
end

結果

hello_rb.png

node.jsの場合

コード

hello.js
# !/usr/bin/env node

var message = 'hello, world'

if (process.stdout.isTTY) {
  console.log("\033[32m" + message + "\033[0m");
} else {
  console.log(message);
}

結果

hello_js.png

結論

自作コマンドが捗る

参考

6
6
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
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?