ANSI转义码
GitHub release Swift Package Manager license

控制终端光标字体擦除内容。 使用彩色动态的输出字符串构建现代、交互式的命令行工具。

https://en.wikipedia.org/wiki/ANSI_escape_code

目录

安装

将 ANSIEscapeCode 添加到 Package.swift

dependencies: [
    .package(url: "https://github.com/flintbox/ANSIEscapeCode", from: "0.1.1")
]

光标

向上

\u{001B}[(n)A:将光标向上移动 n 个单元格(默认为 1)。

ANSIEscapeCode.Cursor.up(3) // \u{001B}[3A
moveCursorUp() /// Move cursor up 1 cell.

向下

\u{001B}[(n)B:将光标向下移动 n 个单元格(默认为 1)。

ANSIEscapeCode.Cursor.down() // \u{001B}[1B
moveCursorDown(5) /// Move cursor down 5 cells.

向前

\u{001B}[(n)C:将光标向前移动 n 个单元格(默认为 1)。

ANSIEscapeCode.Cursor.forward(9) // \u{001B}[9C
moveCursorForward(4) // Move cursor forward 4 cells.

向后

\u{001B}[(n)D:将光标向后移动 n 个单元格(默认为 1)。

ANSIEscapeCode.Cursor.backward() // \u{001B}[1D
moveCursorBackward(2) // Move cursor backward 2 cells.

下一行

\u{001B}[(n)E:将光标移动到下方 n 行(默认为 1)的行首。

ANSIEscapeCode.Cursor.nextLine(2) // \u{001B}[2E
moveCursorNextLine() // Move cursor to next line.

上一行

\u{001B}[(n)F:将光标移动到上方 n 行(默认为 1)的行首。

ANSIEscapeCode.Cursor.previousLine(3) // \u{001B}[3F
moveCursorPreviousLine(5) // Move cursor to next line 5 times.

\u{001B}[(n)G:将光标移动到第 n 列。

ANSIEscapeCode.Cursor.column(2) // \u{001B}[2G
moveCursorToColumn(2) // Move cursor to second column.

位置

\u{001B}[(n);(m)1H:将光标移动到第 n 行,第 m 列。

ANSIEscapeCode.Cursor.position(row: 1, column: 1) // \u{001B}[1;1H
moveCursorToPosition(row: 5, column: 10) // Move cursor to position row 5 and column 10.

保存位置

\u{001B}[s:保存光标位置。

ANSIEscapeCode.Cursor.saveCursorPosition // \u{001B}[s
saveCursorPosition() // Save cursor position.

恢复位置

\u{001B}[u:恢复光标位置。

ANSIEscapeCode.Cursor.restoreCursorPosition // \u{001B}[u
restoreCursorPosition() // Restore cursor position.

隐藏

\u{001B}[?25l:隐藏光标。

ANSIEscapeCode.Cursor.hideCursor // \u{001B}[?25l
hideCursor() // Hide cursor.

显示

\u{001B}[?25h:显示光标。

ANSIEscapeCode.Cursor.showCursor // \u{001B}[?25h
showCursor() // Show cursor.

擦除

擦除显示

\u{001B}[(n)J:清除屏幕的一部分。如果 n 为 0(或缺失),则从光标处清除到屏幕末尾。 如果 n 为 1,则从光标处清除到屏幕开头。 如果 n 为 2,则清除整个屏幕。

ANSIEscapeCode.Erase.eraseInDisplay(.entireScreen) // \u{001B}[2J
eraseInDisplay(.fromCursorToBeginningOfScreen) // Erase content from cursor to beginning of screen.

擦除行

\u{001B}[(n)K:擦除该行的一部分。 如果 n 为零(或缺失),则从光标处清除到行尾。 如果 n 为 1,则从光标处清除到行首。 如果 n 为 2,则清除整行。 光标位置不变。

ANSIEscapeCode.Erase.eraseInLine(.fromCursorToEndOfLine) // \u{001B}[0K
eraseInLine(.entireLine) // Erase entire line.

滚动

向上滚动

\u{001B}[(n)S:将整个页面向上滚动 n 行(默认为 1)。 新行添加到底部。

ANSIEscapeCode.Scroll.up() // \u{001B}[1S
scrollUp(3) // Scroll up screen 3 lines.

向下滚动

\u{001B}[(n)T:将整个页面向下滚动 n 行(默认为 1)。 新行添加到顶部。

ANSIEscapeCode.Scroll.down(5) // \u{001B}[5T
scrollDown() // Scroll down screen 1 line.

装饰

重置

\u{001B}[0m:重置所有装饰属性。

ANSIEscapeCode.Decoration.reset // \u{001B}[0m

重置背景颜色

\u{001B}[39m:重置背景装饰属性。

ANSIEscapeCode.Decoration.resetBackgroundColor // \u{001B}[39m

粗体

\u{001B}[1m:使输出变为粗体。

ANSIEscapeCode.Decoration.bold // \u{001B}[1m
print("bold text".boldOutput) // Print bold text.

斜体

\u{001B}[3m:使输出变为斜体。

ANSIEscapeCode.Decoration.italic // \u{001B}[3m
print("italic text".italicOutput) // Print italic text.

下划线

\u{001B}[4m:使输出带有下划线。

ANSIEscapeCode.Decoration.underline // \u{001B}[4m
print("underline text".underlineOutput) // Print underline text.

闪烁

\u{001B}[5m:使输出闪烁。

ANSIEscapeCode.Decoration.blink // \u{001B}[5m
print("blinking text".blinkOutput) // Print blinking text.

文本颜色

\u{001B}[(COLOR)m 设置终端上的文本颜色。 查看TextColor.swift 以获取可用颜色。

ANSIEscapeCode.Decoration.textColor(.red) // \u{001B}[31m
print("red text".color(.red)) // Print red text.

文本 8 位颜色

\u{001B}[38;5;(COLOR)m 设置终端上的文本 8 位颜色。查看这里 获取可用颜色。

ANSIEscapeCode.Decoration.text8BitsColor(200) // \u{001B}[38;5;200m
print("orange text".colorFrom8BitsColorSet(208)) // Print orange text.

文本 RGB 颜色

\u{001B}[38;2;(RED);(GREEN);(BLUE)m 设置终端上的文本 RGB 颜色。 查看这里 获取更多信息。

ANSIEscapeCode.Decoration.textRGBColor(red: 30, green: 20, blue: 10) // \u{001B}[38;2;30;20;10m
print("mint text".colorFromRGBColorSet(red: 170, green: 240, blue: 209)) // Print mint text.

背景颜色

\u{001B}[(COLOR)m 设置终端上的文本背景颜色。 查看BackgroundColor.swift 以获取可用颜色。

ANSIEscapeCode.Decoration.backgroundColor(.red) // \u{001B}[41m
print("red background".backgroundColor(.red)) // Print text with red background.

背景 8 位颜色

\u{001B}[38;5;(COLOR)m 设置终端上的文本背景 8 位颜色。 查看这里 获取可用颜色。

ANSIEscapeCode.Decoration.background8BitsColor(200) // \u{001B}[48;5;200m
print("orange background".backgroundColorFrom8BitsColorSet(208)) // Print text with orange background.

背景 RGB 颜色

\u{001B}[48;2;(RED);(GREEN);(BLUE)m 设置终端上的文本背景 RGB 颜色。 查看这里 获取更多信息。

ANSIEscapeCode.Decoration.backgroundRGBColor(red: 30, green: 20, blue: 10) // \u{001B}[48;2;30;20;10m
print("mint background".backgroundColorFromRGBColorSet(red: 170, green: 240, blue: 209)) // Print text with mint background.

贡献

如果您有好的想法或建议? 请随时提出 pull request 或发送 电子邮件 给我们。

希望您喜欢使用 ANSIEscapeCode 构建命令行工具!