Skip to content

Commit 6af3a3c

Browse files
committed
[feat]: better error description & ...
1 parent 92143ea commit 6af3a3c

File tree

4 files changed

+77
-50
lines changed

4 files changed

+77
-50
lines changed

Sources/ScriptToolkit/NSColorExtension.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Foundation
99
import AppKit
1010

1111
public extension NSColor {
12-
convenience init(hexString: String) {
12+
convenience init?(hexString: String) {
1313
let hexString = hexString.trimmingCharacters(in: .whitespacesAndNewlines)
1414
let scanner = Scanner(string: hexString)
1515

@@ -18,7 +18,7 @@ public extension NSColor {
1818
}
1919

2020
var color: UInt32 = 0
21-
scanner.scanHexInt32(&color)
21+
guard scanner.scanHexInt32(&color) else { return nil }
2222

2323
let mask = 0x000000FF
2424
let r = Int(color >> 16) & mask

Sources/ScriptToolkit/NSImageExtension.swift

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public extension NSImage {
9393
return finalResult
9494
}
9595

96-
func image(withText text: String, attributes: [NSAttributedString.Key: Any]) -> NSImage {
96+
func image(withText text: String, attributes: [NSAttributedString.Key: Any], position: CGFloat) -> NSImage {
9797
let image = self
9898
let text = text as NSString
9999
let options: NSString.DrawingOptions = [.usesLineFragmentOrigin, .usesFontLeading]
@@ -102,7 +102,7 @@ public extension NSImage {
102102

103103
let x = (image.size.width - textSize.width) / 2
104104
let y = (image.size.height - textSize.height) / 2
105-
let point = NSMakePoint(x, y * 0.2)
105+
let point = NSMakePoint(x, y * position)
106106

107107
image.lockFocus()
108108
text.draw(at: point, withAttributes: attributes)
@@ -111,25 +111,29 @@ public extension NSImage {
111111
return image
112112
}
113113

114-
func annotate(text: String, size: CGFloat, fill: NSColor, stroke: NSColor) -> NSImage {
114+
func annotate(text: String, font: String, size: CGFloat, position: CGFloat, fill: NSColor, stroke: NSColor, strokeWidth: CGFloat) throws -> NSImage {
115+
116+
guard let titleFont = NSFont(name: font, size: size) else { throw ScriptError.argumentError(message: "Unable to find font \(font)") }
115117

116118
// Solid color text
117119
let fillText = image(
118120
withText: text,
119121
attributes: [
120122
.foregroundColor: fill,
121-
.font: NSFont(name: "Impact", size: size)!
122-
])
123+
.font: titleFont
124+
],
125+
position: position)
123126

124127
// Add strokes
125128
return fillText.image(
126129
withText: text,
127130
attributes: [
128-
.foregroundColor: fill,
131+
.foregroundColor: NSColor.clear,
129132
.strokeColor: stroke,
130-
.strokeWidth: 1,
131-
.font: NSFont(name: "Impact", size: size)!
132-
])
133+
.strokeWidth: strokeWidth * size,
134+
.font: titleFont
135+
],
136+
position: position)
133137
}
134138
}
135139

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//
2+
// ScriptError.swift
3+
// ScriptToolkit
4+
//
5+
// Created by Dan on 06.03.2019.
6+
//
7+
8+
import Foundation
9+
import Moderator
10+
11+
public protocol PrintableError {
12+
var errorDescription: String { get }
13+
}
14+
15+
public enum ScriptError: Error {
16+
case generalError(message: String)
17+
case fileExists(message: String)
18+
case fileNotFound(message: String)
19+
case folderExists(message: String)
20+
case folderNotFound(message: String)
21+
case argumentError(message: String)
22+
case moreInfoNeeded(message: String)
23+
}
24+
25+
extension ScriptError: PrintableError {
26+
public var errorDescription: String {
27+
let prefix = "💥 error: "
28+
var errorDescription = ""
29+
30+
switch self {
31+
case let .generalError(message):
32+
errorDescription = message
33+
34+
case let .fileExists(message):
35+
errorDescription = "file exists: \(message)"
36+
37+
case let .fileNotFound(message):
38+
errorDescription = "file not found: \(message)"
39+
40+
case let .folderExists(message):
41+
errorDescription = "folder exists: \(message)"
42+
43+
case let .folderNotFound(message):
44+
errorDescription = "folder not found: \(message)"
45+
46+
case let .argumentError(message):
47+
errorDescription = "invalid argument: \(message)"
48+
49+
case let .moreInfoNeeded(message):
50+
errorDescription = "more info needed: \(message)"
51+
}
52+
53+
return prefix + errorDescription
54+
}
55+
}
56+
57+
58+
extension ArgumentError: PrintableError {
59+
public var errorDescription: String {
60+
return "💥 error: \(errormessage)"
61+
}
62+
}

Sources/ScriptToolkit/ScriptToolkit.swift

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,6 @@ import Foundation
99
import Files
1010
import SwiftShell
1111

12-
public enum ScriptError: Error, CustomStringConvertible {
13-
case generalError(message: String)
14-
case fileExists(message: String)
15-
case fileNotFound(message: String)
16-
case folderExists(message: String)
17-
case folderNotFound(message: String)
18-
case argumentError(message: String)
19-
case moreInfoNeeded(message: String)
20-
21-
public var description: String {
22-
let prefix = "💥 error: "
23-
var errorDescription = ""
24-
25-
switch self {
26-
case let .generalError(message):
27-
errorDescription = message
28-
29-
case let .fileExists(message):
30-
errorDescription = "file exists: \(message)"
31-
32-
case let .fileNotFound(message):
33-
errorDescription = "file not found: \(message)"
34-
35-
case let .folderExists(message):
36-
errorDescription = "folder exists: \(message)"
37-
38-
case let .folderNotFound(message):
39-
errorDescription = "folder not found: \(message)"
40-
41-
case let .argumentError(message):
42-
errorDescription = "invalid argument: \(message)"
43-
44-
case let .moreInfoNeeded(message):
45-
errorDescription = "more info needed: \(message)"
46-
}
47-
48-
return prefix + errorDescription
49-
}
50-
}
5112

5213
public struct ScriptToolkit {
5314

0 commit comments

Comments
 (0)