Skip to content

Commit

Permalink
Update logic to support new bright color flag
Browse files Browse the repository at this point in the history
We can toggle weather terminal should use bright colors, so it will either use bright colors instead of bold fonts
or if they are turned off it will use bold fonts instead of bright colors
  • Loading branch information
iOSappssolutions committed Mar 12, 2024
1 parent c87c2a1 commit e266d39
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
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
2 changes: 2 additions & 0 deletions Sources/SwiftTerm/Mac/MacTerminalView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ open class TerminalView: NSView, NSTextInputClient, NSUserInterfaceValidations,
}
}

public var useBrightColors: Bool = true

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

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

0 comments on commit e266d39

Please sign in to comment.