CommandLineToolkit

Форк
0
140 строк · 2.9 Кб
1
// Reference: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
2

3
enum CursorStyle: UInt8 {
4
    case block = 1
5
    case line  = 3
6
    case bar   = 5
7
}
8

9
extension ANSITerminal {
10
    func setCursorStyle(_ style: CursorStyle, blinking: Bool = true) {
11
        if blinking {
12
            write(.CSI, "\(style.rawValue) q")
13
        } else {
14
            write(.CSI, "\(style.rawValue + 1) q")
15
        }
16
    }
17

18
    func storeCursorPosition(isANSI: Bool = false) {
19
        if isANSI { write(.CSI, "s") } else { write(.ESC, "7") }
20
    }
21

22
    func restoreCursorPosition(isANSI: Bool = false) {
23
        if isANSI { write(.CSI, "u") } else { write(.ESC, "8") }
24
    }
25

26
    func enableInverted() {
27
        write(.CSI, "7m")
28
    }
29

30
    func disableInverted() {
31
        write(.CSI, "27m")
32
    }
33

34
    func enableAlternateBuffer() {
35
        write(.CSI, "?1049h")
36
    }
37

38
    func disableAlternateBuffer() {
39
        write(.CSI, "?1049l")
40
    }
41

42
    func clearBelow() {
43
        write(.CSI, "0J")
44
    }
45

46
    func clearAbove() {
47
        write(.CSI, "1J")
48
    }
49

50
    func clearScreen() {
51
        write(.CSI, "2J", .CSI, "H")
52
    }
53

54
    func clearToEndOfLine() {
55
        write(.CSI, "0K")
56
    }
57

58
    func clearToStartOfLine() {
59
        write(.CSI, "1K")
60
    }
61

62
    func clearLine() {
63
        write(.CSI, "2K")
64
    }
65

66
    func scrollUp(row: Int = 1) {
67
        write(.CSI, "\(row)^")
68
    }
69

70
    func moveUp(_ row: Int = 1) {
71
        write(.CSI, "\(row)A")
72
    }
73

74
    func moveDown(_ row: Int = 1) {
75
        write(.CSI, "\(row)B")
76
    }
77

78
    func moveRight(_ col: Int = 1) {
79
        write(.CSI, "\(col)C")
80
    }
81

82
    func moveLeft(_ col: Int = 1) {
83
        write(.CSI, "\(col)D")
84
    }
85

86
    func moveLineDown(_ row: Int = 1) {
87
        write(.CSI, "\(row)E")
88
    }
89

90
    func moveLineUp(_ row: Int = 1) {
91
        write(.CSI, "\(row)F")
92
    }
93

94
    func moveToColumn(_ col: Int) {
95
        write(.CSI, "\(col)G")
96
    }
97

98
    func moveTo(_ row: Int, _ col: Int) {
99
        write(.CSI, "\(row);\(col)H")
100
    }
101

102
    func insertLine(_ row: Int = 1) {
103
        write(.CSI, "\(row)L")
104
    }
105

106
    func deleteLine(_ row: Int = 1) {
107
        write(.CSI, "\(row)M")
108
    }
109

110
    func deleteChar(_ char: Int = 1) {
111
        write(.CSI, "\(char)P")
112
    }
113

114
    func cursorOff() {
115
        write(.CSI, "?25l")
116
        isCursorVisible = false
117
    }
118

119
    func cursorOn() {
120
        write(.CSI, "?25h")
121
        isCursorVisible = true
122
    }
123

124
    // swiftlint:disable force_unwrapping
125

126
    func readCursorPos() -> Position {
127
        let str = ansiRequest(.CSI + "6n", endChar: "R")  // returns ^[row;colR
128
        if str.isEmpty { return .init(row: -1, col: -1) }
129

130
        let esc = str.firstIndex(of: "[")!
131
        let del = str.firstIndex(of: ";")!
132
        let end = str.firstIndex(of: "R")!
133
        let row = String(str[str.index(after: esc)...str.index(before: del)])
134
        let col = String(str[str.index(after: del)...str.index(before: end)])
135

136
        return .init(row: Int(row)!, col: Int(col)!)
137
    }
138

139
    // swiftlint:enable force_unwrapping
140
}
141

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.