Skip to content

Library Usage Content

Chris W edited this page Dec 9, 2024 · 1 revision

Content Rendering

Goshot's content package provides flexible rendering capabilities for both code snippets and terminal output. This guide covers how to use the code and terminal renderers to create beautiful screenshots.

Code Renderer

The code renderer provides syntax highlighting and advanced formatting options for code snippets.

Basic Usage

import (
    "github.com/watzon/goshot/pkg/content/code"
)

// Create a renderer with default settings
renderer := code.DefaultRenderer(`
func main() {
    fmt.Println("Hello, World!")
}
`)

// Customize the renderer
renderer.WithTheme("dracula").
    WithLanguage("go").
    WithFontSize(14).
    WithLineNumbers(true).
    WithPadding(20, 20, 20, 20)

// Render the code
img, err := renderer.Render()
if err != nil {
    log.Fatal(err)
}

Code Style Options

The code renderer supports extensive customization through the CodeStyle struct:

style := &code.CodeStyle{
    Theme:             "dracula",    // Syntax highlighting theme
    Language:          "go",         // Programming language
    Font:             nil,           // Custom font (defaults to JetBrains Mono)
    FontSize:         14,           // Font size in points
    LineHeight:       1.2,          // Line height multiplier
    PaddingLeft:      20,           // Left padding in pixels
    PaddingRight:     20,           // Right padding in pixels
    PaddingTop:       20,           // Top padding in pixels
    PaddingBottom:    20,           // Bottom padding in pixels
    LineNumberPadding: 10,          // Space between line numbers and code
    TabWidth:         4,            // Tab width in spaces
    MinWidth:         0,            // Minimum width (0 = no minimum)
    MaxWidth:         0,            // Maximum width (0 = no limit)
    ShowLineNumbers:  true,         // Show line numbers
}

Line Ranges and Highlighting

You can render specific line ranges and highlight lines of interest:

renderer.WithLineRange(10, 20).              // Only render lines 10-20
    WithLineHighlightRange(15, 15)           // Highlight line 15

Redaction

The code renderer supports redacting sensitive information:

renderer.WithRedactionEnabled(true).
    WithRedactionPattern(`password\s*=\s*"[^"]*"`, "PASSWORD"). // Redact passwords
    WithRedactionBlurRadius(5).                                 // Blur radius for redacted text
    WithRedactionStyle(code.RedactionStyleBlur)                // Blur style (or Solid)

Terminal Renderer

The terminal renderer simulates a terminal environment with ANSI color support.

Basic Usage

import (
    "github.com/watzon/goshot/pkg/content/term"
)

// Create a renderer with default settings
renderer := term.DefaultRenderer([]byte("$ ls -la\ntotal 12\ndrwxr-xr-x  2 user user 4096 Dec  8 20:55 ."))

// Customize the renderer
renderer.WithTheme("dracula").
    WithFontSize(14).
    WithPadding(20, 20, 20, 20).
    WithShowPrompt().                        // Show command prompt
    WithAutoSize()                           // Auto-size to content

// Render the terminal
img, err := renderer.Render()
if err != nil {
    log.Fatal(err)
}

Terminal Style Options

The terminal renderer can be customized through the TermStyle struct:

style := &term.TermStyle{
    Args:          []string{"ls", "-la"},    // Command and arguments
    Theme:         "dracula",                // Terminal theme
    Font:          nil,                      // Custom font (defaults to JetBrains Mono)
    FontSize:      14,                       // Font size in points
    LineHeight:    1.2,                      // Line height multiplier
    PaddingLeft:   20,                       // Left padding in pixels
    PaddingRight:  20,                       // Right padding in pixels
    PaddingTop:    20,                       // Top padding in pixels
    PaddingBottom: 20,                       // Bottom padding in pixels
    Width:         80,                       // Terminal width in cells
    Height:        24,                       // Terminal height in cells
    AutoSize:      true,                     // Auto-size to content
    CellSpacing:   0,                        // Extra space between cells
    ShowPrompt:    true,                     // Show command prompt
}

Custom Prompts

You can customize the command prompt appearance:

renderer.WithPromptFunc(func(command string) string {
    return "user@host:~$ " + command
})

Best Practices

  1. Performance

    • Reuse renderers when creating multiple images with similar settings
    • Use appropriate font sizes for readability (12-14pt recommended)
    • Set reasonable max-width limits to prevent excessive memory usage
  2. Styling

    • Choose themes that provide good contrast for readability
    • Use consistent padding across different renderers
    • Consider line height for optimal readability (1.2-1.5)
  3. Terminal Output

    • Use auto-size for dynamic content
    • Set appropriate terminal dimensions for fixed-size output
    • Consider using custom prompts for context
  4. Code Formatting

    • Enable line numbers for longer snippets
    • Use line highlighting to emphasize important code
    • Consider tab width based on language conventions