Pretty print for Swift

SwiftPrettyPrint

Parameterized-test for Swift. (with XCTest)

Pretty print that is Human-readable output than print() and debugPrint() in Swift standard library.

Motivation ?

print and debugPrint is implemented by standard library of Swift.
But both function is not readable output string to console sometime.
This is especially when using struct as ValueObject.

For example:

struct Dog {
    var id: DogId
    var price: Price
    var name: String?
}

struct DogId {
    var rawValue: String
}

struct Price {
    var rawValue: Double
}

let dog = Dog(id: DogId(rawValue: "pochi"), price: Price(rawValue: 10.0), name: "ポチ")

print(dog)
// => Dog(id: SwiftPrettyPrint.DogId(rawValue: "pochi"), price: SwiftPrettyPrint.Price(rawValue: 10.0), name: Optional("ポチ"))

debugPrint(dog)
// => SwiftPrettyPrint.Dog(id: SwiftPrettyPrint.DogId(rawValue: "pochi"), price: SwiftPrettyPrint.Price(rawValue: 10.0), name: Optional("ポチ"))

This output is enough information for debug,
but not human-readable for when forcusing on the values.

With SwiftPrittyPrint it looks like this:

Debug.print(dog)
// => Dog(id: "pochi", price: 10.0, name: "ポチ")

Debug.debugPrint(dog)
// => Dog(id: DogId(rawValue: "pochi"), price: Price(10.0), name: Optional("ポチ"))

Installation

CocoaPods (Recommended)

pod "SwiftPrettyPrint", :configuration => "Debug" # enabled on `Debug` build only

Carthage

github "YusukeHosonuma/SwiftPrettyPrint"

Swift Package Manager

dependencies: [
    .package(url: "https://github.com/YusukeHosonuma/SwiftPrettyPrint.git", from: "0.0.3"),
]

or add from Xcode 10+.

Recommend Settings ?

If you don't want to write import statement, I recommended to create Debug.swift in each targets.

// Note:
// Enabled on `debug` configuration only.
// Therefore compile error in `release` build when remaining `Debug` in sources.

#if canImport(SwiftPrettyPrint)
    import SwiftPrettyPrint
    typealias Debug = SwiftPrettyPrint.Debug
#endif

This can be not need to import as follows:

let dog = Dog(id: DogId(rawValue: "pochi"), price: Price(rawValue: 10.0), name: "ポチ")
Debug.print(dog)

Format options

You can configure format option, shared or passed by arguments:

// Global option
Debug.sharedOption = Debug.Option(indent: 4)

// Use `sharedOption`
Debug.prettyPrint(["Hello", "World"])
// =>
// [
//     "Hello",
//     "World"
// ]

// Use option that is passed by argument
Debug.prettyPrint(["Hello", "World"], option: Debug.Option(indent: 2))
// =>
// [
//   "Hello",
//   "World"
// ]

Operator-based API

You can use operator based alias API that like Ruby.

This is no need to enclose in parenthese that convenient to long expression.

Debug.p >>> 42
// => 42

Debug.p >>> 42 + 1 // It can also be applied to expression
// => 43

Debug.p >>> String(string.reversed()).hasSuffix("eH")
// => true

Debug.pp >>> ["Hello", "World"]
// =>
// [
//     "Hello",
//     "World"
// ]
Operator syntax Equatable to
Debug.p >>> 42 Debug.print(42)
Debug.pp >>> 42 Debug.prettyPrint(42)
Debug.pd >>> 42 Debug.debugPrint(42)
Debug.ppd >>> 42 Debug.debugPrettyPrint(42)

Xcode Code Snippets

Copy .codesnippet files to the following directory from .xcode directory:

~/Library/Developer/Xcode/UserData/CodeSnippets/

and restart Xcode.

Or run the following command from the root of the repository:

$ make snippets

Develoopment

Require:

Execute make setup to install development tools to system (not include Xcode 11.3).

$ make help
setup      Install requirement development tools to system and setup (not include Xcode 11.3)
build      swift - build
test       swift - test
xcode      swift - generate xcode project
format     format sources by SwiftFormat
lint       cocoapods - lint podspec
release    cocoapods - release
info       cocoapods - show trunk information

GitHub