-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Model Canvas and Builtin views as protocols
- Loading branch information
Showing
18 changed files
with
91 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
protocol Builtin { | ||
func render(in canvas: any Canvas, environment: EnvironmentValues) | ||
} | ||
|
||
extension Builtin { | ||
public var body: Never { | ||
fatalError("Body should never be called.") | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
|
||
struct AppCanvas<Output: TextOutputStream>: Canvas { | ||
@Mutable var output: Output | ||
|
||
func draw(_ pixel: Pixel, at position: Position) { | ||
output.write(pixel.foreground.foreground) | ||
output.write(pixel.background.background) | ||
output.write(pixel.bold.controlSequence) | ||
output.write(pixel.italic.controlSequence) | ||
output.write(pixel.underline.controlSequence) | ||
output.write(pixel.blinking.controlSequence) | ||
output.write(pixel.inverse.controlSequence) | ||
output.write(pixel.hidden.controlSequence) | ||
output.write(pixel.strikethrough.controlSequence) | ||
output.write(position.controlSequence) | ||
output.write(pixel.content) | ||
} | ||
} | ||
|
||
extension TextOutputStream { | ||
fileprivate mutating func write(_ character: Character) { | ||
write(String(character)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
protocol Canvas { | ||
func draw(_ pixel: Pixel, at position: Position) | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,27 @@ | ||
|
||
public struct Text { | ||
public struct Text: Builtin, View { | ||
|
||
private let string: String | ||
|
||
public init(_ string: String) { | ||
self.string = string | ||
} | ||
} | ||
|
||
extension Text: View { | ||
|
||
public var body: some View { | ||
BuiltinView { canvas, environment in | ||
for (character, index) in zip(string, 1...) { | ||
let pixel = Pixel( | ||
character, | ||
foreground: environment.foregroundColor, | ||
background: environment.backgroundColor, | ||
bold: environment.bold, | ||
italic: environment.italic, | ||
underline: environment.underline, | ||
blinking: environment.blinking, | ||
inverse: environment.inverse, | ||
hidden: environment.hidden, | ||
strikethrough: environment.strikethrough | ||
) | ||
canvas.draw(pixel, at: Position(x: index, y: 0)) | ||
} | ||
func render(in canvas: any Canvas, environment: EnvironmentValues) { | ||
for (character, index) in zip(string, 1...) { | ||
let pixel = Pixel( | ||
character, | ||
foreground: environment.foregroundColor, | ||
background: environment.backgroundColor, | ||
bold: environment.bold, | ||
italic: environment.italic, | ||
underline: environment.underline, | ||
blinking: environment.blinking, | ||
inverse: environment.inverse, | ||
hidden: environment.hidden, | ||
strikethrough: environment.strikethrough | ||
) | ||
canvas.draw(pixel, at: Position(x: index, y: 0)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,19 @@ | ||
@testable import TerminalUI | ||
import Testing | ||
|
||
package struct TestCanvas: Canvas { | ||
@Mutable package var pixels: [Position: Pixel] = [:] | ||
package init() {} | ||
package func draw(_ pixel: Pixel, at position: Position) { | ||
pixels[position] = pixel | ||
} | ||
} | ||
|
||
extension View { | ||
|
||
package func expect(_ expected: [Position: Pixel]) { | ||
var pixels: [Position: Pixel] = [:] | ||
let canvas = Canvas { pixels[$1] = $0 } | ||
render(in: canvas) | ||
#expect(pixels == expected) | ||
let canvas = TestCanvas() | ||
_render(in: canvas) | ||
#expect(canvas.pixels == expected) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters