carbon-now-cli

repository·master·Indexed 27 days ago

https://github.com/mixn/carbon-now-cli

A command-line interface for Carbon (carbon.now.sh) that generates high-quality images of code directly from the terminal. It supports input via files, stdin, or the clipboard, and offers customizable themes, fonts, and presets. Features include an interactive configuration mode, specific line range capture, and options to save images to disk, copy them to the clipboard, or open them in a browser.

Tokens
3.9K
Snippets
16
Records
24
Agent score
90%

What's inside carbon-now-cli

  1. Use local configuration files

    master

    To share presets across a project or team, use the --config flag to point to a local JSON configuration file.

    Important Notes on Local Configs:

    • They are read-only.
    • They will not be created automatically if they don't exist.
    • The latest-preset key is not written to local config files.
    carbon-now _unfold.js --config local-config.json --preset dark
  2. Create and use presets

    master

    Presets allow you to save and reuse your favorite styling settings.

    1. Creating a Preset: Run carbon-now with the --interactive flag. Follow the prompts to configure settings. The resulting configuration is saved to ~/.carbon-now.json.
    2. Using a Preset: Use the --preset flag followed by the name of your saved preset.

    Note: latest-preset is a special key in ~/.carbon-now.json that is overwritten after every run, while named presets remain until manually deleted.

  3. Install carbon-now-cli

    master

    You can install carbon-now-cli globally using various package managers or run it directly using npx.

    # Bun
    bun i -g carbon-now-cli
    
    # pnpm
    pnpm i -g carbon-now-cli
    
    # npm
    npm i -g carbon-now-cli
    
    # yarn
    yarn global add carbon-now-cli
    
    # Run without installing via npx
    npx carbon-now-cli <file>
  4. Configure custom theme colors in presets

    master

    You can define custom syntax highlighting colors by adding a custom key to a preset in your ~/.carbon-now.json file. This complies with the CarbonThemeHighlightsInterface.

    ```json
    {
      "hacker": {
        "backgroundColor": "rgba(0, 255, 0, 1)",
        "windowTheme": "bw",
        "custom": {
          "background": "rgba(0, 0, 0, 1)",
          "text": "rgba(0, 255, 0, 1)",
          "variable": "rgba(0, 255, 0, 1)",
          "attribute": "rgba(0, 255, 0, 1)",
          "definition": "rgba(0, 255, 0, 1)",
          "keyword": "rgba(0, 255, 0, 1)",
          "operator": "rgba(0, 255, 0, 1)",
          "property": "rgba(0, 255, 0, 1)",
          "number": "rgba(0, 255, 0, 1)",
          "string": "rgba(0, 255, 0, 1)",
          "comment": "rgba(0, 255, 0, 1)",
          "meta": "rgba(0, 255, 0, 1)",
          "tag": "rgba(0, 255, 0, 1)"
        }
      }
    }

    Limitation: Custom theme colors are not applied when using --open-in-browser because they rely on localStorage within the Playwright instance rather than query string parameters.

  5. Configure Carbon CLI via interactive prompts

    master

    When running the Carbon CLI in interactive mode, you can configure the visual appearance of your code exports using the following prompt options:

    Visual Styling

    • theme: Select the syntax theme (e.g., 3024 Night).
    • windowTheme: Select the window theme (default: none).
    • fontFamily: Select the font family (default: Hack).
    • fontSize: Set the font size (default: 18px).
    • lineHeight: Set the line height (default: 133%).
    • backgroundColor: Set the background color (default: #ADB7C1).

    Layout and Padding

    • paddingVertical: Set vertical padding (default: 0px).
    • paddingHorizontal: Set horizontal padding (default: 0px).
    • widthAdjustment: Auto adjust width? (default: true).
    • squaredImage: Make squared image? (default: false).

    Window and Decorations

    • windowControls: Include window controls? (default: true).
    • lineNumbers: Include line numbers? (default: false).
      • firstLineNumber: If line numbers are enabled, set the starting line number (default: 1).
    • dropShadow: Include drop shadow? (default: false).
      • dropShadowOffsetY: If drop shadow is enabled, set the y-offset (default: 20px).
      • dropShadowBlurRadius: If drop shadow is enabled, set the blur radius (default: 68px).

    Highlighting

    • highlight: Highlight certain lines? (default: false).
      • selectedLines: Comma-separated list of lines to highlight (e.g., 4,5,6,7). Default is *.

    Export Settings

    • exportSize: Select the export scale (e.g., 2x).
    • type: Select the export format: png or svg (default: png).
    • watermark: Add Carbon watermark? (default: false).

    Presets

    • save: Save these settings as a preset? (default: false).
      • presetName: Name of the preset using kebab-case (default: uses the latest preset constant).
  6. Use stdin or clipboard as input sources

    master

    You can pipe content into carbon-now via stdin or pull content directly from your clipboard.

    Using stdin:

    # Using pbpaste (macOS)
    pbpaste | carbon-now
    
    # Using echo
    echo '<h1>Hi</h1>' | carbon-now

    Using clipboard:

    carbon-now --from-clipboard
  7. Generate images for specific line ranges

    master

    Use the --start and --end flags to capture only a specific subset of lines from a file. Note that the command will throw an error if --start is greater than --end.

    carbon-now _unfold.js --start 3 --end 6
  8. Copy generated image to clipboard

    master

    Use the --to-clipboard flag to copy the resulting image directly to your clipboard instead of saving it to a file.

    Platform Requirements:

    • Linux: Requires xclip. Install via sudo apt-get install xclip.
    • Windows & macOS: Works out of the box.
    carbon-now _unfold.js --to-clipboard
  9. Override settings and use presets

    master

    You can customize a run by specifying a --preset and providing a JSON string of --settings to override specific configuration keys.

    carbon-now _unfold.js --preset presentation --settings '{"theme": "nord", "titleBar": "custom-title.js"}'
  10. Save images to specific locations and names

    master

    Use --save-to to specify a directory and --save-as to specify a filename (without extension) for the output image.

    carbon-now _unfold.js --start 3 --end 6 --save-to ~/Desktop --save-as example-23 --interactive