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

#1 specify color for badge and text #9

Merged
merged 4 commits into from
Jun 8, 2020
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
28 changes: 15 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,17 @@ Usage: badgy small <char> <icon> [options]
Add small square label to app icon

Options:
-h, --help Show help information
-p, --position <value> Position on which to place the badge
-r, --replace Indicates Badgy should replace the input icon
Only works if input is of format .appiconasset
-v, --verbose Log tech details for nerds
-c, --color <value> Specify badge color with a hexadecimal color code
-h, --help Show help information
-p, --position <value> Position on which to place the badge
-r, --replace Indicates Badgy should replace the input icon
-t, --tint-color <value> Specify badge text/tint color with a hexadecimal color code
-v, --verbose Log tech details for nerds
```

#### Example
```sh
badgy small A ~/MyIcon.png --position topRight
badgy small A ~/MyIcon.png --position topRight --tint-color '#000000'
```
<p align="center">
<img src="Assets/a_small_sample.png" title="badgy small">
Expand All @@ -110,17 +111,18 @@ Usage: badgy long <labelText> <icon> [options]
Add rectangular label to app icon

Options:
-a, --angle <value> Rotation angle of the badge
-h, --help Show help information
-p, --position <value> Position on which to place the badge
-r, --replace Indicates Badgy should replace the input icon
Only works if input is of format .appiconasset
-v, --verbose Log tech details for nerds
-a, --angle <value> Rotation angle of the badge
-c, --color <value> Specify badge color with a hexadecimal color code
-h, --help Show help information
-p, --position <value> Position on which to place the badge
-r, --replace Indicates Badgy should replace the input icon
-t, --tint-color <value> Specify badge text/tint color with a hexadecimal color code
-v, --verbose Log tech details for nerds
```

#### Example
```sh
badgy long BETA ~/MyIcon.png --angle 15 --position bottom
badgy long BETA ~/MyIcon.png --angle 15 --position bottom --tint-color '#000000'
```
<p align="center">
<img src="Assets/beta_long_sample.png" title="badgy long">
Expand Down
14 changes: 13 additions & 1 deletion Sources/Badgy/Commands/Long.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ final class Long: DependencyManager, Command, IconSetDelegate {
)
var position: String?

@Key("-c", "--color",
description: "Specify badge color with a hexadecimal color code",
validation: [Validation.hexColorCode()]
)
var color: String?

@Key("-t", "--tint-color",
description: "Specify badge text/tint color with a hexadecimal color code",
validation: [Validation.hexColorCode()]
)
var tintColor: String?

let logger = Logger.shared
let factory = Factory()

Expand Down Expand Up @@ -98,7 +110,7 @@ final class Long: DependencyManager, Command, IconSetDelegate {
private func process(baseIcon: String) throws {
let folder = Path("Badgy")

try factory.makeBadge(with: labelText, angle: angleInt, inFolder: folder, completion: { (result) in
try factory.makeBadge(with: labelText, colorHexCode: color, tintColorHexCode: tintColor, angle: angleInt, inFolder: folder, completion: { (result) in
switch result {
case .success(_):
try self.factory.appendBadge(to: baseIcon,
Expand Down
15 changes: 14 additions & 1 deletion Sources/Badgy/Commands/Small.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ final class Small: DependencyManager, Command, IconSetDelegate {
Position.center.rawValue)]
)
var position: String?

@Key("-c", "--color",
description: "Specify badge color with a hexadecimal color code",
validation: [Validation.hexColorCode()]
)
var color: String?

@Key("-t", "--tint-color",
description: "Specify badge text/tint color with a hexadecimal color code",
validation: [Validation.hexColorCode()]
)
var tintColor: String?

let logger = Logger.shared
let factory = Factory()

Expand Down Expand Up @@ -82,7 +95,7 @@ final class Small: DependencyManager, Command, IconSetDelegate {

private func process(baseIcon: String) throws {
let folder = Path("Badgy")
factory.makeSmall(with: char, inFolder: folder, completion: { (result) in
factory.makeSmall(with: char, colorHexCode: color, tintColorHexCode: tintColor, inFolder: folder, completion: { (result) in
switch result {
case .success(_):
try self.factory.appendBadge(to: baseIcon,
Expand Down
4 changes: 3 additions & 1 deletion Sources/Badgy/Helpers/Factory+Small.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import PathKit
extension Factory {
func makeSmall(with label: String,
colorHexCode: String? = nil,
tintColorHexCode: String? = nil,
inFolder folder: Path,
completion: @escaping BadgeProductionResponse) {

let color = colorHexCode ?? colors.randomElement()!
let tintColor = tintColorHexCode ?? "white"

do {
let folderBase = folder.absolute().description
Expand All @@ -39,7 +41,7 @@ extension Factory {
"-gravity", "Center",
"-weight","700",
"-pointsize", "180",
"-fill", "white",
"-fill", "\(tintColor)",
"caption:\(label)",
"\(folderBase)/bottom.png"
)
Expand Down
4 changes: 3 additions & 1 deletion Sources/Badgy/Helpers/Factory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ struct Factory {

func makeBadge(with label: String,
colorHexCode: String? = nil,
tintColorHexCode: String? = nil,
angle: Int? = nil,
inFolder folder: Path,
completion: @escaping BadgeProductionResponse) throws {

let color = colorHexCode ?? colors.randomElement()!
let tintColor = tintColorHexCode ?? "white"

do {
let folderBase = folder.absolute().description
Expand All @@ -44,7 +46,7 @@ struct Factory {
"-gravity", "Center",
"-weight","700",
"-pointsize", "180",
"-fill", "white",
"-fill", "\(tintColor)",
"caption:\(label)",
"\(folderBase)/bottom.png"
)
Expand Down
23 changes: 23 additions & 0 deletions Sources/Badgy/Helpers/Foundation+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// Badgy
//
// Created by Arthur Alves on 30/05/2020.
//

import Foundation

/// Thanks to [How to use regular expressions in swift | Paul Hudson ](https://www.hackingwithswift.com/articles/108/how-to-use-regular-expressions-in-swift)
internal extension NSRegularExpression {
convenience init(_ pattern: String) {
do {
try self.init(pattern: pattern)
} catch {
preconditionFailure("Illegal regular expression: \(pattern).")
}
}

func matches(_ string: String) -> Bool {
let range = NSRange(location: 0, length: string.utf16.count)
return firstMatch(in: string, options: [], range: range) != nil
}
}
38 changes: 38 additions & 0 deletions Sources/Badgy/Helpers/Validation+HexColor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// Badgy
//
// Created by Arthur Alves on 30/05/2020.
//

import Foundation
import SwiftCLI

extension Validation where T == String {
static func hexColorCode() -> Self {
let message = "Specify valid hex color code in a case insensitive format '#rrbbgg' | '#rrbbggaa'"

return Validation<String>.custom(message) { (input) in
guard NSRegularExpression.hexColorCode.matches(input) else {
Logger.shared.logError("❌ ", item: "Input color '\(input)' doesn't have a valid format")
return false
}

return true
}
}
}

private extension NSRegularExpression {
/// `^` asserts position at start of a line
/// `#` matches the character # literally
///
/// `(:?[0-9a-fA-F]{2})`
/// - `(:?)` denotes a non-capturing group
/// - `[0-9a-fA-F]` match a single character present in the list below
/// - `{2}` - matches exactly 2 times
///
/// `{3,4}` - matches between 3 and 4 times, as many times as possible
///
/// Additional explanation at [regex101](https://regex101.com/)
static let hexColorCode = NSRegularExpression("^#(?:[0-9a-fA-F]{2}){3,4}$")
}