Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/eneko/consoleui
Rasterize SwiftUI views to images from macOS terminal
https://github.com/eneko/consoleui
command-line swift-package-manager swiftpm swiftui terminal
Last synced: 23 days ago
JSON representation
Rasterize SwiftUI views to images from macOS terminal
- Host: GitHub
- URL: https://github.com/eneko/consoleui
- Owner: eneko
- Created: 2019-06-21T12:18:44.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-09-20T22:56:01.000Z (over 3 years ago)
- Last Synced: 2025-01-10T18:55:54.014Z (about 1 month ago)
- Topics: command-line, swift-package-manager, swiftpm, swiftui, terminal
- Language: Swift
- Size: 874 KB
- Stars: 58
- Watchers: 4
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
Awesome Lists containing this project
README
# ConsoleUI
Demo repository to showcase how to **programmatically generate images from a command-line tool with [SwiftUI](https://developer.apple.com/xcode/swiftui/)**.
![](Images/test.png)
## The Basics: Hello, world!
### Create a new command-line executable
```
$ mkdir mytool && cd mytool
$ swift package init --type executable
$ swift run
Hello, world!
```### Generate Xcode project (optional)
Xcode does not support live previews on Swift packages yet (FB8979344). To allow for realtime previews follow these steps:
1. Generate an Xcode project
2. Add a macOS target to the project
3. Add your view (see next section) to the macOS target```
$ swift package generate-xcodeproj
$ open mytool.xcodeproj
```### Add SwiftUI view
Create a view with a red label over white background
```swift
import SwiftUIstruct MySwiftUIView : View {
var body: some View {
ZStack {
Color.blue.edgesIgnoringSafeArea(.all)
Text("Hello, world!")
.foregroundColor(.white)
.font(.largeTitle)
}
}
}#if DEBUG
struct MySwiftUIView_Previews : PreviewProvider {
static var previews: some View {
MySwiftUIView()
}
}
#endif
```![SwiftUI](Images/MySwiftUIView.png)
### Rasterize view and save to file
We will use this method to rasterize views to images:
```swift
func rasterize(view: NSView, format: NSBitmapImageRep.FileType) -> Data? {
guard let bitmapRepresentation = view.bitmapImageRepForCachingDisplay(in: view.bounds) else {
return nil
}
bitmapRepresentation.size = view.bounds.size
view.cacheDisplay(in: view.bounds, to: bitmapRepresentation)
return bitmapRepresentation.representation(using: format, properties: [:])
}
```The following code instantiates the SwiftUI view, wrapped inside a `NSHostingView` which is then
rasterized and saved to disk```swift
let wrapper = NSHostingView(rootView: MySwiftUIView())
wrapper.frame = CGRect(x: 0, y: 0, width: 800, height: 450)let png = rasterize(view: wrapper, format: .png)
try png?.write(to: URL(fileURLWithPath: "test.png"))
```![main.swift](Images/Main.png)
### Run the command and open in Preview
```
$ swift run && open test.png
```![Preview](Images/Preview.png)