-
Notifications
You must be signed in to change notification settings - Fork 4
Library Usage
Chris W edited this page Dec 9, 2024
·
5 revisions
Goshot's library provides a flexible and powerful API for creating code screenshots programmatically. This guide covers the main components and how to use them effectively in your Go applications.
The library is organized into several main components:
- Canvas - The main rendering surface where your code screenshot is composed
- Chrome - Window decorations (macOS, Windows, or Linux style)
- Background - Various background options (solid colors, gradients, images)
- Content - Code and terminal output rendering with syntax highlighting
- Fonts - Font management and customization
- Window Chrome - Customize the window decorations
- Backgrounds - Different background options and configurations
- Content - Code and terminal output rendering
- Font Management - Working with fonts and text rendering
Here's a simple example that demonstrates the basic usage of Goshot:
package main
import (
"image/color"
"log"
"github.com/watzon/goshot/pkg/background"
"github.com/watzon/goshot/pkg/chrome"
"github.com/watzon/goshot/pkg/content/code"
"github.com/watzon/goshot/pkg/render"
)
func main() {
// Create code content
codeContent := code.DefaultRenderer(`func main() {
fmt.Println("Hello, World!")
}`).
WithTheme("dracula").
WithLanguage("go").
WithLineNumbers(true)
// Create a new canvas
canvas := render.NewCanvas().
// Add macOS-style window chrome
WithChrome(chrome.NewMacChrome(
chrome.WithTitle("main.go"),
chrome.WithThemeByName("sequoia", chrome.ThemeVariantDark),
)).
// Set a gradient background
WithBackground(
background.NewGradientBackground(
background.LinearGradient,
background.GradientStop{Color: color.RGBA{R: 26, G: 27, B: 38, A: 255}, Position: 0},
background.GradientStop{Color: color.RGBA{R: 40, G: 42, B: 54, A: 255}, Position: 1},
).
WithAngle(45).
WithPadding(40),
).
// Set the content
WithContent(codeContent)
// Render to an image
img, err := canvas.Render()
if err != nil {
log.Fatal(err)
}
// Save the image to a file
if err := render.SaveAsPNG(img, "output.png"); err != nil {
log.Fatal(err)
}
}
Here's an example of rendering terminal output:
package main
import (
"github.com/watzon/goshot/pkg/background"
"github.com/watzon/goshot/pkg/chrome"
"github.com/watzon/goshot/pkg/content/term"
"github.com/watzon/goshot/pkg/render"
)
func main() {
// Create terminal content
termContent := term.DefaultRenderer([]byte("$ ls -la\ntotal 12\ndrwxr-xr-x 2 user user 4096 Dec 8 20:55 .")).
WithTheme("dracula").
WithShowPrompt().
WithAutoSize()
// Create a new canvas
canvas := render.NewCanvas().
WithChrome(chrome.NewMacChrome(
chrome.WithTitle("Terminal"),
chrome.WithThemeByName("sequoia", chrome.ThemeVariantDark),
)).
WithBackground(background.NewColorBackground().
WithColor(color.RGBA{R: 40, G: 42, B: 54, A: 255}).
WithPadding(20),
).
WithContent(termContent)
// Render and save
img, err := canvas.Render()
if err != nil {
log.Fatal(err)
}
if err := render.SaveAsPNG(img, "terminal.png"); err != nil {
log.Fatal(err)
}
}
Choose a component from the Quick Links section above to learn more about specific features and capabilities. Each component page includes detailed examples and configuration options.
-
Component Organization
- Create and configure content (code/terminal) first
- Add window chrome and background to enhance presentation
- Use consistent themes across components
-
Performance
- Reuse renderers when creating multiple images
- Configure appropriate image dimensions
- Close font faces when done
-
Error Handling
- Always check render errors
- Provide fallbacks for missing fonts
- Handle file operations safely
This project is licensed under the MIT License - see the LICENSE file for details.
-
π€ Contributing
- Code of Conduct
- Development Workflow
- Project Structure
- Guidelines
- Testing
- Documentation