コンテンツにスキップ

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()
end

生のターミナル入力モードを有効にします。ターミナルは生モードへ切り替わり、イベントの送出を開始します。

local ok, err = tty.start()

戻り値: boolean, error

生の入力を無効にし、ターミナルを通常モードへ戻します。

local ok, err = tty.stop()

戻り値: boolean, error

ターミナルイベントを購読し、チャネルを返します。イベントは type フィールドを持つテーブルとして配信されます。

local events = tty.events()

戻り値: EventChannel, error

現在のターミナルの大きさを問い合わせます。

local width, height, err = tty.screen_size()

戻り値: number, number, error

マウスイベントトラッキングを有効化または無効化します。

local ok, err = tty.mouse(true)
パラメータ説明
enableboolean有効化する場合は true、無効化する場合は false

戻り値: boolean, error

イベントは type フィールドを持つテーブルで、それによってどの他のフィールドが存在するかが決まります。

{
type = "key",
key = "a", -- 印刷可能文字またはキー名
key_type = "runes", -- 印刷可能の場合は "runes"、または特殊キー名
action = "press", -- "press" または "release"
alt = false,
ctrl = false,
shift = false
}

tty.mouse(true) が必要です。

{
type = "mouse",
action = "press", -- "press"、"release"、"motion"、"wheel"
button = "left", -- ボタン名
x = 10,
y = 5,
alt = false,
ctrl = false,
shift = false
}
{type = "resize", width = 120, height = 40}

tty.start() 後に初期サイズとともに 1 度だけ送出されます。

{type = "start", width = 120, height = 40}
{type = "focus", focused = true}
{type = "paste", text = "pasted content"}

キーイベントに照合する再利用可能なキーバインディングを作成します:

local quit = tty.bind({
keys = {"q", "ctrl+c"},
help = {key = "q/ctrl+c", desc = "quit"}
})
-- イベントループ内
if quit:matches(ev) then
break
end
フィールド説明
keysstring[]一致させるキーパターン(例:"a""ctrl+c""enter"
helptable任意。ヘルプテキスト用の {key = "...", desc = "..."}

戻り値: 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"))

新しい空のスタイルを作成します。

戻り値: Style

すべてのメソッドは新しい Style を返し、チェーン可能です。

メソッドパラメータ説明
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()このスタイルのコピーを作成
tty.borders.NORMAL
tty.borders.ROUNDED
tty.borders.THICK
tty.borders.DOUBLE
tty.borders.HIDDEN
tty.align.LEFT -- 0
tty.align.CENTER -- 0.5
tty.align.RIGHT -- 1

スタイル付きテキスト用のレイアウトと計測関数。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)
tty.text.position.TOP -- 0
tty.text.position.LEFT -- 0
tty.text.position.CENTER -- 0.5
tty.text.position.BOTTOM -- 1
tty.text.position.RIGHT -- 1