Skip to content

Commit

Permalink
More complete HTML renderer implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
marciniwanicki committed May 25, 2020
1 parent c5b0e24 commit bb789c1
Show file tree
Hide file tree
Showing 14 changed files with 603 additions and 715 deletions.
114 changes: 114 additions & 0 deletions CommandTests/Generated/html_format.2.e5ef416a.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

114 changes: 114 additions & 0 deletions CommandTests/Generated/html_format_verbose.2.3ef640ed.md

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions CommandTests/manual_test_commands.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -25,80 +25,6 @@ final class TextProjectCompareResultRenderer: ProjectCompareResultRenderer {
self.verbose = verbose
}

func render(_ result: ProjectCompareResult) {
result.results.forEach(render)
}

// MARK: - Private

private func render(_ result: CompareResult) {
guard result.same() == false else {
renderer.successHeader(title(from: result))
return
}

renderer.errorHeader(title(from: result))

guard verbose else {
return
}

if let description = result.description {
renderer.text(description)
}

let onlyInFirst = result.onlyInFirst
if !onlyInFirst.isEmpty {
renderer.onlyInFirstHeader(count: onlyInFirst.count)
renderer.list(.begin)
onlyInFirst.forEach {
renderer.bullet($0, indent: .one)
}
renderer.list(.end)
}

let onlyInSecond = result.onlyInSecond
if !onlyInSecond.isEmpty {
renderer.onlyInSecondHeader(count: onlyInSecond.count)
renderer.list(.begin)
onlyInSecond.forEach {
renderer.bullet($0, indent: .one)
}
renderer.list(.end)
}

let differentValues = result.differentValues
if !differentValues.isEmpty {
renderer.differentValuesHeader(count: differentValues.count)
differentValues.forEach {
renderer.list(.begin)
renderer.bullet($0.context, indent: .one)
renderer.bullet("\($0.first)", indent: .two)
renderer.bullet("\($0.second)", indent: .two)
renderer.list(.end)
}
}

renderer.newLine(1)
}

private func title(from result: CompareResult) -> String {
let rootContext = result.tag.uppercased()
let subContext = !result.context.isEmpty ? " > " + result.context.joined(separator: " > ") : ""
return rootContext + subContext
}
}


final class TextProjectCompareResultRenderer2: ProjectCompareResultRenderer {
private let renderer: Renderer2
private let verbose: Bool

init(renderer: Renderer2, verbose: Bool) {
self.renderer = renderer
self.verbose = verbose
}

func render(_ result: ProjectCompareResult) {
renderer.begin()
result.results.forEach(render)
Expand Down Expand Up @@ -160,7 +86,7 @@ final class TextProjectCompareResultRenderer2: ProjectCompareResultRenderer {
renderer.list {
differentValues.forEach { item in
renderer.item {
renderer.text(item.context)
renderer.pre(item.context)
renderer.list {
renderer.item(item.first)
renderer.item(item.second)
Expand All @@ -172,7 +98,9 @@ final class TextProjectCompareResultRenderer2: ProjectCompareResultRenderer {
}

// added for compatibility with the old renderer
renderer.line(1)
if differentValues.isEmpty {
renderer.line(1)
}
}
}

Expand Down
73 changes: 6 additions & 67 deletions Sources/XCDiffCore/ResultRenderer/Renderer/ConsoleRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,69 +18,6 @@ import Foundation

final class ConsoleRenderer: Renderer {
private let output: AnyOutput<String>

init(output: AnyOutput<String>) {
self.output = output
}

func text(_ text: String) {
write("\(text)\n")
}

func list(_ element: RendererElement.List) {
switch element {
case .begin:
return
case .end:
newLine(1)
}
}

func bullet(_ text: String, indent: RendererElement.Indent) {
let spacing = String(repeating: " ", count: 2 * indent.rawValue)
let symbol = bullet(indent: indent)
self.text("\(spacing)\(symbol) \(text)")
}

func newLine(_ count: Int = 1) {
write(String(repeating: "\n", count: count))
}

func header(_ text: String, _ header: RendererElement.Header) {
switch header {
case .h1:
write("\n")
write("=\n")
write("= \(text)\n")
write("=\n")
write("\n")
case .h2:
write("\(text)\n")
case .h3:
write("\n\(text)\n\n")
}
}

// MARK: - Private

private func bullet(indent: RendererElement.Indent) -> String {
switch indent {
case .zero:
return "»"
case .one:
return ""
case .two:
return ""
}
}

private func write(_ string: String) {
output.write(string)
}
}

final class ConsoleRenderer2: Renderer2 {
private let output: AnyOutput<String>
private var indent: Int = 0

init(output: AnyOutput<String>) {
Expand All @@ -95,7 +32,7 @@ final class ConsoleRenderer2: Renderer2 {
// nothing
}

func section(_ style: RendererElement.Style, _ content: () -> Void) {
func section(_: RendererElement.Style, _ content: () -> Void) {
content()
}

Expand All @@ -118,6 +55,10 @@ final class ConsoleRenderer2: Renderer2 {
write("\(text)\n")
}

func pre(_ text: String) {
self.text(text)
}

func list(_ content: () -> Void) {
indent += 1
content()
Expand All @@ -128,6 +69,7 @@ final class ConsoleRenderer2: Renderer2 {
func item(_ text: String) {
item {
write(text)
line(1)
}
}

Expand All @@ -136,7 +78,6 @@ final class ConsoleRenderer2: Renderer2 {
let symbol = bullet(indent: indent)
write("\(spacing)\(symbol) ")
content()
line(1)
}

func line(_ count: Int) {
Expand All @@ -151,8 +92,6 @@ final class ConsoleRenderer2: Renderer2 {

private func bullet(indent: Int) -> String {
switch indent {
case 0:
return "»"
case 1:
return ""
default:
Expand Down
Loading

0 comments on commit bb789c1

Please sign in to comment.