Next: text.csv - CSVテーブル, Previous: sxml.serializer - SXMLからXMLとXHTMLのシリアライゼーション, Up: ライブラリモジュール - ユーティリティ [Contents][Index]
text.console - テキスト端末制御このモジュールは文字端末を制御する簡単なインタフェースを提供します。 今のところ、vt100互換端末とWindowsコンソールがサポートされています。
curses等の外部ライブラリには依存しておらず、Gaucheだけで使うことができますが、
出来ることは限られています。
例えばシフトキーだけが押されたタイミングでイベントを受け取ることはできません。
より細かい制御には、何らかの外部拡張ライブラリが必要になるでしょう。
このモジュールの機能の例としては、 Gaucheソースのexamplesディレクトリにあるsnake.scmを見てください。
{text.console} vt100互換端末を表します。このクラスのインスタンスは 以降のジェネリックファンクションの “console” 引数に渡せます。
端末に接続されている入力ポートです。デフォルトは標準入力です。
端末に接続されている出力ポートです。デフォルトは標準出力です。
端末は、特殊キーが押された場合、ESCから始まるエスケープシーケンスを送って来ます。
実際にESCキーが押された場合と区別するために、入力の間隔を測っています。
後続の入力がinput-delayμs以内に来なかった場合はそこでシーケンスが終了したと
みなし、受け取っているシーケンスが有効なエスケープシーケンスを構成しなければ
個別にキー入力されたとみなします。デフォルトは1000、すなわち1msです。
Windowsコンソールを表します。このクラス自体は全てのプラットフォームで 定義されていますが、有用なメソッドはWindowsネイティブのランタイムでしか提供されません。
パブリックなスロットはありません。
アプリケーションは実行時にどの種類のコンソールが利用可能かを見極めなければなりません。 推奨される手順は次のとおりです。
has-windows-console?が真の値を返したなら、<windows-console>の
インスタンスを作ります。
has-windows-console?はWindowsプラットフォーム以外では常に#fを
返すので、cond-expandは不要です。
TERMを調べます。それが定義されていて、vt100-compatible?を
満たすなら、<vt100>のインスタンスを作ります。
(Windows上でも<vt100>端末を使うことになる可能性もあります。
例えばgoshがMSYSシェルから使われている場合です。)
次の手続きは上の手順を実装しています。
{text.console} 実行中のプロセスで使える端末のインスタンスを作成して返します。
適切な端末クラスが無い場合、振る舞いはif-not-availableキーワード引数に
依存します。デフォルトである:errorの場合はエラーが報告されます。
#fの場合はこの手続きが#fを返します。
{text.console}
環境変数TERMの値を文字列で受け取り、それが<vt100>端末として
扱えるなら#tを、そうでなければ#fを返します。
{text.console}
コンソールの制御を取得した状態で、procをconsoleを唯一の引数として
呼び出します。コンソールはmodeで指定されるモードにセットされます。
modeはwith-terminal-modeが受け付けるシンボル、
すなわちraw、rare、cookedのいずれかでなければなりません。
省略された場合はrareになります。
このモードは、端末でのエコーを抑止し、ほぼ全てのキーストロークを
プログラムにそのまま渡しますが、端末制御機能はシステム側で処理します
(割り込みのCtrl-C、プロセス一時停止のCtrl-Zなど。実際のキー割り当ては
端末制御で変更可能です。詳しくはgauche.termios - 端末の制御参照。)
procが捕捉されない例外を投げた場合、このジェネリック関数は 端末モードを元に戻します。ただ、スクリーンはクリアされません。
{text.console} 文字charをコンソールの現在のカーソル位置に出力し、カーソルをその分進めます。
{text.console} 文字列stringコンソールの現在のカーソル位置に出力し、カーソルをその分進めます。
{text.console} 可能なら、端末のビープ音を鳴らすか、画面をフラッシュさせます(visual bell)。
{text.console} コンソールからキー入力を読み取ります。何らかのキーが押されるまでブロックします。
戻り値は以下の値のいずれかです。
その文字に該当するキーが押された場合。
コントロールキーと一緒に押された場合は、制御文字が返ります。
例えばユーザがCtrl-Aをタイプしたら#\x01が返ります。
特殊キーの場合です。以下のキーがサポートされています。
KEY_UP, KEY_DOWN, KEY_LEFT, KEY_RIGHT,
KEY_HOME, KEY_END, KEY_INS, KEY_DEL,
KEY_PGDN, KEY_PGUP, KEY_F1, KEY_F2,
KEY_F3, KEY_F4, KEY_F5, KEY_F6,
KEY_F7, KEY_F8, KEY_F9, KEY_F10,
KEY_F11, KEY_F12。
(註: DELETEキーは通常#\x7fにマップされていますが、
端末設定で変更可能です)。
ALTと文字のリスト文字キーがAltキーと同時に押されたことを示します。
例えばユーザがAlt-aを押したなら、(ALT #\a)が返されます。
入力がクローズされたことを示します。
ALT以外のモディファイアキーは別扱いではなく返される文字コードにエンコードされます。
CAPSLOCKがoffであるとき、ユーザが
a、Shift+a、Ctrl+aをタイプしたとすると、
戻り値はそれぞれ#\a、#\A、#\x01となります。
Ctrl+Shift+aはCtrl+aと区別できません。
ALT+a、ALT+Shift+a、
ALT+Ctrl+aの戻り値はそれぞれ、
(ALT #\a)、(ALT #\A)、(ALT #\x01)となります。
{text.console} Returns true if there’s a key sequence to be read in the console’s input.
{text.console} Returns two values, the current cursor’s x and y position. The top-left corner is (0,0).
{text.console} Move cursor to the specified position. The top-left corner is (0,0).
{text.console} Reset terminal. Usually this sets the character attributes to the default, clears the screen, and moves the cursor to (0, 0).
{text.console} Clear entire screen.
{text.console} Clear characters from the current cursor position to the end of the line.
{text.console} Clear characters from the current cursor position to the end of the screen.
{text.console} Hide/show the cursor.
{text.console} If the cursor is at the bottom line of the screen, scroll up the contents and clear the bottom line; the cursor stays the same position. If the cursor is not at the bottom line of the screen, move the cursor down.
{text.console} If the cursor is at the top line of the screen, scroll down the contents and clear the top line; the cursor stays the same position. If the cursor is not at the top line of the screen, move the cursor up.
{text.console} Returns two values, the width and height of the screen.
Note: This may affect what’s shown in the console. It is recommended that you only call this before redrawing the entire screen and save the result.
{text.console} Set the console so that the subsequent characters will be written with attributes specified by spec.
The character attributes spec is a list in the following format:
(<fgcolor> [<bgcolor> . <option> ...])
where:
<fgcolor> : <color> | #f ; #f means default <bgcolor> : <color> | #f <color> : black | red | green | yellow | blue | magenta | cyan | white <option> : bright | reverse | underscore
For example, you can set characters to be written in red with black background and underscore, you can call:
(set-character-attribute con '(red black underscore))
That the options may seem rather limited in the age of full-color bitmap displays. That’s what it used to be, young lads.
{text.console} Reset character attributes to the default.
{text.console} Sets the console’s attributes to attrs and calls thunk, then restores the attributes. Even if thunk throws an error, attributes are restored.
Note: You should be able to nest this, but currently nesting isn’t working.
Next: text.csv - CSVテーブル, Previous: sxml.serializer - SXMLからXMLとXHTMLのシリアライゼーション, Up: ライブラリモジュール - ユーティリティ [Contents][Index]