Skip to content

Commit

Permalink
Add style argument and format (#46)
Browse files Browse the repository at this point in the history
* use latest swift syntax version

* Fix compile error in CsvBuilder

* Refactor.

* Add `exportType` option to CLI

* Add `style` argument when generating output. Apply format.

* Generate docs

---------

Co-authored-by: infinitepower18 <[email protected]>
  • Loading branch information
fummicc1 and infinitepower18 authored Dec 3, 2023
1 parent cf8cbf9 commit 74b0055
Show file tree
Hide file tree
Showing 226 changed files with 1,578 additions and 775 deletions.
342 changes: 263 additions & 79 deletions Sources/Csv2Img/Csv.swift

Large diffs are not rendered by default.

50 changes: 38 additions & 12 deletions Sources/Csv2Img/CsvColumn.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ extension Csv {
public var name: Name
public var style: Style

public init(name: Name, style: Style) {
public init(
name: Name,
style: Style
) {
self.name = name
self.style = style
}
Expand All @@ -43,35 +46,58 @@ extension Csv.Column {
/// `applyOnlyColumn` determines whether this style affects both `Column` and `Row` or not.
/// Default value of `applyOnlyColumn` is false, which means ``Style`` is also applied to ``Row``.
public var applyOnlyColumn: Bool

public init(color: CGColor, applyOnlyColumn: Bool = false) {

public init(
color: CGColor,
applyOnlyColumn: Bool = false
) {
self.color = color
self.applyOnlyColumn = applyOnlyColumn
}

public static func random(count: Int) -> [Style] {

public static func random(
count: Int
) -> [Style] {
var styles: [Style] = []
let saturation = 80.0 / 100.0
let value = 80.0 / 100.0
var hue: Double = 0.5
for _ in 0..<count {
let color: Color
if let random = (0..<360).randomElement() {
hue = Double(random) / 360
color = Color(hue: hue, saturation: saturation, brightness: value, alpha: 1)
if let random = (
0..<360
).randomElement() {
hue = Double(
random
) / 360
color = Color(
hue: hue,
saturation: saturation,
brightness: value,
alpha: 1
)
} else {
color = Color(hue: 1 - hue, saturation: saturation, brightness: value, alpha: 1)
color = Color(
hue: 1 - hue,
saturation: saturation,
brightness: value,
alpha: 1
)
}
let style = Style(
color: color.cgColor
)
styles.append(style)
styles.append(
style
)
}
return styles
}

public static func random() -> Style {
random(count: 1).first!
random(
count: 1
).first!
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion Sources/Csv2Img/CsvColumnStyle+Ex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ extension Csv.Column.Style {
if applyOnlyColumn {
return normalColor()
}
return Color(cgColor: color)
return Color(
cgColor: color
)
}
}
23 changes: 18 additions & 5 deletions Sources/Csv2Img/CsvError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,30 @@ extension Csv {
/// `Error` related with Csv implmentation.
public enum Error: Swift.Error {
/// Specified network url is invalid or failed to download csv data.
case invalidDownloadResource(url: String, data: Data)
case invalidDownloadResource(
url: String,
data: Data
)
/// Specified local url is invalid (file may not exist. using incorrect `String.Encoding` Type).
case invalidLocalResource(url: String, data: Data, encoding: String.Encoding)
case invalidLocalResource(
url: String,
data: Data,
encoding: String.Encoding
)
/// If file is not accessible due to security issue.
case cannotAccessFile(url: String)
case cannotAccessFile(
url: String
)
/// given `exportType` is invalid.
case invalidExportType(ExportType)
case invalidExportType(
ExportType
)
/// Both columns and rows are empty
case emptyData
/// Csv denied execution because it is generating another contents.
case workInProgress
case underlying(Swift.Error?)
case underlying(
Swift.Error?
)
}
}
5 changes: 4 additions & 1 deletion Sources/Csv2Img/CsvRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ extension Csv {
///
public struct Row {

public init(index: Int, values: [String]) {
public init(
index: Int,
values: [String]
) {
self.index = index
self.values = values
}
Expand Down
13 changes: 10 additions & 3 deletions Sources/Csv2Img/Image+Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@ import Foundation
import AppKit
extension CGImage {
public func convertToData() -> Data? {
let rep = NSBitmapImageRep(cgImage: self)
return rep.representation(using: .png, properties: [:])
let rep = NSBitmapImageRep(
cgImage: self
)
return rep.representation(
using: .png,
properties: [:]
)
}
}
#elseif canImport(UIKit)
import UIKit
extension CGImage {
public func convertToData() -> Data? {
let img = UIImage(cgImage: self)
let img = UIImage(
cgImage: self
)
return img.pngData()
}
}
Expand Down
Loading

0 comments on commit 74b0055

Please sign in to comment.