Control terminal cursor and font or erase content. Build modern, interactive command line tool with colorful and dynamic output strings.
https://en.wikipedia.org/wiki/ANSI_escape_code
Table of Contents
Add ANSIEscapeCode to Package.swift
.
dependencies: [
.package(url: "https://github.com/flintbox/ANSIEscapeCode", from: "0.1.1")
]
\u{001B}[(n)A
: Move cursor up n
(default 1) cells.
ANSIEscapeCode.Cursor.up(3) // \u{001B}[3A
moveCursorUp() /// Move cursor up 1 cell.
\u{001B}[(n)B
: Move cursor down n
(default 1) cells.
ANSIEscapeCode.Cursor.down() // \u{001B}[1B
moveCursorDown(5) /// Move cursor down 5 cells.
\u{001B}[(n)C
: Move cursor forward n
(default 1) cells.
ANSIEscapeCode.Cursor.forward(9) // \u{001B}[9C
moveCursorForward(4) // Move cursor forward 4 cells.
\u{001B}[(n)D
: Move cursor backward n
(default 1) cells.
ANSIEscapeCode.Cursor.backward() // \u{001B}[1D
moveCursorBackward(2) // Move cursor backward 2 cells.
\u{001B}[(n)E
: Move cursor to beginning of the line n
(default 1) lines down.
ANSIEscapeCode.Cursor.nextLine(2) // \u{001B}[2E
moveCursorNextLine() // Move cursor to next line.
\u{001B}[(n)F
: Move cursor to beginning of the line n
(default 1) lines up.
ANSIEscapeCode.Cursor.previousLine(3) // \u{001B}[3F
moveCursorPreviousLine(5) // Move cursor to next line 5 times.
\u{001B}[(n)G
: Move cursor to column n
.
ANSIEscapeCode.Cursor.column(2) // \u{001B}[2G
moveCursorToColumn(2) // Move cursor to second column.
\u{001B}[(n);(m)1H
: Move cursor to row n
, column 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
: Save cursor position.
ANSIEscapeCode.Cursor.saveCursorPosition // \u{001B}[s
saveCursorPosition() // Save cursor position.
\u{001B}[u
: Restore cursor position.
ANSIEscapeCode.Cursor.restoreCursorPosition // \u{001B}[u
restoreCursorPosition() // Restore cursor position.
\u{001B}[?25l
: Hide cursor.
ANSIEscapeCode.Cursor.hideCursor // \u{001B}[?25l
hideCursor() // Hide cursor.
\u{001B}[?25h
: Show cursor.
ANSIEscapeCode.Cursor.showCursor // \u{001B}[?25h
showCursor() // Show cursor.
\u{001B}[(n)J
: Clear part of the screen. If n
is 0 (or missing), clear from cursor to end of screen. If n
is 1, clear from cursor to beginning of the screen. If n
is 2, clear entire screen.
ANSIEscapeCode.Erase.eraseInDisplay(.entireScreen) // \u{001B}[2J
eraseInDisplay(.fromCursorToBeginningOfScreen) // Erase content from cursor to beginning of screen.
\u{001B}[(n)K
: Erase part of the line. If n
is zero (or missing), clear from cursor to the end of the line. If n
is 1, clear from cursor to beginning of the line. If n
is 2, clear entire line. Cursor position does not change.
ANSIEscapeCode.Erase.eraseInLine(.fromCursorToEndOfLine) // \u{001B}[0K
eraseInLine(.entireLine) // Erase entire line.
\u{001B}[(n)S
: Scroll whole page up by n
(default 1) lines. New lines are added at the bottom.
ANSIEscapeCode.Scroll.up() // \u{001B}[1S
scrollUp(3) // Scroll up screen 3 lines.
\u{001B}[(n)T
: Scroll whole page down by n
(default 1) lines. New lines are added at the top.
ANSIEscapeCode.Scroll.down(5) // \u{001B}[5T
scrollDown() // Scroll down screen 1 line.
\u{001B}[0m
: Reset all decoration attributes.
ANSIEscapeCode.Decoration.reset // \u{001B}[0m
\u{001B}[39m
: Reset background decoration attributes.
ANSIEscapeCode.Decoration.resetBackgroundColor // \u{001B}[39m
\u{001B}[1m
: Make output bold.
ANSIEscapeCode.Decoration.bold // \u{001B}[1m
print("bold text".boldOutput) // Print bold text.
\u{001B}[3m
: Make output italic.
ANSIEscapeCode.Decoration.italic // \u{001B}[3m
print("italic text".italicOutput) // Print italic text.
\u{001B}[4m
: Make output have underline.
ANSIEscapeCode.Decoration.underline // \u{001B}[4m
print("underline text".underlineOutput) // Print underline text.
\u{001B}[5m
: Make output blink.
ANSIEscapeCode.Decoration.blink // \u{001B}[5m
print("blinking text".blinkOutput) // Print blinking text.
\u{001B}[(COLOR)m
Set text color on terminal. Check TextColor.swift
for available colors.
ANSIEscapeCode.Decoration.textColor(.red) // \u{001B}[31m
print("red text".color(.red)) // Print red text.
\u{001B}[38;5;(COLOR)m
Set text 8 bits color on terminal. Check here for available colors.
ANSIEscapeCode.Decoration.text8BitsColor(200) // \u{001B}[38;5;200m
print("orange text".colorFrom8BitsColorSet(208)) // Print orange text.
\u{001B}[38;2;(RED);(GREEN);(BLUE)m
Set text RGB color on terminal. Check here for more.
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
Set text background color on terminal. Check BackgroundColor.swift
for available colors.
ANSIEscapeCode.Decoration.backgroundColor(.red) // \u{001B}[41m
print("red background".backgroundColor(.red)) // Print text with red background.
\u{001B}[38;5;(COLOR)m
Set text background 8 bits color on terminal. Check here for available colors.
ANSIEscapeCode.Decoration.background8BitsColor(200) // \u{001B}[48;5;200m
print("orange background".backgroundColorFrom8BitsColorSet(208)) // Print text with orange background.
\u{001B}[48;2;(RED);(GREEN);(BLUE)m
Set text background RGB color on terminal. Check here for more.
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.
If you have good idea or suggestion? Please, don't hesitate to open a pull request or send me an email.
Hope you enjoy building command line tool with ANSIEscapeCode!