-
Notifications
You must be signed in to change notification settings - Fork 3
/
TerminalViewController.swift
201 lines (160 loc) · 7.39 KB
/
TerminalViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import UIKit
import Terminal
final class TerminalViewController: UIViewController, TerminalViewDelegate, KeyboardPickerDelegate {
private static let initialFontSize = "9"
private static let fontSizes: [String] = Array(1...50).map { String($0) }
private static let initialCursorShape = TerminalCursorShape.beam.rawValue
private static let cursorShapes: [String] = [
TerminalCursorShape.block.rawValue,
TerminalCursorShape.beam.rawValue,
TerminalCursorShape.underline.rawValue,
]
private static let initialCursorBlink = "true"
private static let cursorBlinks: [String] = ["true", "false"]
private let terminalInputAccessoryView = InputAccessoryView.createFromNib()
@IBOutlet private var terminalView: TerminalView!
@IBOutlet private var fontSizePicker: KeyboardPicker!
@IBOutlet private var cursorShapePicker: KeyboardPicker!
@IBOutlet private var cursorBlinkPicker: KeyboardPicker!
@IBOutlet private var terminalModeChangeButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
terminalInputAccessoryView?.terminalView = terminalView
terminalView.delegate = self
terminalView.bottomAnchor.constraint(equalTo: view.keyboardLayoutGuide.topAnchor, constant: -8.0).isActive = true // fail if write `@IBOutlet var didSet` scope
fontSizePicker.dataSource = Self.fontSizes
fontSizePicker.delegate = self
fontSizePicker.selectedData = Self.initialFontSize
cursorShapePicker.dataSource = Self.cursorShapes
cursorShapePicker.delegate = self
cursorShapePicker.selectedData = Self.initialCursorShape
cursorBlinkPicker.dataSource = Self.cursorBlinks
cursorBlinkPicker.delegate = self
cursorBlinkPicker.selectedData = Self.initialCursorBlink
terminalViewDidChangeMode(terminalView)
}
@IBAction private func handleTouchUpInsideClearScrollbackButton(_ sender: UIButton) {
terminalView?.terminalClear(.clearScrollback)
}
@IBAction private func handleTouchUpInsideResetButton(_ sender: UIButton) {
terminalView?.terminalClear(.reset)
}
@IBAction private func handleTouchUpInsideSoftResetButton(_ sender: UIButton) {
terminalView?.terminalClear(.softReset)
}
@IBAction private func handleTouchUpInsideReload(_ sender: UIButton) {
terminalView?.reloadTerminal()
}
@IBAction private func handleTouchUpInsideScrollHome(_ sender: UIButton) {
terminalView?.terminalScroll(.home)
}
@IBAction private func handleTouchUpInsideScrollEnd(_ sender: UIButton) {
terminalView?.terminalScroll(.end)
}
@IBAction private func handleTouchUpInsideScrollPageUp(_ sender: UIButton) {
terminalView?.terminalScroll(.pageUp)
}
@IBAction private func handleTouchUpInsideScrollPageDown(_ sender: UIButton) {
terminalView?.terminalScroll(.pageDown)
}
@IBAction private func handleTouchUpInsideScrollLineUp(_ sender: UIButton) {
terminalView?.terminalScroll(.lineUp)
}
@IBAction private func handleTouchUpInsideScrollLineDown(_ sender: UIButton) {
terminalView?.terminalScroll(.lineDown)
}
@IBAction private func handleTouchUpInsideModeButton(_ sender: UIButton) {
guard let t = terminalView else {
return
}
switch t.mode {
case .input: t.enterScrollMode()
case .scroll: t.enterInputMode()
}
}
@IBAction private func handleTouchUpInsideEditableChangeButton(_ sender: UIButton) {
guard let t = terminalView else {
return
}
t.isTerminalContentEditable.toggle()
sender.setTitle("Editable: \(t.isTerminalContentEditable)", for: .normal)
}
@IBAction private func handleTouchUpInsideGetVisibleTextButton(_ sender: UIButton) {
terminalView?.getVisibleText {
print($0)
}
}
@IBAction private func handleTouchUpInsideGetAllTextButton(_ sender: UIButton) {
terminalView?.getAllText {
print($0)
}
}
func terminalViewDidLoad(_ terminalView: TerminalView) {
print("[TerminalViewController(TerminalViewDelegate).terminalViewDidLoad:]")
}
func terminalView(_ terminalView: TerminalView, didChangeTerminalSize oldSize: TerminalSize) {
print("[TerminalViewController(TerminalViewDelegate).terminalView:didChangeTerminalSize:] oldSize: \(oldSize), currentSize: \(terminalView.terminalSize)")
}
func terminalView(_ terminalView: TerminalView, didHandleURL url: URL) {
print("[TerminalViewController(TerminalViewDelegate).terminalView:didHandleURL:] url: \(url)")
}
func terminalView(_ terminalView: TerminalView, didHandleKeyInput string: String) {
print("[TerminalViewController(TerminalViewDelegate).terminalView:didHandleKeyInput:] string: \(string)")
if let data = string.data(using: .utf8) {
terminalView.appendBuffer(data)
}
}
func terminalView(_ terminalView: TerminalView, didHandleSendString string: String) {
print("[TerminalViewController(TerminalViewDelegate).terminalView:didHandleSendString:] string: \(string)")
if let data = string.data(using: .utf8) {
terminalView.appendBuffer(data)
}
}
func terminalViewDidHandleDeleteBackward(_ terminalView: TerminalView) {
print("[TerminalViewController(TerminalViewDelegate).terminalViewDidHandleDeleteBackward:]")
// terminalView.terminalErase(.toLeft)
// terminalView.terminalClear(.clear)
terminalView.press(.backspace)
}
func terminalView(_ terminalView: TerminalView, didHandleOnVTKeyStroke string: String) {
print("[TerminalViewController(TerminalViewDelegate).terminalView:didHandleOnVTKeyStroke:] \(string)")
if let data = string.data(using: .utf8) {
terminalView.appendBuffer(data)
}
}
func terminalViewRequestInputAccessoryView(_ terminalView: TerminalView) -> UIView? {
print("[TerminalViewController(TerminalViewDelegate).terminalView:terminalViewRequestInputAccessoryView:]")
return terminalInputAccessoryView
}
func terminalViewRequestInputAccessoryViewController(_ terminalView: TerminalView) -> UIInputViewController? {
print("[TerminalViewController(TerminalViewDelegate).terminalView:terminalViewRequestInputAccessoryViewController:]")
return nil
}
func terminalViewDidChangeMode(_ terminalView: TerminalView) {
let title: String
switch terminalView.mode {
case .input: title = "Mode: input"
case .scroll: title = "Mode: scroll"
}
terminalModeChangeButton.setTitle(title, for: .normal)
}
func keyboardPicker(_ keyboardPicker: KeyboardPicker, didSelect index: Int) {
guard let terminalView = terminalView else {
return
}
switch keyboardPicker {
case fontSizePicker:
let fontSize = UInt(keyboardPicker.selectedData, fallback: 9)
terminalView.terminalFontSize = fontSize
case cursorShapePicker:
if let shape = TerminalCursorShape(rawValue: keyboardPicker.selectedData) {
terminalView.terminalCursorShape = shape
}
case cursorBlinkPicker:
let isBlink = keyboardPicker.selectedData == "true"
terminalView.isTerminalCursorBlink = isBlink
default:
break
}
}
}