For Development HEAD DRAFTSearch (procedure/syntax/module):

Next: , Previous: , Up: ライブラリモジュール - ユーティリティ   [Contents][Index]

12.63 text.console - テキスト端末制御

Module: text.console

このモジュールは文字端末を制御する簡単なインタフェースを提供します。 今のところ、vt100互換端末とWindowsコンソールがサポートされています。

curses等の外部ライブラリには依存しておらず、Gaucheだけで使うことができますが、 出来ることは限られています。 例えばシフトキーだけが押されたタイミングでイベントを受け取ることはできません。 より細かい制御には、何らかの外部拡張ライブラリが必要になるでしょう。

このモジュールの機能の例としては、 Gaucheソースのexamplesディレクトリにあるsnake.scmを見てください。

コンソールオブジェクト

Class: <vt100>

{text.console} vt100互換端末を表します。このクラスのインスタンスは 以降のジェネリックファンクションの “console” 引数に渡せます。

Instance Variable of <vt100>: iport

端末に接続されている入力ポートです。デフォルトは標準入力です。

Instance Variable of <vt100>: oport

端末に接続されている出力ポートです。デフォルトは標準出力です。

Instance Variable of <vt100>: input-delay

端末は、特殊キーが押された場合、ESCから始まるエスケープシーケンスを送って来ます。 実際にESCキーが押された場合と区別するために、入力の間隔を測っています。 後続の入力がinput-delayμs以内に来なかった場合はそこでシーケンスが終了したと みなし、受け取っているシーケンスが有効なエスケープシーケンスを構成しなければ 個別にキー入力されたとみなします。デフォルトは1000、すなわち1msです。

Class: <windows-console>

Windowsコンソールを表します。このクラス自体は全てのプラットフォームで 定義されていますが、有用なメソッドはWindowsネイティブのランタイムでしか提供されません。

パブリックなスロットはありません。

アプリケーションは実行時にどの種類のコンソールが利用可能かを見極めなければなりません。 推奨される手順は次のとおりです。

次の手続きは上の手順を実装しています。

Function: make-default-console :key if-not-available

{text.console} 実行中のプロセスで使える端末のインスタンスを作成して返します。

適切な端末クラスが無い場合、振る舞いはif-not-availableキーワード引数に 依存します。デフォルトである:errorの場合はエラーが報告されます。 #fの場合はこの手続きが#fを返します。

Function: vt100-compatible? string

{text.console} 環境変数TERMの値を文字列で受け取り、それが<vt100>端末として 扱えるなら#tを、そうでなければ#fを返します。

コンソール制御

Generic function: call-with-console console proc :key mode

{text.console} コンソールの制御を取得した状態で、procconsoleを唯一の引数として 呼び出します。コンソールはmodeで指定されるモードにセットされます。 modewith-terminal-modeが受け付けるシンボル、 すなわちrawrarecookedのいずれかでなければなりません。 省略された場合はrareになります。 このモードは、端末でのエコーを抑止し、ほぼ全てのキーストロークを プログラムにそのまま渡しますが、端末制御機能はシステム側で処理します (割り込みのCtrl-C、プロセス一時停止のCtrl-Zなど。実際のキー割り当ては 端末制御で変更可能です。詳しくはgauche.termios - 端末の制御参照。)

procが捕捉されない例外を投げた場合、このジェネリック関数は 端末モードを元に戻します。ただ、スクリーンはクリアされません。

Generic function: putch console char

{text.console} 文字charをコンソールの現在のカーソル位置に出力し、カーソルをその分進めます。

Generic function: putstr console string

{text.console} 文字列stringコンソールの現在のカーソル位置に出力し、カーソルをその分進めます。

Generic function: beep console

{text.console} 可能なら、端末のビープ音を鳴らすか、画面をフラッシュさせます(visual bell)。

Generic function: getch console

{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)が返されます。

EOF

入力がクローズされたことを示します。

ALT以外のモディファイアキーは別扱いではなく返される文字コードにエンコードされます。 CAPSLOCKがoffであるとき、ユーザが aShift+aCtrl+aをタイプしたとすると、 戻り値はそれぞれ#\a#\A#\x01となります。 Ctrl+Shift+aCtrl+aと区別できません。 ALT+aALT+Shift+aALT+Ctrl+aの戻り値はそれぞれ、 (ALT #\a)(ALT #\A)(ALT #\x01)となります。

Generic function: chready? console

{text.console} Returns true if there’s a key sequence to be read in the console’s input.

Generic function: query-cursor-position console

{text.console} Returns two values, the current cursor’s x and y position. The top-left corner is (0,0).

Generic function: move-cursor-to console row column

{text.console} Move cursor to the specified position. The top-left corner is (0,0).

Generic function: reset-terminal console

{text.console} Reset terminal. Usually this sets the character attributes to the default, clears the screen, and moves the cursor to (0, 0).

Generic function: clear-screen console

{text.console} Clear entire screen.

Generic function: clear-to-eol console

{text.console} Clear characters from the current cursor position to the end of the line.

Generic function: clear-to-eos console

{text.console} Clear characters from the current cursor position to the end of the screen.

Generic function: hide-cursor console
Generic function: show-cursor console

{text.console} Hide/show the cursor.

Generic function: cursor-down/scroll-up console

{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.

Generic function: cursor-up/scroll-down console

{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.

Generic function: query-screen-size console

{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.

Generic function: set-character-attribute console spec

{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.

Generic function: reset-character-attribute console

{text.console} Reset character attributes to the default.

Generic function: with-character-attribute console attrs thunk

{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: , Previous: , Up: ライブラリモジュール - ユーティリティ   [Contents][Index]


For Development HEAD DRAFTSearch (procedure/syntax/module):
DRAFT