Rubyでcursesの実験 【APIまとめ編】
Rubyでcursesを使う実験をした時のメモ。APIまとめ編。
属性編はこちら。
Rubyでcursesの実験 【キー入力編】 - 開発メモ
http://seeku.hateblo.jp/entry/2013/07/01/235842
RubyのサイトのAPIのまとめがはてしなく使いにくいので、勝手にまとめなおしてみる。
Curses
※1文字=1バイトであることに注意
初期化・終了
API | 概要 |
---|---|
init_screen -> Curses::Window | cursesの初期化 |
close_screen | cursesの終了 |
closed? -> bool | cursesが終了してるか? |
stdscr -> Curses::Window | 標準スクリーンを取得 |
カーソル操作
API | 概要 |
---|---|
inch -> Integer | カーソル位置から1文字読み込み |
setpos(y, x) | カーソル位置を移動 |
curs_set(visibility) -> Integer or nil | カーソルの表示/非表示を設定 |
文字出力
API | 概要 |
---|---|
addch(ch) | カーソルの位置に1文字出力 |
addstr(str) | カーソルの位置に文字列を出力 |
insch(ch) | カーソルの位置に1文字挿入 |
insertln | カーソルの位置に1行挿入 |
refresh | 標準スクリーンの表示を更新 |
doupdate | refreshと同じ(効率が良い?) |
文字の属性
API | 概要 |
---|---|
attrset(attrs) -> Integer | 属性を設定 |
attron(attron) -> Integer | 指定した属性をオンに |
attroff(attrs) -> Integer | 指定した属性をオフに |
standout | 属性を最強調に |
standend | 属性を通常に |
文字/背景の色
API | 概要 |
---|---|
has_colors? -> bool | カラー表示に対応しているか? |
can_change_color? -> bool | 色を変更できるか? |
start_color -> bool | カラーの使用を開始 |
colors -> Integer | 色として使える最大値を取得 |
init_color(color, r, g, b) -> bool | 色のRGB値を変更 |
color_content(color) -> Array | 色のRGB値を取得 |
color_pairs -> Integer | ペアとして使える最大値を取得 |
init_pair(pair, f, b) -> bool | 文字色・背景色のペアを設定 |
pair_content(pair) -> Array | ペアから文字色・背景色を取得 |
color_pair(attrs) -> Integer | ペアを属性に設定 |
pair_number(attrs) -> Integer | 属性に設定する時のペアの番号を取得(?) |
use_default_colors | 文字色・背景色をデフォルトに戻す |
文字入力
API | 概要 |
---|---|
getch -> Integer | 標準スクリーンから1文字読み込み |
getstrgetstr -> String | 標準スクリーンから文字列を読み込み |
ungetch(ch) | 標準スクリーンに1文字読み戻す |
keyname(c) -> String or nil | キーに対する文字列を取得 |
timeout=(delay) | ウィンドウから読み込むときのタイムアウトを設定 |
入力モード設定
API | 概要 |
---|---|
cbreak | control入力モードに設定 |
crmode | control入力モードに設定 |
nocbreak | control入力モードを終了 |
nocrmode | control入力モードを終了 |
raw | raw入力モードに設定 |
noraw | raw入力モードを終了 |
echo | ユーザーの入力を画面に表示する |
noecho | ユーザーの入力を画面に表示しない |
nl | returnでCtrl+Jを返すようにする |
nonl | returnでCtrl+Mを返すようにする |
それぞれ、モードの内容はこんな感じ。
- control入力モード
一部のキー入力(Ctrl+C、Ctrl+Zなど)をOSに処理させるモード。入力のバッファリング無し。 - raw入力モード
すべてのキー入力をプログラムで処理する。入力のバッファリング無し。 - 通常入力モード
OS側で出来るだけキー入力を解釈させるモード。入力はバッファリングされる。
文字消去/画面クリア
API | 概要 |
---|---|
delch | カーソル位置で1文字削除 |
deleteln | カーソルのある行を削除 |
clrtoeol | カーソル位置からウィンドウの最後までを削除 |
clear | 標準スクリーン全体を削除(refresh不要) |
bkgdset(ch) | 削除に使われる背景文字を設定(?) |
bkgd(ch) -> bool | ウィンドウの背景を変更(?) |
マウス操作
API | 概要 |
---|---|
mouseinterval(interval) -> bool | マウスの情報を取得する間隔を設定 |
getmouse -> Integer or nil | マウスの情報を取得 |
mousemask(mask) -> Integer | マウスの情報だけを取り出す |
ungetmouse(mevent) -> bool | マウスの情報を読み戻す |
画面サイズ
API | 概要 |
---|---|
cols -> Integer | 標準スクリーンで表示可能な桁数を取得 |
lines -> Integer | 標準スクリーンで表示可能な行数を取得 |
resizeterm(lines, cols) -> bool or nil | 標準スクリーンのサイズを変更 |
resize(lines, cols) -> bool or nil | 標準スクリーンのサイズを変更 |
各種設定
API | 概要 |
---|---|
TABSIZE -> Integer | タブ幅を取得 |
TABSIZE=(val) | タブ幅を設定 |
ESCDELAY -> Integer | ESCシーケンスを合成するための待機時間を取得 |
ESCDELAY=(val) | ESCシーケンスを合成するための待機時間を設定 |
その他
API | 概要 |
---|---|
beep | 音を出す |
flash | 画面を点滅させる |
setscrreg(top, bottom) -> bool | スクロール可能な範囲を設定 |
scrl(num) -> bool | ウィンドウをスクロールさせる |
def_prog_mode -> bool | 現在の端末の属性を保存(?) |
reset_prog_mode -> bool | 端末の属性をリセット(?) |
スクロール関係のAPIはCurses.scrollokが使えないため、実際には意味がない。標準スクリーンを明示的に使えば使用可能。
端末の属性関係のAPIも、属性を変更するAPIが存在しないため意味がない?
Curses::Window
ほぼ、Cursesと同じなので省略。keypadはtrueにしておくのがおすすめかな。
情報元
module Curses
http://doc.ruby-lang.org/ja/2.0.0/class/Curses.html
class Curses::Window
http://doc.ruby-lang.org/ja/2.0.0/class/Curses=3a=3aWindow.html