跳到主要内容

io 输入与输出

io 是 Alia 的标准库输入与输出模块,提供标准输入输出操作、终端控制和 TUI 核心能力。

引入模块​

import 'io' as io ~ 导入为变量
import 'io' ~ 导入作用域

基础 I/O​

输出函数​

print​

io.print 输出到标准输出,不换行。

io.print 'Hello, '
io.println 'World!'

println​

io.println 输出到标准输出并换行。

io.println 'This is a line'

eprint​

io.eprint 输出到标准错误,不换行。

io.eprint 'Error: '
io.eprintln 'Something went wrong'

eprintln​

io.eprintln 输出到标准错误并换行。

io.eprintln 'Error occurred'

format​

io.format 使用 {} 作为占位符格式化输出。

const result = io.format 'Name: {}, Age: {}' with ['Alice', 30]
io.println result ~ 输出: Name: Alice, Age: 30

终端信息​

terminalWidth​

terminalWidth 为终端宽度(列数),类型为 Number。

const width = io.terminalWidth
print 'Terminal width: ' .. width

terminalHeight​

terminalHeight 为终端高度(行数),类型为 Number。

const height = io.terminalHeight
print 'Terminal height: ' .. height

terminalSize​

terminalSize 返回终端尺寸 Object,包含 width 与 height 属性。

const size = io.terminalSize
print 'Size: ' .. size.width .. 'x' .. size.height

终端控制​

光标控制​

moveCursor​

moveCursor 将光标移动到指定位置(0-based)。

io.moveCursor at row 0 column 0 ~ 移动到左上角
io.moveCursor at row 5 column 10 ~ 移动到第5行第10列

moveCursorUp / moveCursorDown / moveCursorForward / moveCursorBack​

四个方法执行光标相对移动。

io.moveCursorUp 1
io.moveCursorDown 1
io.moveCursorForward 1
io.moveCursorBack 1

saveCursor / restoreCursor​

saveCursor 与 restoreCursor 保存并恢复光标位置。

io.saveCursor
io.moveCursor at row 0 column 0
io.restoreCursor

hideCursor / showCursor​

hideCursor 与 showCursor 隐藏并显示光标。

io.hideCursor
io.showCursor

屏幕控制​

clearScreen​

clearScreen 清除整个屏幕。

io.clearScreen

clearLine​

clearLine 清除当前行。

io.clearLine

clearToEndOfLine​

clearToEndOfLine 清除到行尾。

io.clearToEndOfLine

clearToEndOfScreen​

clearToEndOfScreen 清除到屏幕末尾。

io.clearToEndOfScreen

全屏模式​

enterAlternateScreen / exitAlternateScreen​

enterAlternateScreen 与 exitAlternateScreen 进入并退出备用屏幕缓冲区。

io.enterAlternateScreen
io.clearScreen
io.println 'This is in alternate screen'
io.exitAlternateScreen

输入增强​

输入函数​

io.read、io.readChar、io.readKey 为属性式读取,io.readLine、io.readPassword 接受单个提示字符串。上述读取均返回符合 Alia 异步模型的 Stack,须经 await 消费;EOF 时结果为 Null。

read​

read 读取一行(遇换行或 EOF),结果不含行尾换行符。

const task = io.read
const line = await task

readLine​

readLine 读取一行,并在读取前输出提示文本。

const task = io.readLine '姓名> '
const name = await task

readChar​

readChar 读取单个字符(按 UTF-8 前导字节读取 1-4 字节解码)。

const task = io.readChar
const ch = await task

readPassword​

readPassword 读取密码:TTY 下采用原始模式抑制回显,支持退格与 Ctrl+C;非 TTY 时回退为整行读取。

const task = io.readPassword '密码> '
const pw = await task

原始模式​

setRawMode​

setRawMode 设置原始模式,启用逐字符读取。

io.setRawMode true
~ 此时 readKey 可以读取特殊键
io.setRawMode false

按键读取​

readKey​

readKey 读取按键并返回 Stack,await 后得到按键信息对象。

const task = io.readKey
const key = await task
if (typeOf key) == 'Object' {
print 'Key: ' .. key['key']
print 'Ctrl: ' .. key['ctrl']
print 'Alt: ' .. key['alt']
}

返回对象结构:

  • key: 键名(如 'a', 'enter', 'up', 'f1' 等)
  • ctrl: Ctrl 键是否按下
  • alt: Alt 键是否按下
  • shift: Shift 键是否按下
  • raw: 原始字节码

hasKey​

hasKey 非阻塞检查是否有按键。

const has = io.hasKey
~ has 为 true 时可调用 readKey

鼠标支持​

enableMouse / disableMouse​

enableMouse 与 disableMouse 启用并禁用鼠标事件。

io.enableMouse
~ 此时可以接收鼠标事件
io.disableMouse

文本样式​

颜色设置​

颜色常量通过位置参数传入 io.setColor 与 io.setBackgroundColor。

