2007/08/30
screen の hardstatus の文字化けを強引に直すパッチ
現在再生中の曲名を screen の hardstatus に表示しようと思って以下のような設定を書いてみました。
~/.screenrc:
hardstatus alwayslastline "[%02c] %-Lw%{=b bw} %n%f* %t %[-}%+Lw %= %?%0`"
backtick 0 0 10 ${HOME}/tmp/nowplaying
backtick 機能によって %0\` が現在再生中の曲名になるはずなんですが、どうも hardstatus 表示部で正しいエンコード処理をやっていないみたいで、見事に文字化けしてしまいます。
しょうがないので、曲再生スクリプトで
echo $title > ~/tmp/nowplaying # 日本語文字列を含む
とやっていたのを、 mecab と kakasi で無理矢理ローマ字に変換してみたのですが(*)、
(*) 元々 mecab だけで処理しようと思ってたんですが、 mecab にはデフォルトでローマ字変換機能がないようなので、後づけで kakasi を使うことにしました。普通はどちらか一方を使うべきです。
romaji_title=`echo "$title" | mecab -Owakati | nkf -e -W | kakasi -Ha -Ka -Ea -ka -ieuc | nkf -E -w` echo $romaji_title > ~/tmp/nowplaying
やっぱり格好悪いので、強引に直してみました。よくわかっていませんが、 encoding utf-8 な環境で、入力が必ず UTF8 になる場合のみ正しく動作します(たぶん)。この手の処理には知識が全くないのですが、それでも明らかに邪道とわかる直し方をしています。おそらく encoding.c の何らかの関数を使えばいいと思うのですが、 GNU の中規模ソフトウェアにはよくあるように、 screen も例にもれずソースコードがわけわからんっていう…
ちなみに日本語文字が入っていると幅が正しく計算されないのでレンダリングがおかしくなります(特に右詰めで文字列を表示する場合)。気になる場合は公式で直るのを待ちましょう(すでに直ってたりして、あるいは ./configure のオプションが変だったとか)。
screen-utf8-hstatus.patch:
--- display.c.orig 2007-08-29 22:56:45.000000000 +0900
+++ display.c 2007-08-29 22:58:28.000000000 +0900
@@ -120,6 +120,7 @@
#endif
int captionalways;
int hardstatusemu = HSTATUS_IGNORE;
+int utf8chars = 0;
/*
* Default layer management
@@ -903,7 +904,10 @@
AddCStr(D_CE0);
goto addedutf8;
}
- AddUtf8(c);
+ if (utf8chars)
+ AddChar(c);
+ else
+ AddUtf8(c);
goto addedutf8;
}
# endif
@@ -2390,10 +2394,13 @@
if (D_blocked)
return;
+ if (D_encoding == UTF8)
+ utf8chars = 1;
+
if (D_HS && D_has_hstatus == HSTATUS_HS)
{
if (!D_hstatus && (str == 0 || *str == 0))
- return;
+ goto exit;
debug("ShowHStatus: using HS\n");
SetRendition(&mchar_null);
InsertMode(0);
@@ -2401,7 +2408,7 @@
AddCStr(D_DS);
D_hstatus = 0;
if (str == 0 || *str == 0)
- return;
+ goto exit;
AddCStr2(D_TS, 0);
max = D_WS > 0 ? D_WS : (D_width - !D_CLP);
if ((int)strlen(str) > max)
@@ -2440,6 +2447,9 @@
debug("ShowHStatus: using message\n");
Msg(0, "%s", str);
}
+
+exit:
+ utf8chars = 0;
}
- The URL to Trackback this entry is:
- http://dev.ariel-networks.com/Members/matsuyama/screen-hardstatus/tbping
Re:screen の hardstatus の文字化けを強引に直すパッチ
Posted by
matsuyama
at
2007-08-30 01:24
正しく直すには FromUtf8 を msg に対してかけてやれば良いと思われる。現在調査中。