Skip to content

Commit

Permalink
Merge pull request #338 from iOSappssolutions/main
Browse files Browse the repository at this point in the history
Update logic to support new bright color flag
  • Loading branch information
migueldeicaza authored Mar 12, 2024
2 parents c87c2a1 + 3be1b33 commit 93b9289
Show file tree
Hide file tree
Showing 3 changed files with 24 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
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

0 comments on commit 93b9289

Please sign in to comment.