Skip to content

Commit

Permalink
Add debug output to CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
khlopko committed Mar 17, 2024
1 parent 4b49d1b commit 014dd22
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
9 changes: 8 additions & 1 deletion Cli/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import DotMd
struct Cli: ParsableCommand {
@Argument
var inputFilePath: String

@Flag
var debug: Bool = false

mutating func run() throws {
let file = fopen(inputFilePath, "r")
Expand All @@ -27,7 +30,11 @@ struct Cli: ParsableCommand {
}
}
let md = try Markdown(contents: contents)
print(md)
if debug {
print(md.debugDescription)
} else {
print(md.description)
}
}
}

19 changes: 19 additions & 0 deletions Markdown/Markdown.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ extension Markdown: CustomStringConvertible {
}
}

extension Markdown: CustomDebugStringConvertible {
public var debugDescription: String {
blocks.map(\.debugDescription).joined(separator: "\n")
}
}

public enum Block: Equatable {
case p([Block])
case text(String, TextStyle)
Expand All @@ -37,6 +43,19 @@ extension Block: CustomStringConvertible {
}
}

extension Block: CustomDebugStringConvertible {
public var debugDescription: String {
switch self {
case let .p(blocks):
return "p(\(blocks.map(\.debugDescription).joined(separator: ", ")))"
case let .text(value, style):
return "text(\(value), \(style))"
case let .h(level, block):
return "h(\(level), \(block.debugDescription))"
}
}
}

public enum HeaderLevel: Int, Equatable {
case h1 = 1
case h2
Expand Down

0 comments on commit 014dd22

Please sign in to comment.