SPB Git

Repository Public

NEXTerminalView.swift 3.4 KB 86 lines swift

1import AppKit2import SwiftTerm3import WorkspaceCore45/// The AppKit terminal view behind every pane: SwiftTerm's `LocalProcessTerminalView`6/// plus NEXTERM appearance handling, paste protection and find-bar helpers.7/// Instances are owned by `TerminalSession`, not by SwiftUI; hosting views only reparent them.8/// Focus is tracked by the host container (SwiftTerm's responder overrides are not `open`).9@MainActor10public final class NEXTerminalView: LocalProcessTerminalView {11    /// When set, multi-line pastes are routed here first. Call the continuation to proceed.12    public var pasteConfirmation: ((_ text: String, _ proceed: @escaping @MainActor () -> Void) -> Void)?13    /// Paste protection toggle (settings).14    public var confirmMultilinePaste = true1516    public override func paste(_ sender: Any) {17        guard confirmMultilinePaste, let handler = pasteConfirmation,18              let text = NSPasteboard.general.string(forType: .string),19              text.contains("\n") || text.contains("\r") else {20            super.paste(sender)21            return22        }23        handler(text) { [weak self] in24            self?.pasteWithoutConfirmation(sender)25        }26    }2728    private func pasteWithoutConfirmation(_ sender: Any) {29        let previous = confirmMultilinePaste30        confirmMultilinePaste = false31        super.paste(sender)32        confirmMultilinePaste = previous33    }3435    public func apply(appearance: TerminalAppearance) {36        let theme = appearance.theme37        font = appearance.makeFont()38        if let fg = NSColor(hex: theme.foreground) { nativeForegroundColor = fg }39        if let bg = NSColor(hex: theme.background) { nativeBackgroundColor = bg }40        if let cursor = NSColor(hex: theme.cursor) { caretColor = cursor }41        if let sel = NSColor(hex: theme.selection) { selectedTextBackgroundColor = sel }42        let palette = theme.ansi.compactMap { NSColor(hex: $0)?.swiftTermColor }43        if palette.count == 16 { installColors(palette) }44        optionAsMetaKey = appearance.optionAsMeta45        allowMouseReporting = appearance.allowMouseReporting46        switch appearance.cursorStyle {47        case "underline": setCursorStyle(appearance.cursorBlinks ? .blinkUnderline : .steadyUnderline)48        case "bar": setCursorStyle(appearance.cursorBlinks ? .blinkBar : .steadyBar)49        default: setCursorStyle(appearance.cursorBlinks ? .blinkBlock : .steadyBlock)50        }51        changeScrollback(appearance.scrollbackLines)52    }5354    public func setBell(style: String) {55        switch style {56        case "visual": bellStyle = .visual57        case "none": bellStyle = .none58        default: bellStyle = .sound59        }60    }6162    // MARK: Find (drives SwiftTerm's built-in find bar)6364    private func findAction(_ action: NSFindPanelAction) {65        let item = NSMenuItem()66        item.tag = Int(action.rawValue)67        performFindPanelAction(item)68    }6970    public func showFind() { findAction(.showFindPanel) }71    public func findNextMatch() { findAction(.next) }72    public func findPreviousMatch() { findAction(.previous) }7374    /// Clears the visible screen and scrollback (like Terminal.app ⌘K): resets via the terminal, not the process.75    public func clearScreenAndScrollback() {76        clearScrollback()77        send(txt: "\u{0c}")78    }7980    /// Terminal dimensions as the child sees them.81    public var gridSize: (cols: Int, rows: Int) {82        let size = getWindowSize()83        return (Int(size.ws_col), Int(size.ws_row))84    }85}86
Files main
Menu

Theme