xmas_tree


mwedアドベントカレンダー15日目の記事です。

treeコマンドの結果がチカチカしたらxmas_treeなのでは?!

perl版

xmas_tree_perl.sh
function xmas_tree () {
  tree $* | perl -ne '
  binmode(STDOUT, ":utf8");
  utf8::decode($_);
  @string = split //, $_;
  foreach $char(@string) {
    $r = int(rand(15)) ;
    if($r == 1){
      $s = "\e[5m\e[37m";
      $e = "\e[m\e[m";
    } elsif ($r == 2) {
      $s = "\e[5m\e[35m";
      $e = "\e[m\e[m";
    } elsif ($r == 3) {
      $s = "\e[5m\e[33m";
      $e = "\e[m\e[m";
    } elsif ($r == 4) {
      $s = "\e[5m\e[31m";
      $e = "\e[m\e[m";
    } else {
      $s = "\e[32m";
      $e = "\e[m";
    }
    print "$s$char$e";
  }
  '
}

ruby版

xmas_tree_ruby.sh
function xmas_tree {
  tree $* | ruby -ne '
    string = $_.split ""
    string.each do |char|
      case rand(15)
    when 1 then
      s = "\e[5m\e[37m"
        e = "\e[m\e[m"
      when 2 then
        s = "\e[5m\e[35m"
        e = "\e[m\e[m"
      when 3 then
        s = "\e[5m\e[33m"
        e = "\e[m\e[m"
      when 4 then
        s = "\e[5m\e[31m"
        e = "\e[m\e[m"
      else
        s = "\e[32m"
        e = "\e[m"
    end
      print "#{s}#{char}#{e}"
    end
  '
}

使い方

コンソールにペタッ!
実行!

動かない?

treeコマンドがない?

Macにはデフォルトでインストールされてないので、適宜インストールしてくださいまし。便利ですよ。
homebrewを使っていればbrew install treeでインストールできます。

チカチカしない?

コンソールの設定でBLINKしないようになっているものもありますので、適宜設定を変えてくださいまし。

最後に

くだらなくてすみません

明日は@HamaguchiRyosukeさんの「新卒同期5人でHackDayのハッカソンに参加してみた」です。