TTY
生の入力イベント、スタイル付き出力、レイアウトユーティリティ用のターミナル UI モジュール。
local tty = require("tty")生の入力リーダーを起動し、イベントを購読し、ループで処理します:
local tty = require("tty")local io = require("io")
local function handler() tty.start() local events = tty.events()
while true do local ev = events:receive() if not ev then break end
if ev.type == "key" then if ev.key == "q" or (ev.ctrl and ev.key == "c") then break end io.print("Key: " .. ev.key)
elseif ev.type == "resize" then io.print("Size: " .. ev.width .. "x" .. ev.height) end end
tty.stop()endtty.start()
Section titled “tty.start()”生のターミナル入力モードを有効にします。ターミナルは生モードへ切り替わり、イベントの送出を開始します。
local ok, err = tty.start()戻り値: boolean, error
tty.stop()
Section titled “tty.stop()”生の入力を無効にし、ターミナルを通常モードへ戻します。
local ok, err = tty.stop()戻り値: boolean, error
tty.events()
Section titled “tty.events()”ターミナルイベントを購読し、チャネルを返します。イベントは type フィールドを持つテーブルとして配信されます。
local events = tty.events()戻り値: EventChannel, error
tty.screen_size()
Section titled “tty.screen_size()”現在のターミナルの大きさを問い合わせます。
local width, height, err = tty.screen_size()戻り値: number, number, error
tty.mouse(enable)
Section titled “tty.mouse(enable)”マウスイベントトラッキングを有効化または無効化します。
local ok, err = tty.mouse(true)| パラメータ | 型 | 説明 |
|---|---|---|
enable | boolean | 有効化する場合は true、無効化する場合は false |
戻り値: boolean, error
イベント種別
Section titled “イベント種別”イベントは type フィールドを持つテーブルで、それによってどの他のフィールドが存在するかが決まります。
キーイベント
Section titled “キーイベント”{ type = "key", key = "a", -- 印刷可能文字またはキー名 key_type = "runes", -- 印刷可能の場合は "runes"、または特殊キー名 action = "press", -- "press" または "release" alt = false, ctrl = false, shift = false}マウスイベント
Section titled “マウスイベント”tty.mouse(true) が必要です。
{ type = "mouse", action = "press", -- "press"、"release"、"motion"、"wheel" button = "left", -- ボタン名 x = 10, y = 5, alt = false, ctrl = false, shift = false}リサイズイベント
Section titled “リサイズイベント”{type = "resize", width = 120, height = 40}スタートイベント
Section titled “スタートイベント”tty.start() 後に初期サイズとともに 1 度だけ送出されます。
{type = "start", width = 120, height = 40}フォーカスイベント
Section titled “フォーカスイベント”{type = "focus", focused = true}ペーストイベント
Section titled “ペーストイベント”{type = "paste", text = "pasted content"}キーバインディング
Section titled “キーバインディング”キーイベントに照合する再利用可能なキーバインディングを作成します:
local quit = tty.bind({ keys = {"q", "ctrl+c"}, help = {key = "q/ctrl+c", desc = "quit"}})
-- イベントループ内if quit:matches(ev) then breakendtty.bind(config)
Section titled “tty.bind(config)”| フィールド | 型 | 説明 |
|---|---|---|
keys | string[] | 一致させるキーパターン(例:"a"、"ctrl+c"、"enter") |
help | table | 任意。ヘルプテキスト用の {key = "...", desc = "..."} |
戻り値: KeyBinding
KeyBinding メソッド
Section titled “KeyBinding メソッド”| メソッド | 戻り値 | 説明 |
|---|---|---|
matches(event) | boolean | キーイベントがこのバインディングに一致するかをテスト |
set_enabled(bool) | self | バインディングを有効化または無効化 |
is_enabled() | boolean | バインディングが有効かをチェック |
help() | table | {key, desc} のヘルプ情報を返す |
lipgloss ベースのスタイリングを使用してスタイル付きテキスト出力を作成します。すべてのスタイルメソッドは新しいスタイルを返します(不変)。
local tty = require("tty")local io = require("io")
local title = tty.style() :bold() :foreground("#FF0000") :padding(0, 1)
local box = tty.style() :border(tty.borders.ROUNDED) :border_foreground("#00FF00") :width(40) :padding(1, 2)
io.print(box:render(title:render("Hello"), "World"))tty.style()
Section titled “tty.style()”新しい空のスタイルを作成します。
戻り値: Style
Style メソッド
Section titled “Style メソッド”すべてのメソッドは新しい Style を返し、チェーン可能です。
テキスト装飾
Section titled “テキスト装飾”| メソッド | パラメータ | 説明 |
|---|---|---|
foreground(color) | string | テキストカラー(hex "#FF0000"、ANSI "9"、または名前) |
background(color) | string | 背景色 |
bold(enable?) | boolean | 太字テキスト(デフォルト: true) |
italic(enable?) | boolean | イタリック体テキスト |
underline(enable?) | boolean | 下線付きテキスト |
strikethrough(enable?) | boolean | 取り消し線付きテキスト |
faint(enable?) | boolean | 薄いテキスト |
blink(enable?) | boolean | 点滅テキスト |
reverse(enable?) | boolean | 前景/背景の入れ替え |
| メソッド | パラメータ | 説明 |
|---|---|---|
width(n) | number | 固定幅 |
height(n) | number | 固定高さ |
max_width(n) | number | 最大幅 |
max_height(n) | number | 最大高さ |
padding(...) | numbers | パディング(CSS スタイル:top、right、bottom、left) |
margin(...) | numbers | マージン(CSS スタイル) |
align(pos) | number | 水平方向の配置 |
align_vertical(pos) | number | 垂直方向の配置 |
inline(enable?) | boolean | インラインレンダリングモード |
| メソッド | パラメータ | 説明 |
|---|---|---|
border(name, ...) | string, booleans | ボーダースタイル、辺ごとの任意トグル |
border_foreground(...) | strings | ボーダーカラー |
border_background(...) | strings | ボーダー背景色 |
| メソッド | 説明 |
|---|---|
render(...) | このスタイルを適用して文字列をレンダリング |
copy() | このスタイルのコピーを作成 |
ボーダー定数
Section titled “ボーダー定数”tty.borders.NORMALtty.borders.ROUNDEDtty.borders.THICKtty.borders.DOUBLEtty.borders.HIDDENアライメント定数
Section titled “アライメント定数”tty.align.LEFT -- 0tty.align.CENTER -- 0.5tty.align.RIGHT -- 1テキストユーティリティ
Section titled “テキストユーティリティ”スタイル付きテキスト用のレイアウトと計測関数。tty.text の下で利用可能です。
local w = tty.text.width("hello") -- 印刷可能幅(ANSI 対応)local h = tty.text.height("a\nb\nc") -- 行数local w, h = tty.text.size("hello\nworld") -- 両方-- 横並びに結合、上揃えlocal row = tty.text.join_horizontal(tty.text.position.TOP, left, right)
-- 縦に積む、中央揃えlocal col = tty.text.join_vertical(tty.text.position.CENTER, top, bottom)local w = tty.text.max_width({"short", "a longer string"}) -- 最も広いものlocal h = tty.text.max_height({"one\ntwo", "single"}) -- 最も高いもの指定された寸法のボックス内に文字列を配置します:
-- 80x24 のボックスの中央に配置local out = tty.text.place(80, 24, tty.text.position.CENTER, tty.text.position.CENTER, content)
-- 水平方向のみlocal out = tty.text.place_horizontal(80, tty.text.position.RIGHT, content)
-- 垂直方向のみlocal out = tty.text.place_vertical(24, tty.text.position.BOTTOM, content)ポジション定数
Section titled “ポジション定数”tty.text.position.TOP -- 0tty.text.position.LEFT -- 0tty.text.position.CENTER -- 0.5tty.text.position.BOTTOM -- 1tty.text.position.RIGHT -- 1