From f14492d3fefe43cff09dafd6fc9c43621d667eaf Mon Sep 17 00:00:00 2001 From: neon443 <69979447+neon443@users.noreply.github.com> Date: Fri, 27 Jun 2025 10:38:03 +0100 Subject: [PATCH] added applyTheme() function added SwiftTerm.Color.uiColor extension to get a UIColor from SwiftTerm.Color rename SSHTerminalView to SSHTerminalDelegate --- ShhShell.xcodeproj/project.pbxproj | 8 ++++---- ShhShell/Themes/Theme.swift | 13 +++++++++++-- ...TerminalView.swift => SSHTerminalDelegate.swift} | 13 ++++++++++++- ShhShell/Views/Terminal/TerminalController.swift | 4 ++-- 4 files changed, 29 insertions(+), 9 deletions(-) rename ShhShell/Views/Terminal/{SSHTerminalView.swift => SSHTerminalDelegate.swift} (84%) diff --git a/ShhShell.xcodeproj/project.pbxproj b/ShhShell.xcodeproj/project.pbxproj index 8a540d9..eafaf95 100644 --- a/ShhShell.xcodeproj/project.pbxproj +++ b/ShhShell.xcodeproj/project.pbxproj @@ -9,7 +9,7 @@ /* Begin PBXBuildFile section */ A9083E402DF2226F0042906E /* libz.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = A9083E3F2DF2225A0042906E /* libz.tbd */; }; A923172A2E07113100ECE1E6 /* TerminalController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A92317292E07113100ECE1E6 /* TerminalController.swift */; }; - A923172D2E07138000ECE1E6 /* SSHTerminalView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A923172C2E07138000ECE1E6 /* SSHTerminalView.swift */; }; + A923172D2E07138000ECE1E6 /* SSHTerminalDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A923172C2E07138000ECE1E6 /* SSHTerminalDelegate.swift */; }; A923172F2E08851200ECE1E6 /* ShellView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A923172E2E08851200ECE1E6 /* ShellView.swift */; }; A92538C82DEE0742007E0A18 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A92538C52DEE0742007E0A18 /* ContentView.swift */; }; A92538C92DEE0742007E0A18 /* ShhShellApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = A92538C62DEE0742007E0A18 /* ShhShellApp.swift */; }; @@ -79,7 +79,7 @@ /* Begin PBXFileReference section */ A9083E3F2DF2225A0042906E /* libz.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libz.tbd; path = usr/lib/libz.tbd; sourceTree = SDKROOT; }; A92317292E07113100ECE1E6 /* TerminalController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TerminalController.swift; sourceTree = ""; }; - A923172C2E07138000ECE1E6 /* SSHTerminalView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SSHTerminalView.swift; sourceTree = ""; }; + A923172C2E07138000ECE1E6 /* SSHTerminalDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SSHTerminalDelegate.swift; sourceTree = ""; }; A923172E2E08851200ECE1E6 /* ShellView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = ShellView.swift; path = ShhShell/Views/Terminal/ShellView.swift; sourceTree = SOURCE_ROOT; }; A925389A2DEE06DC007E0A18 /* ShhShell.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ShhShell.app; sourceTree = BUILT_PRODUCTS_DIR; }; A92538A72DEE06DE007E0A18 /* ShhShellTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = ShhShellTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -153,7 +153,7 @@ children = ( A92317292E07113100ECE1E6 /* TerminalController.swift */, A923172E2E08851200ECE1E6 /* ShellView.swift */, - A923172C2E07138000ECE1E6 /* SSHTerminalView.swift */, + A923172C2E07138000ECE1E6 /* SSHTerminalDelegate.swift */, ); path = Terminal; sourceTree = ""; @@ -482,7 +482,7 @@ A98554592E0553AA009051BD /* KeyManager.swift in Sources */, A9C897EF2DF1A9A400EF9A5F /* SSHHandler.swift in Sources */, A98554552E05535F009051BD /* KeyManagerView.swift in Sources */, - A923172D2E07138000ECE1E6 /* SSHTerminalView.swift in Sources */, + A923172D2E07138000ECE1E6 /* SSHTerminalDelegate.swift in Sources */, A96C6AFE2E0C43B600F377FE /* Keypair.swift in Sources */, A9C4140C2E096DB7005E3047 /* SSHError.swift in Sources */, A923172A2E07113100ECE1E6 /* TerminalController.swift in Sources */, diff --git a/ShhShell/Themes/Theme.swift b/ShhShell/Themes/Theme.swift index 26c71c2..67c54e2 100644 --- a/ShhShell/Themes/Theme.swift +++ b/ShhShell/Themes/Theme.swift @@ -7,6 +7,7 @@ import Foundation import SwiftTerm +import SwiftUI struct Theme: Hashable, Equatable { var name: String @@ -21,7 +22,6 @@ struct Theme: Hashable, Equatable { static func fromiTermColors(name: String, data: Data?) -> Theme? { guard let data else { return nil } - guard let string = String(data: data, encoding: .utf8) else { return nil } let decoder = PropertyListDecoder() @@ -96,7 +96,7 @@ struct ThemeCodable: Codable { } extension ThemeCodable { - var ansi: [Color] { + var ansi: [SwiftTerm.Color] { let arr = [ansi0, ansi1, ansi2, ansi3, ansi4, ansi5, ansi6, ansi7, ansi8, ansi9, ansi10, ansi11, ansi12, ansi13, ansi14, ansi15] return arr.map(SwiftTerm.Color.init) } @@ -123,3 +123,12 @@ extension SwiftTerm.Color { self.init(red: red, green: green, blue: blue) } } + +extension SwiftTerm.Color { + var uiColor: UIColor { + let red = CGFloat(self.red/65535) + let green = CGFloat(self.green/65535) + let blue = CGFloat(self.blue/65535) + return UIColor(red: red, green: green, blue: blue, alpha: 1) + } +} diff --git a/ShhShell/Views/Terminal/SSHTerminalView.swift b/ShhShell/Views/Terminal/SSHTerminalDelegate.swift similarity index 84% rename from ShhShell/Views/Terminal/SSHTerminalView.swift rename to ShhShell/Views/Terminal/SSHTerminalDelegate.swift index db6a072..d5b92a9 100644 --- a/ShhShell/Views/Terminal/SSHTerminalView.swift +++ b/ShhShell/Views/Terminal/SSHTerminalDelegate.swift @@ -10,7 +10,7 @@ import UIKit import SwiftTerm @MainActor -final class SSHTerminalView: TerminalView, Sendable, @preconcurrency TerminalViewDelegate { +final class SSHTerminalDelegate: TerminalView, Sendable, @preconcurrency TerminalViewDelegate { var handler: SSHHandler? public convenience init(frame: CGRect, handler: SSHHandler) { @@ -48,6 +48,17 @@ final class SSHTerminalView: TerminalView, Sendable, @preconcurrency TerminalVie } } + func applyTheme(_ theme: Theme) { + getTerminal().installPalette(colors: theme.ansi) + getTerminal().foregroundColor = theme.foreground + getTerminal().backgroundColor = theme.background + + caretColor = theme.cursor.uiColor + selectedTextBackgroundColor = theme.selection.uiColor + + // TODO: selectedtext and cursor colors + } + public override init(frame: CGRect) { super.init(frame: frame) terminalDelegate = self diff --git a/ShhShell/Views/Terminal/TerminalController.swift b/ShhShell/Views/Terminal/TerminalController.swift index c9e480e..db137c2 100644 --- a/ShhShell/Views/Terminal/TerminalController.swift +++ b/ShhShell/Views/Terminal/TerminalController.swift @@ -14,7 +14,7 @@ struct TerminalController: UIViewRepresentable { @ObservedObject var handler: SSHHandler final class TerminalViewContainer { - @MainActor static var shared: SSHTerminalView? + @MainActor static var shared: SSHTerminalDelegate? } func makeUIView(context: Context) -> TerminalView { @@ -22,7 +22,7 @@ struct TerminalController: UIViewRepresentable { return existing } - let tv = SSHTerminalView(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: .zero), handler: handler) + let tv = SSHTerminalDelegate(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: .zero), handler: handler) tv.translatesAutoresizingMaskIntoConstraints = false tv.autoresizingMask = [.flexibleWidth, .flexibleHeight]