Skip to content

Commit

Permalink
add styledString
Browse files Browse the repository at this point in the history
  • Loading branch information
luoxiu committed Nov 15, 2020
1 parent 9896f3c commit 7013b72
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 44 deletions.
54 changes: 25 additions & 29 deletions Sources/Chalk/TerminalString.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
//
// File.swift
//
//
// Created by jinxiangqiu on 18/3/2020.
//

import Foundation

public struct TerminalString {
Expand All @@ -22,34 +15,37 @@ public struct TerminalString {
public var string: String {
strings.map { $0.0 }.joined()
}

public var styledString: String {
strings.map {
var openCodes: [String] = []
var closeCodes: [String] = []
if let modifiers = $0.1.modifiers {
openCodes.append(contentsOf: modifiers.map { $0.openCode } )
closeCodes.append(contentsOf: modifiers.map { $0.closeCode } )
}
if let color = $0.1.fgColor {
openCodes.append(color.fgOpenCode)
closeCodes.append(color.fgCloseCode)
}
if let color = $0.1.bgColor {
openCodes.append(color.bgOpenCode)
closeCodes.append(color.bgCloseCode)
}
return openCodes.joined() + $0.0 + closeCodes.joined()
}.joined()
}
}

extension TerminalString: CustomStringConvertible {

public var description: String {
strings.map {
switch TerminalSupportedColor.current {
case .none:
return $0.0
default:
var openCodes: [String] = []
var closeCodes: [String] = []
if let modifiers = $0.1.modifiers {
openCodes.append(contentsOf: modifiers.map { $0.openCode } )
closeCodes.append(contentsOf: modifiers.map { $0.closeCode } )
}
if let color = $0.1.fgColor {
openCodes.append(color.fgOpenCode)
closeCodes.append(color.fgCloseCode)
}
if let color = $0.1.bgColor {
openCodes.append(color.bgOpenCode)
closeCodes.append(color.bgCloseCode)
}
return openCodes.joined() + $0.0 + closeCodes.joined()
}
switch TerminalSupportedColor.current {
case .none:
return string
default:
return styledString
}
.joined()
}
}

Expand Down
7 changes: 0 additions & 7 deletions Sources/Chalk/TerminalStringConvertible.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
//
// File.swift
//
//
// Created by jinxiangqiu on 19/3/2020.
//

public protocol TerminalStringConvertible {

var terminalString: TerminalString { get }
Expand Down
7 changes: 0 additions & 7 deletions Sources/chalk-app/main.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
//
// File.swift
//
//
// Created by jinxiangqiu on 15/3/2020.
//

import Foundation
import Chalk

Expand Down
3 changes: 2 additions & 1 deletion Tests/ChalkTests/ChalkTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ final class ChalkTests: XCTestCase {
XCTAssertEqual(chalk.underline.red.bgGreen.on("foo").description, "\u{001B}[4m\u{001B}[31m\u{001B}[42mfoo\u{001B}[24m\u{001B}[39m\u{001B}[49m")

let nest = ck.red.on("a" + ck.yellow.on("b" + ck.green.on("c") + "b") + "c")
XCTAssertEqual(nest.description, "\u{001B}[31ma\u{001B}[39m\u{001B}[33mb\u{001B}[39m\u{001B}[32mc\u{001B}[39m\u{001B}[33mb\u{001B}[39m\u{001B}[31mc\u{001B}[39m")
XCTAssertEqual(nest.string, "abcbc")
XCTAssertEqual(nest.styledString, "\u{001B}[31ma\u{001B}[39m\u{001B}[33mb\u{001B}[39m\u{001B}[32mc\u{001B}[39m\u{001B}[33mb\u{001B}[39m\u{001B}[31mc\u{001B}[39m")
}
}

0 comments on commit 7013b72

Please sign in to comment.