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
Installation
Add ANSIEscapeCode to Package.swift
.
dependencies: [
.package(url: "https://github.com/flintbox/ANSIEscapeCode", from: "0.1.1")
]
Cursor
Up
\u{001B}[(n)A
: Move cursor up n
(default 1) cells.
ANSIEscapeCode.Cursor.up(3) // \u{001B}[3A
moveCursorUp() /// Move cursor up 1 cell.
Down
\u{001B}[(n)B
: Move cursor down n
(default 1) cells.
ANSIEscapeCode.Cursor.down() // \u{001B}[1B
moveCursorDown(5) /// Move cursor down 5 cells.
Forward
\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.
Backward
\u{001B}[(n)D
: Move cursor backward n
(default 1) cells.
ANSIEscapeCode.Cursor.backward() // \u{001B}[1D
moveCursorBackward(2) // Move cursor backward 2 cells.
Next Line
\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.
Previous 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.
Column
\u{001B}[(n)G
: Move cursor to column n
.
ANSIEscapeCode.Cursor.column(2) // \u{001B}[2G
moveCursorToColumn(2) // Move cursor to second column.
Position
\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.
Save Position
\u{001B}[s
: Save cursor position.
ANSIEscapeCode.Cursor.saveCursorPosition // \u{001B}[s
saveCursorPosition() // Save cursor position.
Restore Position
\u{001B}[u
: Restore cursor position.
ANSIEscapeCode.Cursor.restoreCursorPosition // \u{001B}[u
restoreCursorPosition() // Restore cursor position.
Hide
\u{001B}[?25l
: Hide cursor.
ANSIEscapeCode.Cursor.hideCursor // \u{001B}[?25l
hideCursor() // Hide cursor.
Show
\u{001B}[?25h
: Show cursor.
ANSIEscapeCode.Cursor.showCursor // \u{001B}[?25h
showCursor() // Show cursor.
Erase
Erase in Display
\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.
Erase in Line
\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.
Scroll
Scroll Up
\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.
Scroll Down
\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.
Decoration
Reset
\u{001B}[0m
: Reset all decoration attributes.
ANSIEscapeCode.Decoration.reset // \u{001B}[0m
Reset Background Color
\u{001B}[39m
: Reset background decoration attributes.
ANSIEscapeCode.Decoration.resetBackgroundColor // \u{001B}[39m
Bold
\u{001B}[1m
: Make output bold.
ANSIEscapeCode.Decoration.bold // \u{001B}[1m
print("bold text".boldOutput) // Print bold text.
Italic
\u{001B}[3m
: Make output italic.
ANSIEscapeCode.Decoration.italic // \u{001B}[3m
print("italic text".italicOutput) // Print italic text.
Underline
\u{001B}[4m
: Make output have underline.
ANSIEscapeCode.Decoration.underline // \u{001B}[4m
print("underline text".underlineOutput) // Print underline text.
Blink
\u{001B}[5m
: Make output blink.
ANSIEscapeCode.Decoration.blink // \u{001B}[5m
print("blinking text".blinkOutput) // Print blinking text.
Text Color
\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.
Text 8 Bits Color
\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.
Text RGB Color
\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.
Background Color
\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.
Background 8 Bits Color
\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.
Background RGB Color
\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.
Contribute
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!