Improve LogView
This commit is contained in:
@@ -3,6 +3,11 @@ import SwiftUI
|
|||||||
|
|
||||||
public enum ANSIColors {
|
public enum ANSIColors {
|
||||||
private static let ansiRegex = try! NSRegularExpression(pattern: "\u{001B}\\[[;\\d]*m")
|
private static let ansiRegex = try! NSRegularExpression(pattern: "\u{001B}\\[[;\\d]*m")
|
||||||
|
private static let cache: NSCache<NSString, CachedAttributedString> = {
|
||||||
|
let cache = NSCache<NSString, CachedAttributedString>()
|
||||||
|
cache.countLimit = 3000
|
||||||
|
return cache
|
||||||
|
}()
|
||||||
|
|
||||||
private static let logRed = Color(red: 1.0, green: 0.13, blue: 0.35)
|
private static let logRed = Color(red: 1.0, green: 0.13, blue: 0.35)
|
||||||
private static let logGreen = Color(red: 0.18, green: 0.8, blue: 0.44)
|
private static let logGreen = Color(red: 0.18, green: 0.8, blue: 0.44)
|
||||||
@@ -12,12 +17,21 @@ public enum ANSIColors {
|
|||||||
private static let logBlueLight = Color(red: 0.36, green: 0.68, blue: 0.89)
|
private static let logBlueLight = Color(red: 0.36, green: 0.68, blue: 0.89)
|
||||||
private static let logWhite = Color(red: 0.93, green: 0.94, blue: 0.95)
|
private static let logWhite = Color(red: 0.93, green: 0.94, blue: 0.95)
|
||||||
|
|
||||||
|
public static func clearCache() {
|
||||||
|
cache.removeAllObjects()
|
||||||
|
}
|
||||||
|
|
||||||
public static func parseAnsiString(_ text: String) -> AttributedString {
|
public static func parseAnsiString(_ text: String) -> AttributedString {
|
||||||
let nsString = text as NSString
|
let nsString = text as NSString
|
||||||
|
if let cached = cache.object(forKey: nsString) {
|
||||||
|
return cached.value
|
||||||
|
}
|
||||||
let matches = ansiRegex.matches(in: text, range: NSRange(location: 0, length: nsString.length))
|
let matches = ansiRegex.matches(in: text, range: NSRange(location: 0, length: nsString.length))
|
||||||
|
|
||||||
if matches.isEmpty {
|
if matches.isEmpty {
|
||||||
return AttributedString(text)
|
let plain = AttributedString(text)
|
||||||
|
cache.setObject(CachedAttributedString(plain), forKey: nsString)
|
||||||
|
return plain
|
||||||
}
|
}
|
||||||
|
|
||||||
var cleanText = text
|
var cleanText = text
|
||||||
@@ -60,6 +74,7 @@ public enum ANSIColors {
|
|||||||
attributedString[startIndex...].mergeAttributes(existingStyle)
|
attributedString[startIndex...].mergeAttributes(existingStyle)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cache.setObject(CachedAttributedString(attributedString), forKey: nsString)
|
||||||
return attributedString
|
return attributedString
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,3 +140,11 @@ public enum ANSIColors {
|
|||||||
return hasAttribute ? container : nil
|
return hasAttribute ? container : nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private final class CachedAttributedString: NSObject {
|
||||||
|
let value: AttributedString
|
||||||
|
|
||||||
|
init(_ value: AttributedString) {
|
||||||
|
self.value = value
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import Library
|
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
import Library
|
||||||
|
|
||||||
public struct LogView: View {
|
public struct LogView: View {
|
||||||
@Environment(\.selection) private var selection
|
@Environment(\.selection) private var selection
|
||||||
@@ -27,14 +27,13 @@ public struct LogView: View {
|
|||||||
"sing-box started (1.666s)",
|
"sing-box started (1.666s)",
|
||||||
]
|
]
|
||||||
ScrollView {
|
ScrollView {
|
||||||
VStack(alignment: .leading, spacing: 0) {
|
LazyVStack(alignment: .leading, spacing: 8) {
|
||||||
ForEach(Array(logList.enumerated()), id: \.offset) { it in
|
ForEach(logList.indices, id: \.self) { index in
|
||||||
Text(ANSIColors.parseAnsiString(it.element))
|
Text(ANSIColors.parseAnsiString(logList[index]))
|
||||||
.font(logFont)
|
.font(logFont)
|
||||||
#if os(tvOS)
|
#if os(tvOS)
|
||||||
.focusable()
|
.focusable()
|
||||||
#endif
|
#endif
|
||||||
Spacer(minLength: 8)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
|
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
|
||||||
@@ -57,20 +56,13 @@ public struct LogView: View {
|
|||||||
} else {
|
} else {
|
||||||
ScrollViewReader { reader in
|
ScrollViewReader { reader in
|
||||||
ScrollView {
|
ScrollView {
|
||||||
LazyVGrid(columns: [GridItem(.flexible())], alignment: .leading, spacing: 0) {
|
LazyVStack(alignment: .leading, spacing: 8) {
|
||||||
ForEach(Array(commandClient.logList.enumerated()), id: \.offset) { it in
|
ForEach(commandClient.logList.indices, id: \.self) { index in
|
||||||
Text(ANSIColors.parseAnsiString(it.element))
|
Text(ANSIColors.parseAnsiString(commandClient.logList[index]))
|
||||||
.font(logFont)
|
.font(logFont)
|
||||||
#if os(tvOS)
|
#if os(tvOS)
|
||||||
.focusable()
|
.focusable()
|
||||||
#endif
|
#endif
|
||||||
Spacer(minLength: 8)
|
|
||||||
}
|
|
||||||
|
|
||||||
.onChangeCompat(of: commandClient.logList.count) { newCount in
|
|
||||||
withAnimation {
|
|
||||||
reader.scrollTo(newCount - 1)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
|
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
|
||||||
@@ -81,7 +73,17 @@ public struct LogView: View {
|
|||||||
.focusSection()
|
.focusSection()
|
||||||
#endif
|
#endif
|
||||||
.onAppear {
|
.onAppear {
|
||||||
reader.scrollTo(commandClient.logList.count - 1)
|
if let lastIndex = commandClient.logList.indices.last {
|
||||||
|
reader.scrollTo(lastIndex, anchor: .bottom)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.onChangeCompat(of: commandClient.logList.count) { _ in
|
||||||
|
guard let lastIndex = commandClient.logList.indices.last else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
withAnimation {
|
||||||
|
reader.scrollTo(lastIndex, anchor: .bottom)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user