const red = io.Colors.Red
io.setColor red ~ 设置前景色

const blue = io.BgColors.Blue
io.setBackgroundColor blue ~ 设置背景色

io.resetColor ~ 重置颜色

文本样式​

io.bold、io.italic、io.underline、io.strikethrough、io.inverse 设置对应文本样式,io.resetStyle 重置所有样式。

io.bold
io.italic
io.underline
io.strikethrough
io.inverse
io.resetStyle ~ 重置所有样式

颜色常量​

前景色​

io.Colors.Black
io.Colors.Red
io.Colors.Green
io.Colors.Yellow
io.Colors.Blue
io.Colors.Magenta
io.Colors.Cyan
io.Colors.White
io.Colors.Gray
io.Colors.BrightRed
io.Colors.BrightGreen
io.Colors.BrightYellow
io.Colors.BrightBlue
io.Colors.BrightMagenta
io.Colors.BrightCyan
io.Colors.BrightWhite

背景色​

io.BgColors.Black
io.BgColors.Red
io.BgColors.Green
io.BgColors.Yellow
io.BgColors.Blue
io.BgColors.Magenta
io.BgColors.Cyan
io.BgColors.White

绘图原语​

drawBox​

drawBox 绘制边框。

io.drawBox at 0 and 0 sized 20 and 10 ~ 绘制单线边框

fillRect​

fillRect 填充矩形。

io.fillRect at 0 and 0 sized 10 and 3 with '#'

drawText​

drawText 在指定位置绘制文本。

io.drawText at 0 and 0 with 'Hello at origin'
io.drawText at 5 and 10 with 'Text at (5, 10)'

事件循环​

创建事件循环​

const loop = io.eventLoop

EventLoop 方法​

on​

on 注册事件回调。

const onKey = alias <Object keyInfo> -> {
print 'Key pressed: ' .. keyInfo.key
}
loop.on 'key' with onKey

off​

off 移除事件回调。

loop.off 'key' onKey

start / stop​

start 与 stop 启动并停止事件循环。

loop.start
~ ...
loop.stop

setTimeout​

setTimeout 注册一次性定时器。

const fire = alias -> {
print 'Timer fired!'
}
const timerId = loop.setTimeout 1000 with fire
loop.clearTimer timerId ~ 取消定时器

setInterval​

setInterval 注册间隔定时器。

const tick = alias -> {
print 'Tick!'
}
const timerId = loop.setInterval 500 with tick
loop.clearTimer timerId ~ 取消定时器

事件类型​

事件参数说明
'key'<Object keyInfo>按键事件
'mouse'<Object mouseInfo>鼠标事件
'resize'-终端尺寸改变
'tick'-定时器触发

示例代码​

简单菜单​

import 'io' as io

io.enterAlternateScreen
io.clearScreen
io.hideCursor

const items = ['Start', 'Settings', 'Exit']
let selected = 0

const drawMenu = alias -> {
io.moveCursor at row 0 column 0
io.println 'Main Menu'
io.println '========='
let i = 0
while i < items.length {
if i == selected {
const cyan = io.Colors.Cyan
io.setColor cyan
const line = '> ' .. items[i]
io.println line
io.resetColor
} else {
const line = ' ' .. items[i]
io.println line
}
i++
}
}

call drawMenu

io.setRawMode true
let running = true
while running {
const keyInfo = await io.readKey
if (typeOf keyInfo) == 'Object' {
if keyInfo['key'] == 'up' {
if selected > 0 { selected = selected - 1 }
} else {
if keyInfo['key'] == 'down' {
if selected < items.length - 1 { selected = selected + 1 }
} else {
if keyInfo['key'] == 'enter' { running = false }
}
}
}
if running {
io.clearScreen
call drawMenu
}
}

io.setRawMode false
io.showCursor
io.exitAlternateScreen
const selectedText = 'Selected: ' .. items[selected]
io.println selectedText

彩色输出​

import 'io' as io

io.println 'Colorful output:'

const red = io.Colors.Red
io.setColor red
io.print 'Red '
const green = io.Colors.Green
io.setColor green
io.print 'Green '
const blue = io.Colors.Blue
io.setColor blue
io.print 'Blue '
io.resetColor
io.println ''

io.bold
const yellow = io.Colors.Yellow
io.setColor yellow
io.println 'Bold Yellow Text'
io.resetStyle

进度条​

import 'io' as io

const total = 50
let current = 0

while current <= total {
const progress = current * 100 / total
const filledCount = current * 20 / total

let bar = ''
let i = 0
while i < 20 {
if i < filledCount {
bar = bar .. '#'
} else {
bar = bar .. '-'
}
i++
}

io.moveCursor at row 0 column 0
const green = io.Colors.Green
io.setColor green
print 'Progress: [' .. bar .. '] ' .. progress .. '%'
io.resetColor

current++
}

io.println ''
io.println 'Complete!'

下一步​