|
| 1 | +// Copyright © 2025 Saleem Abdulrasool <compnerd@compnerd.org> |
| 2 | +// SPDX-License-Identifier: BSD-3-Clause |
| 3 | + |
| 4 | +public enum DECTerminalType: UInt8 { |
| 5 | + // VT100 Series |
| 6 | + |
| 7 | + // VT125 Series (Graphics) |
| 8 | + case VT125 = 12 // VT125 with ReGIS graphics |
| 9 | + |
| 10 | + // VT200 Series |
| 11 | + case VT220 = 62 // VT220 |
| 12 | + |
| 13 | + // VT300 Series |
| 14 | + case VT320 = 63 // VT320 |
| 15 | + |
| 16 | + // VT400 Series |
| 17 | + case VT420 = 64 // VT420 |
| 18 | +} |
| 19 | + |
| 20 | +extension DECTerminalType: Sendable { } |
| 21 | +extension DECTerminalType: CaseIterable { } |
| 22 | + |
| 23 | +public enum VTDAFeature: UInt8 { |
| 24 | + case ExtendedColumns = 1 // 132 columns |
| 25 | + case PrinterPort = 2 |
| 26 | + case ReGISGraphics = 3 |
| 27 | + case SixelGraphics = 4 |
| 28 | + case SelectiveErase = 6 |
| 29 | + case SoftCharacterSet = 7 |
| 30 | + case UserDefinedKeys = 8 |
| 31 | + case NationalReplacementCharacterSet = 9 |
| 32 | + case SerboCroatianCharacterSet = 12 |
| 33 | + case EightBitInterfaceArchitecture = 14 |
| 34 | + case TechnicalCharacterSet = 15 |
| 35 | + case LocatorPort = 16 |
| 36 | + case TerminalStateInterrogation = 17 |
| 37 | + case WindowingCapability = 18 |
| 38 | + case Sessions = 19 |
| 39 | + case HorizontalScrolling = 21 |
| 40 | + case ANSIColor = 22 |
| 41 | + case GreekCharacterSet = 23 |
| 42 | + case TurkishCharacterSet = 24 |
| 43 | + case RectangularAreaOperations = 28 |
| 44 | + case ANSITextLocator = 29 |
| 45 | + case TextMacros = 32 |
| 46 | + case ISOLatin2CharacterSet = 42 |
| 47 | + case PCTerm = 44 |
| 48 | + case SoftKeyMapping = 45 |
| 49 | + case ASCIIEmulation = 46 |
| 50 | + case ClipboardAccess = 52 |
| 51 | +} |
| 52 | + |
| 53 | +public struct VTExtensions: Sendable, OptionSet { |
| 54 | + public typealias RawValue = VTDAFeature.RawValue |
| 55 | + |
| 56 | + public let rawValue: RawValue |
| 57 | + |
| 58 | + public init(rawValue: RawValue) { |
| 59 | + self.rawValue = rawValue |
| 60 | + } |
| 61 | + |
| 62 | + internal init(_ parameter: VTDAFeature) { |
| 63 | + self.init(rawValue: 1 << parameter.rawValue) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +extension VTExtensions { |
| 68 | + public static var none: VTExtensions { |
| 69 | + VTExtensions(rawValue: 0) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +public struct VTCapabilities: Sendable { |
| 74 | + public let standard: DECTerminalType? |
| 75 | + public let features: VTExtensions |
| 76 | + |
| 77 | +} |
| 78 | + |
| 79 | +extension VTCapabilities { |
| 80 | + /// Queries the terminal to determine its capabilities and supported features. |
| 81 | + /// |
| 82 | + /// Use this method to detect what your terminal supports instead of making assumptions |
| 83 | + /// or relying on environment variables. This allows your application to gracefully |
| 84 | + /// adapt its behavior based on actual terminal capabilities. |
| 85 | + /// |
| 86 | + /// ```swift |
| 87 | + /// if let capabilities = await VTCapabilities.query(terminal) { |
| 88 | + /// if capabilities.features.contains(.ANSIColor) { |
| 89 | + /// // Safe to use colors |
| 90 | + /// await terminal <<< .SelectGraphicRendition([.foreground(.red)]) |
| 91 | + /// } |
| 92 | + /// } else { |
| 93 | + /// // Use basic functionality only |
| 94 | + /// } |
| 95 | + /// ``` |
| 96 | + /// |
| 97 | + /// - Parameters: |
| 98 | + /// - terminal: The terminal to query |
| 99 | + /// - timeout: How long to wait for a response (default: 250ms) |
| 100 | + /// - Returns: Terminal capabilities, or `nil` if detection failed |
| 101 | + public static func query(_ terminal: some VTTerminal, |
| 102 | + timeout: Duration = .milliseconds(250)) async |
| 103 | + -> VTCapabilities? { |
| 104 | + return await withTaskGroup(of: Void.self) { group in |
| 105 | + defer { group.cancelAll() } |
| 106 | + |
| 107 | + var capabilities: VTCapabilities? |
| 108 | + |
| 109 | + group.addTask { |
| 110 | + capabilities = try? await Task<VTCapabilities, Error>.withTimeout(timeout: timeout) { |
| 111 | + for try await event in terminal.input { |
| 112 | + if case let .key(event) = event, case let .response(attributes) = event.type { |
| 113 | + let standard: DECTerminalType? = |
| 114 | + if case let .some(value) = attributes.first, |
| 115 | + let id = UInt8(exactly: value) { |
| 116 | + DECTerminalType(rawValue: id) |
| 117 | + } else { |
| 118 | + nil |
| 119 | + } |
| 120 | + |
| 121 | + let features = attributes.dropFirst() |
| 122 | + .compactMap(UInt8.init(exactly:)) |
| 123 | + .compactMap(VTDAFeature.init(rawValue:)) |
| 124 | + .compactMap(VTExtensions.init) |
| 125 | + .reduce(into: VTExtensions()) { $0.formUnion($1) } |
| 126 | + |
| 127 | + return VTCapabilities(standard: standard, features: features) |
| 128 | + } |
| 129 | + } |
| 130 | + return .unknown |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + group.addTask { |
| 135 | + await terminal <<< .DeviceAttributes(.Request) |
| 136 | + } |
| 137 | + |
| 138 | + await group.waitForAll() |
| 139 | + |
| 140 | + return capabilities |
| 141 | + } |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +extension VTCapabilities { |
| 146 | + public static var unknown: VTCapabilities { |
| 147 | + VTCapabilities(standard: nil, features: []) |
| 148 | + } |
| 149 | +} |
0 commit comments