控制台操作
模块路径:console
功能说明:提供终端控制、光标操作、键盘输入等控制台交互功能。支持两种调用方式:
- 函数式调用:
Console.func(arg0, arg1...) - 命令式调用:
Console.func arg0 arg1...
函数分类及详细说明#
一、终端基本信息#
Console.width功能:获取终端宽度(列数) 参数:无 返回值:integer(失败返回none) 示例:1 Console.width() # => 120Console.height功能:获取终端高度(行数) 参数:无 返回值:integer(失败返回none) 示例:1 Console.height() # => 40
二、终端控制#
Console.write功能:在指定位置输出文本 参数:x: integer(列位置)y: integer(行位置)text: string(要输出的内容) 返回值:none示例:
1 Console.write 10 5 "Hello" # 在第5行第10列输出"Hello"Console.title功能:设置终端窗口标题 参数:title: string返回值:none示例:1 Console.title "My App"Console.clear功能:清空终端屏幕 参数:无 返回值:none示例:1 Console.clear()Console.flush功能:强制刷新输出缓冲区 参数:无 返回值:none示例:1 Console.flush()
三、终端模式控制#
Console.mode_raw功能:启用原始输入模式(禁用行缓冲) 参数:无 返回值:none示例:1 Console.mode_raw()Console.mode_normal功能:禁用原始输入模式(启用行缓冲) 参数:无 返回值:none示例:1 Console.mode_normal()Console.screen_alternate功能:启用备用屏幕(保留主屏幕内容) 参数:无 返回值:none示例:1 Console.screen_alternate()Console.screen_normal功能:禁用备用屏幕(返回主屏幕) 参数:无 返回值:none示例:1 Console.screen_normal()
四、光标控制#
Console.cursor_to功能:移动光标到指定位置 参数:x: integer(列位置)y: integer(行位置) 返回值:none示例:
1 Console.cursor_to 10 5方向移动:
函数 参数 功能 Console.cursor_up()lines: integer上移N行 Console.cursor_down()lines: integer下移N行 Console.cursor_left()cols: integer左移N列 Console.cursor_right()cols: integer右移N列 示例:
1 Console.cursor_down 3 # 下移3行Console.cursor_save功能:保存当前光标位置 参数:无 返回值:none示例:1 Console.cursor_save()Console.cursor_restore功能:恢复保存的光标位置 参数:无 返回值:none示例:1 Console.cursor_restore()Console.cursor_hide功能:隐藏光标 参数:无 返回值:none示例:1 Console.cursor_hide()Console.cursor_show功能:显示光标 参数:无 返回值:none示例:1 Console.cursor_show()
五、键盘输入#
Console.read_line功能:读取一行输入(显示输入内容) 参数:无 返回值:string示例:1 let input = Console.read_line()Console.read_password功能:读取密码(隐藏输入内容) 参数:无 返回值:string示例:1 let pwd = Console.read_password()Console.read_key功能:读取单个按键 参数:无 返回值:string(特殊按键的转义序列) 示例:1 let key = Console.read_key()特殊按键常量:
1 Console.keys.enter # 回车键 2 Console.keys.backspace # 退格键 3 Console.keys.tab # Tab键 4 Console.keys.esc # ESC键 5 Console.keys.up # 上箭头 6 Console.keys.down # 下箭头 7 Console.keys.left # 左箭头 8 Console.keys.right # 右箭头 9 Console.keys.f1 # F1功能键 10 ... # 其他功能键类似
通用规则#
坐标系统:
- 左上角为原点 (0, 0)
- X轴向右递增(列)
- Y轴向下递增(行)
错误处理:
- 参数类型错误时抛出异常
- 终端操作失败返回
none
ANSI转义序列: 所有控制功能均使用标准ANSI转义序列实现