restores Helder in all its glory
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
import AppKit
|
||||
import HelderCore
|
||||
import SwiftTerm
|
||||
|
||||
/// Maps the config's CSS colour table onto SwiftTerm's ANSI palette.
|
||||
enum TerminalColors {
|
||||
/// Channels are 16-bit in SwiftTerm; a colour the config cannot parse falls back to the default theme's.
|
||||
static func ansi(_ theme: TerminalTheme) -> [SwiftTerm.Color] {
|
||||
let fallback = TerminalTheme().ansi
|
||||
return zip(theme.ansi, fallback).map { value, backup in
|
||||
let ns = (NSColor(css: value) ?? NSColor(css: backup) ?? .white).usingColorSpace(.sRGB) ?? .white
|
||||
return SwiftTerm.Color(red: UInt16(ns.redComponent * 65535),
|
||||
green: UInt16(ns.greenComponent * 65535),
|
||||
blue: UInt16(ns.blueComponent * 65535))
|
||||
}
|
||||
}
|
||||
|
||||
/// One theme entry as an AppKit colour, with the default theme's value as fallback.
|
||||
static func color(_ value: String, fallback: String) -> NSColor {
|
||||
NSColor(css: value) ?? NSColor(css: fallback) ?? .white
|
||||
}
|
||||
}
|
||||
|
||||
/// Everything in `config.terminal` that SwiftTerm can express. Line height and letter
|
||||
/// spacing have no SwiftTerm equivalent, so they are not applied.
|
||||
enum TerminalAppearance {
|
||||
static func cursorStyle(_ style: TerminalCursorStyle, blink: Bool) -> CursorStyle {
|
||||
switch (style, blink) {
|
||||
case (.bar, true): .blinkBar
|
||||
case (.bar, false): .steadyBar
|
||||
case (.block, true): .blinkBlock
|
||||
case (.block, false): .steadyBlock
|
||||
case (.underline, true): .blinkUnderline
|
||||
case (.underline, false): .steadyUnderline
|
||||
}
|
||||
}
|
||||
|
||||
/// The configured family, else the bundled IBM Plex Mono at the terminal type size, times the window zoom.
|
||||
static func font(_ settings: HelderConfig.Terminal, scale: Double = 1) -> NSFont {
|
||||
FontRegistry.registerBundledFonts()
|
||||
let size = CGFloat((settings.fontSize ?? Double(Typography.terminal.size)) * scale)
|
||||
if let family = settings.fontFamily?.trimmingCharacters(in: .whitespaces), !family.isEmpty {
|
||||
if let named = NSFont(name: family, size: size) { return named }
|
||||
if let fromFamily = NSFontManager.shared.font(withFamily: family, traits: [], weight: 5, size: size) {
|
||||
return fromFamily
|
||||
}
|
||||
}
|
||||
return NSFont(name: PlexFace.monoRegular.rawValue, size: size) ?? .monospacedSystemFont(ofSize: size, weight: .regular)
|
||||
}
|
||||
|
||||
static func options(_ settings: HelderConfig.Terminal) -> TerminalOptions {
|
||||
TerminalOptions(termName: "xterm-256color",
|
||||
cursorStyle: cursorStyle(settings.cursorStyle, blink: settings.cursorBlink),
|
||||
scrollback: max(0, settings.scrollback))
|
||||
}
|
||||
|
||||
/// Restyles a running terminal; the buffer and the process are untouched.
|
||||
@MainActor
|
||||
static func apply(_ settings: HelderConfig.Terminal, scale: Double = 1, to view: TerminalView) {
|
||||
let theme = settings.theme
|
||||
let base = TerminalTheme()
|
||||
let newFont = font(settings, scale: scale)
|
||||
if view.font.fontName != newFont.fontName || view.font.pointSize != newFont.pointSize {
|
||||
view.font = newFont
|
||||
}
|
||||
view.nativeBackgroundColor = TerminalColors.color(theme.background, fallback: base.background)
|
||||
view.nativeForegroundColor = TerminalColors.color(theme.foreground, fallback: base.foreground)
|
||||
view.caretColor = TerminalColors.color(theme.cursor, fallback: base.cursor)
|
||||
view.caretTextColor = TerminalColors.color(theme.cursorAccent, fallback: base.cursorAccent)
|
||||
view.selectedTextBackgroundColor = TerminalColors.color(theme.selectionBackground, fallback: base.selectionBackground)
|
||||
view.installColors(TerminalColors.ansi(theme))
|
||||
view.useBrightColors = settings.boldIsBright
|
||||
view.optionAsMetaKey = settings.optionIsMeta
|
||||
view.terminal.setCursorStyle(cursorStyle(settings.cursorStyle, blink: settings.cursorBlink))
|
||||
if view.terminal.options.scrollback != settings.scrollback {
|
||||
view.terminal.changeScrollback(max(0, settings.scrollback))
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user