Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update logic to support new bright color flag #338

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions Sources/SwiftTerm/Apple/AppleTerminalView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ extension TerminalView {
return CellDimension(width: max (1, cellWidth), height: max (min (cellHeight, 8192), 1))
}

func mapColor (color: Attribute.Color, isFg: Bool, isBold: Bool) -> TTColor
func mapColor (color: Attribute.Color, isFg: Bool, isBold: Bool, useBrightColors: Bool = true) -> TTColor
{
switch color {
case .defaultColor:
Expand All @@ -170,8 +170,14 @@ extension TerminalView {
return nativeBackgroundColor.inverseColor()
}
case .ansi256(let ansi):
// Ansi 8 to 16 are high-intensity colors, they are already treated as bold
let midx = ansi < 7 ? (Int (ansi) + (isBold ? 8 : 0)) : Int (ansi)
var midx: Int
// if high - bright colors are enabled we will represent bold text by using more intense colors
// otherwise we will reduce colors but use bold fonts
if useBrightColors {
midx = ansi < 7 ? (Int (ansi) + (isBold ? 8 : 0)) : Int (ansi)
} else {
midx = ansi > 7 ? (Int (ansi) - 8) : Int(ansi)
}
if let c = colors [midx] {
return c
}
Expand Down Expand Up @@ -325,9 +331,15 @@ extension TerminalView {
return result
}

var useBoldForBrightColor: Bool = false
// if high - bright colors are disabled in settings we will use bold font instead
if case .ansi256(let code) = fg, code > 7, !useBrightColors {
useBoldForBrightColor = true
}
var tf: TTFont
let isBold = flags.contains(.bold)
if isBold {

if isBold || useBoldForBrightColor {
if flags.contains (.italic) {
tf = fontSet.boldItalic
} else {
Expand All @@ -339,7 +351,7 @@ extension TerminalView {
tf = fontSet.normal
}

let fgColor = mapColor (color: fg, isFg: true, isBold: isBold)
let fgColor = mapColor (color: fg, isFg: true, isBold: isBold, useBrightColors: useBrightColors)
var nsattr: [NSAttributedString.Key:Any] = [
.font: tf,
.foregroundColor: fgColor,
Expand All @@ -353,6 +365,7 @@ extension TerminalView {
nsattr [.strikethroughColor] = fgColor
nsattr [.strikethroughStyle] = NSUnderlineStyle.single.rawValue
}

if withUrl {
nsattr [.underlineStyle] = NSUnderlineStyle.single.rawValue | NSUnderlineStyle.patternDash.rawValue
nsattr [.underlineColor] = fgColor
Expand Down
3 changes: 3 additions & 0 deletions Sources/SwiftTerm/Mac/MacTerminalView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ open class TerminalView: NSView, NSTextInputClient, NSUserInterfaceValidations,
}
}

/// Controls weather to use high ansi colors, if false terminal will use bold text instead of high ansi colors
public var useBrightColors: Bool = true

/// Controls the color for the caret
public var caretColor: NSColor {
get { caretView.caretColor }
Expand Down
3 changes: 3 additions & 0 deletions Sources/SwiftTerm/iOS/iOSTerminalView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,9 @@ open class TerminalView: UIScrollView, UITextInputTraits, UIKeyInput, UIScrollVi
get { caretView?.caretTextColor }
set { caretView?.caretTextColor = newValue }
}

/// Controls weather to use high ansi colors, if false terminal will use bold text instead of high ansi colors
public var useBrightColors: Bool = true

var _selectedTextBackgroundColor = UIColor (red: 204.0/255.0, green: 221.0/255.0, blue: 237.0/255.0, alpha: 1.0)
/// The color used to render the selection
Expand Down
Loading