Skip to content

Commit ed395c7

Browse files
committed
Introduced a way of setting the selection color foreground,
as it is possible to end up in cases where it is difficult to see the text. And changed the defaults for the terminal. Fixes: #588
1 parent 849e8a4 commit ed395c7

5 files changed

Lines changed: 72 additions & 7 deletions

File tree

Sources/SwiftTerm/Apple/AppleTerminalView.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,13 @@ extension TerminalView {
749749
if isSelected {
750750
var mutable = attributes
751751
mutable[.selectionBackgroundColor] = selectedTextBackgroundColor
752+
mutable[.foregroundColor] = selectedTextForegroundColor
753+
if mutable[.underlineColor] != nil {
754+
mutable[.underlineColor] = selectedTextForegroundColor
755+
}
756+
if mutable[.strikethroughColor] != nil {
757+
mutable[.strikethroughColor] = selectedTextForegroundColor
758+
}
752759
currentAttributes = mutable
753760
} else {
754761
currentAttributes = attributes

Sources/SwiftTerm/Mac/MacTerminalView.swift

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -727,14 +727,29 @@ open class TerminalView: NSView, NSTextInputClient, NSUserInterfaceValidations,
727727
set { caretView.caretTextColor = newValue }
728728
}
729729

730-
var _selectedTextBackgroundColor = NSColor.selectedTextBackgroundColor
731-
/// The color used to render the selection
730+
var _selectedTextBackgroundColor = NSColor(srgbRed: 0, green: 166.0 / 255.0, blue: 178.0 / 255.0, alpha: 1.0)
731+
/// The background color used to render the selection.
732732
public var selectedTextBackgroundColor: NSColor {
733733
get {
734734
return _selectedTextBackgroundColor
735735
}
736736
set {
737737
_selectedTextBackgroundColor = newValue
738+
terminal.updateFullScreen()
739+
queuePendingDisplay()
740+
}
741+
}
742+
743+
var _selectedTextForegroundColor = NSColor.black
744+
/// The foreground color used to render selected text.
745+
public var selectedTextForegroundColor: NSColor {
746+
get {
747+
return _selectedTextForegroundColor
748+
}
749+
set {
750+
_selectedTextForegroundColor = newValue
751+
terminal.updateFullScreen()
752+
queuePendingDisplay()
738753
}
739754
}
740755

Sources/SwiftTerm/iOS/iOSTerminalView.swift

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,14 +1297,29 @@ open class TerminalView: UIScrollView, UITextInputTraits, UIKeyInput, UIScrollVi
12971297
}
12981298
}
12991299

1300-
var _selectedTextBackgroundColor = UIColor (red: 204.0/255.0, green: 221.0/255.0, blue: 237.0/255.0, alpha: 1.0)
1301-
/// The color used to render the selection
1300+
var _selectedTextBackgroundColor = UIColor(red: 0, green: 166.0 / 255.0, blue: 178.0 / 255.0, alpha: 1.0)
1301+
/// The background color used to render the selection.
13021302
public var selectedTextBackgroundColor: UIColor {
13031303
get {
13041304
return _selectedTextBackgroundColor
13051305
}
13061306
set {
13071307
_selectedTextBackgroundColor = newValue
1308+
terminal.updateFullScreen()
1309+
queuePendingDisplay()
1310+
}
1311+
}
1312+
1313+
var _selectedTextForegroundColor = UIColor.black
1314+
/// The foreground color used to render selected text.
1315+
public var selectedTextForegroundColor: UIColor {
1316+
get {
1317+
return _selectedTextForegroundColor
1318+
}
1319+
set {
1320+
_selectedTextForegroundColor = newValue
1321+
terminal.updateFullScreen()
1322+
queuePendingDisplay()
13081323
}
13091324
}
13101325

TerminalApp/MacTerminal/ViewController.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ class ViewController: NSViewController, LocalProcessTerminalViewDelegate, NSUser
123123
print("METAL DISABLED: \(error)")
124124
}
125125
let defaultForegroundColor = NSColor(
126-
calibratedRed: CGFloat(0xcc) / 255.0,
127-
green: CGFloat(0xcc) / 255.0,
128-
blue: CGFloat(0xcc) / 255.0,
126+
calibratedRed: 1.0,
127+
green: 1.0,
128+
blue: 1.0,
129129
alpha: 1.0
130130
)
131131
let defaultBackgroundColor = NSColor(

Tests/SwiftTermTests/SelectionTests.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
import Foundation
99
import Testing
1010

11+
#if os(macOS)
12+
import AppKit
13+
#endif
14+
1115
@testable import SwiftTerm
1216

1317
final class SelectionTests: TerminalDelegate {
@@ -107,6 +111,30 @@ final class SelectionTests: TerminalDelegate {
107111
#expect(view.terminal.cols == originalCols)
108112
#expect(view.terminal.rows == originalRows)
109113
}
114+
115+
@Test func testSelectionColorsOverrideCellAndDecorationColors() {
116+
let view = TerminalView(frame: CGRect(origin: .zero, size: .init(width: 320, height: 160)))
117+
let selectionBackground = NSColor(srgbRed: 0, green: 166.0 / 255.0, blue: 178.0 / 255.0, alpha: 1.0)
118+
let selectionForeground = NSColor.black
119+
120+
#expect(view.selectedTextBackgroundColor.isEqual(selectionBackground))
121+
#expect(view.selectedTextForegroundColor.isEqual(selectionForeground))
122+
123+
view.terminal.feed(text: "\u{001B}[31;44;4;9mX")
124+
view.selection.setSelection(start: Position(col: 0, row: 0), end: Position(col: 1, row: 0))
125+
126+
let renderedLine = view.buildAttributedString(
127+
row: 0,
128+
line: view.terminal.displayBuffer.lines[0],
129+
cols: view.terminal.cols
130+
)
131+
let attributes = renderedLine.segments[0].attributedString.attributes(at: 0, effectiveRange: nil)
132+
133+
#expect((attributes[.selectionBackgroundColor] as? NSColor)?.isEqual(selectionBackground) == true)
134+
#expect((attributes[.foregroundColor] as? NSColor)?.isEqual(selectionForeground) == true)
135+
#expect((attributes[.underlineColor] as? NSColor)?.isEqual(selectionForeground) == true)
136+
#expect((attributes[.strikethroughColor] as? NSColor)?.isEqual(selectionForeground) == true)
137+
}
110138
#endif
111139

112140
// MARK: - Selection Tests Ported from Ghostty

0 commit comments

Comments
 (0)