watchコマンドdiff表示をカラーに

久々の熱暴走した家鯖のモニタに また watchコマンド使ってたのですが
http://d.hatena.ne.jp/tkuro/20110519/1305753099
パッと見に上がってるのか下がってるのかわからんことがあったので色つけてみました。

ncursesの基本

  • initscr() でスタート
  • 色使うなら start_color()
  • ペア色にする場合は init_pairで作る。 init_pair(番号, foreground, background)
  • attron(...) で色つける。ペア参照はマクロ COLOR_PAIR(番号)が必要。

必要なのはこんなところかな

watchはどこに?

watchコマンドは procpsパッケージの中にあるらしい。

% apt-get source procps

で...
とりあえずキャラクタコードの大小で色を変えてみよう。

あててみた

diff --git a/watch.c b/watch.c
index 436d213..bdb2496 100644
--- a/watch.c
+++ b/watch.c
@@ -116,6 +116,9 @@ static void init_ansi_colors(void)

        for (i = 0; i < 8; i++)
                init_pair(i + 1, ncurses_colors[i], -1);
+
+       init_pair(10, COLOR_WHITE, COLOR_RED);
+       init_pair(11, COLOR_RED, COLOR_BLUE);
 }

 static void set_ansi_attribute(const int attrib)
@@ -493,6 +496,7 @@ static int run_command(char *restrict command, char **restrict command_argv)
                                exit_early = (unsigned char)c != oldc;
 #endif
                        }
+                       int attr_stands = 0;
                        if (flags & WATCH_DIFF) {
 #ifdef WITH_WATCH8BIT
                                cchar_t oldc;
@@ -511,16 +515,21 @@ static int run_command(char *restrict command, char **restrict command_argv)
                                        ((flags & WATCH_CUMUL)
                                         && (oldch & A_ATTRIBUTES)));
 #endif
+                               if (attr && (unsigned char)c > oldc) {
+                                       attr_stands = COLOR_PAIR(10);
+                               } else {
+                                       attr_stands = COLOR_PAIR(11);
+                               }
                        }
-                       if (attr)
-                               standout();
+                       if (attr)
+                               attron(attr_stands);
 #ifdef WITH_WATCH8BIT
                        addnwstr((wchar_t *) & c, 1);
 #else
                        addch(c);
 #endif
                        if (attr)
-                               standend();
+                               attroff(attr_stands);
 #ifdef WITH_WATCH8BIT
                        if (wcwidth(c) == 0) {
                                x--;

configure & make で新しい watch コマンドげとー

でけたあ

誰得?

微妙だw

まとめ

いらんことしてる場合じゃない