Skip to content

Commit b563270

Browse files
committed
Extract Colorizer from Swiftline
0 parents  commit b563270

14 files changed

+651
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
/*.xcodeproj

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
language: objective-c
2+
osx_image: xcode10
3+
script:
4+
- swift test

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2015 Omar Abdelhafith <o.arrabi@me.com>
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

Package.resolved

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// swift-tools-version:4.2
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "Colorizer",
8+
products: [
9+
// Products define the executables and libraries produced by a package, and make them visible to other packages.
10+
.library(
11+
name: "Colorizer",
12+
targets: ["Colorizer"]),
13+
],
14+
dependencies: [
15+
// Dependencies declare other packages that this package depends on.
16+
.package(url: "https://github.com/Quick/Quick.git", from: "1.3.1"),
17+
.package(url: "https://github.com/Quick/Nimble.git", from: "7.3.0"),
18+
],
19+
targets: [
20+
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
21+
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
22+
.target(
23+
name: "Colorizer",
24+
dependencies: []),
25+
.testTarget(
26+
name: "ColorizerTests",
27+
dependencies: ["Colorizer", "Nimble", "Quick"]),
28+
]
29+
)

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Colorizer
2+
3+
Colorizer helps adding colors to strings written to the terminal.
4+
5+
This package was extracted from the [Swiftline](https://github.com/nsomar/Swiftline) package.
6+
7+
## Usage
8+
9+
Colorizer helps styling the strings before printing them to the terminal. You
10+
can change the text color, the text background and the text style. Colorizer
11+
works by extending `String` struct to add styling to it.
12+
13+
To change the text color, use either `string.f` or `string.foreground`:
14+
15+
```swift
16+
print("Red String".f.Red)
17+
print("Blue String".foreground.Blue)
18+
```
19+
20+
To change the text background color, use either `string.b` or `string.background`:
21+
22+
```swift
23+
print("I have a white background".b.White)
24+
print("My background color is green".background.Green)
25+
```
26+
27+
To change the text background style, use either `string.s` or `string.style`:
28+
29+
```swift
30+
print("I am a bold string".s.Bold)
31+
print("I have an underline".style.Underline)
32+
```
33+
34+
You can compose foreground, background, and style:
35+
```swift
36+
print("I am an underlined red on white string".s.Underline.f.Red.b.White)
37+
```
38+
39+
## Installation
40+
41+
### Swift Package Manager
42+
Add `Colorizer` as dependency in your `Package.swift`
43+
44+
```
45+
import PackageDescription
46+
47+
let package = Package(name: "YourPackage",
48+
dependencies: [
49+
.package(url: "https://github.com/getGuaka/Colorizer.git", from: "0.0.0"),
50+
]
51+
)
52+
```
53+
54+
## License
55+
56+
MIT

Sources/Colorizer/Colorizer.swift

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//
2+
// Colorizer.swift
3+
// Colorizer
4+
//
5+
// Created by Omar Abdelhafith on 30/10/2015.
6+
// Copyright © 2015 Omar Abdelhafith. All rights reserved.
7+
//
8+
9+
10+
extension String {
11+
12+
/// Access the methods to change the foreground color
13+
public var f: StringForegroundColorizer {
14+
return foreground
15+
}
16+
17+
/// Access the methods to change the foreground color
18+
public var foreground: StringForegroundColorizer {
19+
return StringForegroundColorizer(string: self)
20+
}
21+
22+
/// Access the methods to change the background color
23+
public var b: StringBackgroundColorizer {
24+
return background
25+
}
26+
27+
/// Access the methods to change the background color
28+
public var background: StringBackgroundColorizer {
29+
return StringBackgroundColorizer(string: self)
30+
}
31+
32+
/// Access the methods to change the text style
33+
public var s: StringStyleColorizer {
34+
return style
35+
}
36+
37+
/// Access the methods to change the text style
38+
public var style: StringStyleColorizer {
39+
return StringStyleColorizer(string: self)
40+
}
41+
42+
}
43+
44+
// MARK- Internal
45+
46+
class Colorizer: CustomStringConvertible {
47+
48+
let color: StringStyle
49+
let string: String
50+
51+
init(string: String, color: StringStyle) {
52+
self.string = string
53+
self.color = color
54+
}
55+
56+
var description: String {
57+
return color.colorize(string: string)
58+
}
59+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// StringBackgroundColorizer.swift
3+
// StringBackgroundColorizer
4+
//
5+
// Created by Omar Abdelhafith on 31/10/2015.
6+
// Copyright © 2015 Omar Abdelhafith. All rights reserved.
7+
//
8+
9+
10+
extension String {
11+
12+
public struct StringBackgroundColorizer {
13+
14+
let string: String
15+
16+
public var Black: String {
17+
return Colorizer(string: string, color: BackgroundColor.black).description
18+
}
19+
20+
public var Red: String {
21+
return Colorizer(string: string, color: BackgroundColor.red).description
22+
}
23+
24+
public var Green: String {
25+
return Colorizer(string: string, color: BackgroundColor.green).description
26+
}
27+
28+
public var Yellow: String {
29+
return Colorizer(string: string, color: BackgroundColor.yellow).description
30+
}
31+
32+
public var Blue: String {
33+
return Colorizer(string: string, color: BackgroundColor.blue).description
34+
}
35+
36+
public var Magenta: String {
37+
return Colorizer(string: string, color: BackgroundColor.magenta).description
38+
}
39+
40+
public var Cyan: String {
41+
return Colorizer(string: string, color: BackgroundColor.cyan).description
42+
}
43+
44+
public var White: String {
45+
return Colorizer(string: string, color: BackgroundColor.white).description
46+
}
47+
}
48+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// StringForegroundColorizer.swift
3+
// StringForegroundColorizer
4+
//
5+
// Created by Omar Abdelhafith on 31/10/2015.
6+
// Copyright © 2015 Omar Abdelhafith. All rights reserved.
7+
//
8+
9+
10+
extension String {
11+
12+
public struct StringForegroundColorizer {
13+
14+
let string: String
15+
16+
public var Black: String {
17+
return Colorizer(string: string, color: ForegroundColor.black).description
18+
}
19+
20+
public var Red: String {
21+
return Colorizer(string: string, color: ForegroundColor.red).description
22+
}
23+
24+
public var Green: String {
25+
return Colorizer(string: string, color: ForegroundColor.green).description
26+
}
27+
28+
public var Yellow: String {
29+
return Colorizer(string: string, color: ForegroundColor.yellow).description
30+
}
31+
32+
public var Blue: String {
33+
return Colorizer(string: string, color: ForegroundColor.blue).description
34+
}
35+
36+
public var Magenta: String {
37+
return Colorizer(string: string, color: ForegroundColor.magenta).description
38+
}
39+
40+
public var Cyan: String {
41+
return Colorizer(string: string, color: ForegroundColor.cyan).description
42+
}
43+
44+
public var White: String {
45+
return Colorizer(string: string, color: ForegroundColor.white).description
46+
}
47+
}
48+
49+
}

0 commit comments

Comments
 (0)