-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTerminalOutputView.swift
More file actions
359 lines (322 loc) · 11.4 KB
/
TerminalOutputView.swift
File metadata and controls
359 lines (322 loc) · 11.4 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import AppKit
import SwiftUI
public struct TerminalOutputView: View {
let buffer: TerminalBuffer
var fontSize: CGFloat
public init(buffer: TerminalBuffer, fontSize: CGFloat = 11) {
self.buffer = buffer
self.fontSize = fontSize
}
public var body: some View {
let _ = buffer.version
TerminalNSView(buffer: buffer, version: buffer.version, fontSize: fontSize)
}
}
private struct TerminalNSView: NSViewRepresentable {
let buffer: TerminalBuffer
let version: Int
let fontSize: CGFloat
func makeNSView(context: Context) -> NSScrollView {
let textView = NSTextView()
textView.isEditable = false
textView.isSelectable = true
textView.backgroundColor = .clear
textView.drawsBackground = false
textView.font = NSFont.monospacedSystemFont(ofSize: fontSize, weight: .regular)
textView.textContainerInset = NSSize(width: 8, height: 8)
textView.isAutomaticQuoteSubstitutionEnabled = false
textView.isAutomaticDashSubstitutionEnabled = false
textView.isAutomaticTextReplacementEnabled = false
textView.isRichText = false
textView.textContainer?.widthTracksTextView = true
textView.textContainer?.lineFragmentPadding = 4
textView.isVerticallyResizable = true
textView.isHorizontallyResizable = false
textView.autoresizingMask = [.width]
let scrollView = NSScrollView()
scrollView.documentView = textView
scrollView.hasVerticalScroller = true
scrollView.hasHorizontalScroller = false
scrollView.drawsBackground = false
scrollView.autohidesScrollers = true
context.coordinator.textView = textView
return scrollView
}
func updateNSView(_ scrollView: NSScrollView, context: Context) {
context.coordinator.update(buffer: buffer, fontSize: fontSize)
}
func makeCoordinator() -> Coordinator {
Coordinator()
}
@MainActor
class Coordinator {
weak var textView: NSTextView?
private var cachedFontSize: CGFloat = 0
private var regularFont: NSFont?
private var boldFont: NSFont?
private var colorCache: [ANSIColor: NSColor] = [:]
private var isFirstUpdate = true
func update(buffer: TerminalBuffer, fontSize: CGFloat) {
guard let textView else { return }
guard let storage = textView.textStorage else { return }
guard let scrollView = textView.enclosingScrollView else { return }
let clipView = scrollView.contentView
let maxScrollY = max(textView.frame.height - clipView.bounds.height, 0)
let isAtBottom = isFirstUpdate || clipView.bounds.origin.y >= maxScrollY - 20
ensureFontCache(fontSize: fontSize)
let attrStr = buildAttributedString(buffer: buffer, fontSize: fontSize)
storage.beginEditing()
storage.setAttributedString(attrStr)
storage.endEditing()
if isAtBottom {
if isFirstUpdate {
isFirstUpdate = false
DispatchQueue.main.async { [weak textView] in
guard let textView, let sv = textView.enclosingScrollView else { return }
sv.layoutSubtreeIfNeeded()
textView.scrollToEndOfDocument(nil)
}
} else {
textView.scrollToEndOfDocument(nil)
}
}
}
private func ensureFontCache(fontSize: CGFloat) {
guard fontSize != cachedFontSize else { return }
cachedFontSize = fontSize
regularFont = NSFont.monospacedSystemFont(ofSize: fontSize, weight: .regular)
boldFont = NSFont.monospacedSystemFont(ofSize: fontSize, weight: .bold)
colorCache.removeAll()
}
private func resolveColor(_ color: ANSIColor) -> NSColor {
if let cached = colorCache[color] { return cached }
let resolved = color.nsColor
colorCache[color] = resolved
return resolved
}
private func buildAttributedString(buffer: TerminalBuffer, fontSize: CGFloat) -> NSAttributedString {
let result = NSMutableAttributedString()
let regFont = regularFont!
let bldFont = boldFont!
let defaultAttrs: [NSAttributedString.Key: Any] = [
.font: regFont,
.foregroundColor: NSColor.labelColor,
]
for (i, line) in buffer.lines.enumerated() {
if line.isEmpty {
result.append(NSAttributedString(string: " ", attributes: defaultAttrs))
} else {
for span in line.spans {
var attrs = defaultAttrs
let style = span.style
if style.foreground != .default {
attrs[.foregroundColor] = resolveColor(style.foreground)
}
if style.bold {
attrs[.font] = bldFont
}
if style.dim, style.foreground == .default {
attrs[.foregroundColor] = NSColor.secondaryLabelColor
}
if style.underline {
attrs[.underlineStyle] = NSUnderlineStyle.single.rawValue
}
if style.strikethrough {
attrs[.strikethroughStyle] = NSUnderlineStyle.single.rawValue
}
result.append(NSAttributedString(string: span.text, attributes: attrs))
}
}
if i < buffer.lines.count - 1 {
result.append(NSAttributedString(string: "\n", attributes: defaultAttrs))
}
}
return result
}
}
}
public struct TerminalPreviewText: View {
let buffer: TerminalBuffer
var lineLimit: Int
var fontSize: CGFloat
public init(buffer: TerminalBuffer, lineLimit: Int = 3, fontSize: CGFloat = 10) {
self.buffer = buffer
self.lineLimit = lineLimit
self.fontSize = fontSize
}
public var body: some View {
Text(previewAttributedString())
.font(.system(size: fontSize, design: .monospaced))
.lineLimit(lineLimit)
.truncationMode(.tail)
.frame(maxWidth: .infinity, alignment: .leading)
}
private func previewAttributedString() -> AttributedString {
let nonEmptyLines = buffer.lines.filter { !$0.isEmpty }
let lastLines = nonEmptyLines.suffix(lineLimit)
var result = AttributedString()
for (i, line) in lastLines.enumerated() {
for span in line.spans {
var attr = AttributedString(span.text)
if span.style.foreground != .default {
attr.foregroundColor = span.style.foreground.swiftUIColor
}
if span.style.bold {
attr.font = .system(size: fontSize, weight: .bold, design: .monospaced)
}
if span.style.dim, span.style.foreground == .default {
attr.foregroundColor = .secondary
}
if span.style.underline {
attr.underlineStyle = .single
}
result.append(attr)
}
if i < lastLines.count - 1 {
result.append(AttributedString("\n"))
}
}
if result.characters.isEmpty {
var placeholder = AttributedString("Waiting for output...")
placeholder.foregroundColor = .secondary
return placeholder
}
return result
}
}
extension ANSIColor {
public var swiftUIColor: Color {
switch self {
case .default:
return .primary
case .standard(let n):
return Self.standardSwiftUIColor(n)
case .bright(let n):
return Self.brightSwiftUIColor(n)
case .palette(let n):
return Self.paletteSwiftUIColor(n)
case .rgb(let r, let g, let b):
return Color(
red: Double(r) / 255,
green: Double(g) / 255,
blue: Double(b) / 255
)
}
}
private static func standardSwiftUIColor(_ index: UInt8) -> Color {
switch index {
case 0: Color(red: 0.2, green: 0.2, blue: 0.2)
case 1: Color(red: 0.85, green: 0.25, blue: 0.25)
case 2: Color(red: 0.25, green: 0.75, blue: 0.35)
case 3: Color(red: 0.85, green: 0.75, blue: 0.25)
case 4: Color(red: 0.35, green: 0.45, blue: 0.9)
case 5: Color(red: 0.8, green: 0.35, blue: 0.8)
case 6: Color(red: 0.3, green: 0.8, blue: 0.85)
case 7: Color(red: 0.8, green: 0.8, blue: 0.8)
default: .primary
}
}
private static func brightSwiftUIColor(_ index: UInt8) -> Color {
switch index {
case 0: Color(red: 0.5, green: 0.5, blue: 0.5)
case 1: Color(red: 1.0, green: 0.35, blue: 0.35)
case 2: Color(red: 0.35, green: 0.95, blue: 0.45)
case 3: Color(red: 1.0, green: 0.95, blue: 0.35)
case 4: Color(red: 0.45, green: 0.55, blue: 1.0)
case 5: Color(red: 0.95, green: 0.45, blue: 0.95)
case 6: Color(red: 0.4, green: 0.95, blue: 1.0)
case 7: Color(red: 1.0, green: 1.0, blue: 1.0)
default: .primary
}
}
private static func paletteSwiftUIColor(_ index: UInt8) -> Color {
let n = Int(index)
if n < 8 {
return standardSwiftUIColor(index)
} else if n < 16 {
return brightSwiftUIColor(UInt8(n - 8))
} else if n < 232 {
let adjusted = n - 16
let r = adjusted / 36
let g = (adjusted % 36) / 6
let b = adjusted % 6
return Color(
red: r == 0 ? 0 : Double(r * 40 + 55) / 255,
green: g == 0 ? 0 : Double(g * 40 + 55) / 255,
blue: b == 0 ? 0 : Double(b * 40 + 55) / 255
)
} else {
let gray = Double((n - 232) * 10 + 8) / 255
return Color(red: gray, green: gray, blue: gray)
}
}
}
extension ANSIColor {
public var nsColor: NSColor {
switch self {
case .default:
return .labelColor
case .standard(let n):
return Self.standardNSColor(n)
case .bright(let n):
return Self.brightNSColor(n)
case .palette(let n):
return Self.paletteNSColor(n)
case .rgb(let r, let g, let b):
return NSColor(
red: CGFloat(r) / 255,
green: CGFloat(g) / 255,
blue: CGFloat(b) / 255,
alpha: 1
)
}
}
private static func standardNSColor(_ index: UInt8) -> NSColor {
switch index {
case 0: NSColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)
case 1: NSColor(red: 0.85, green: 0.25, blue: 0.25, alpha: 1)
case 2: NSColor(red: 0.25, green: 0.75, blue: 0.35, alpha: 1)
case 3: NSColor(red: 0.85, green: 0.75, blue: 0.25, alpha: 1)
case 4: NSColor(red: 0.35, green: 0.45, blue: 0.9, alpha: 1)
case 5: NSColor(red: 0.8, green: 0.35, blue: 0.8, alpha: 1)
case 6: NSColor(red: 0.3, green: 0.8, blue: 0.85, alpha: 1)
case 7: NSColor(red: 0.8, green: 0.8, blue: 0.8, alpha: 1)
default: .labelColor
}
}
private static func brightNSColor(_ index: UInt8) -> NSColor {
switch index {
case 0: NSColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1)
case 1: NSColor(red: 1.0, green: 0.35, blue: 0.35, alpha: 1)
case 2: NSColor(red: 0.35, green: 0.95, blue: 0.45, alpha: 1)
case 3: NSColor(red: 1.0, green: 0.95, blue: 0.35, alpha: 1)
case 4: NSColor(red: 0.45, green: 0.55, blue: 1.0, alpha: 1)
case 5: NSColor(red: 0.95, green: 0.45, blue: 0.95, alpha: 1)
case 6: NSColor(red: 0.4, green: 0.95, blue: 1.0, alpha: 1)
case 7: NSColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1)
default: .labelColor
}
}
private static func paletteNSColor(_ index: UInt8) -> NSColor {
let n = Int(index)
if n < 8 {
return standardNSColor(index)
} else if n < 16 {
return brightNSColor(UInt8(n - 8))
} else if n < 232 {
let adjusted = n - 16
let r = adjusted / 36
let g = (adjusted % 36) / 6
let b = adjusted % 6
return NSColor(
red: r == 0 ? 0 : CGFloat(r * 40 + 55) / 255,
green: g == 0 ? 0 : CGFloat(g * 40 + 55) / 255,
blue: b == 0 ? 0 : CGFloat(b * 40 + 55) / 255,
alpha: 1
)
} else {
let gray = CGFloat((n - 232) * 10 + 8) / 255
return NSColor(red: gray, green: gray, blue: gray, alpha: 1)
}
}
}