Skip to content

🦀️📸 Pure Rust tool to generate beautiful code snapshots, provide CLI and Library

License

Notifications You must be signed in to change notification settings

mistricky/CodeSnap

Repository files navigation

Lint CI Code Style CI Crates.io Version Lint convention

CodeSnap

Warning

This project is still in early stage and may have some bugs

CodeSnap is a pure Rust tool for generate beautiful code snapshots, it directly use graphic engine to generate snapshots, which means the entire process is just matter of computation and rendering, without need for network or something like browser-based rendering solution.

Generally, you can directly use CLI tool provide by CodeSnap to generate code snapshots you want. Or CodeSnap also provide a library for you to integrate it into your own project, so you can generate code snapshots in your own way (See Related projects for more information).

📷 Screenshots

✨ Features

  • Fast: Pure Rust tool, generate code snapshot from graphic engine directly.
  • CLI tool: CodeSnap provide a CLI tool for you to generate code snapshot directly from command line.
  • Library: CodeSnap also provide a library for you to integrate it into your own project.
  • Line number: Generate code snapshot with line number, it's really helpful if someone want to know the position of the code snippet.
  • Watermark: Watermark can help make your code snapshot more personalized and interesting.
  • More beautiful themes: The Syntect be treated as the syntax highlighterin CodeSnap, and it using Sublime Text syntax definitions to highlight code, so basically you can use any theme that Sublime Text support.
  • Scale: You can scale your code snapshot with a specific scale factor, CodeSnap will generate treble size snapshot by default to ensure the quality of the snapshot.
  • Beautiful background: CodeSnap provide a beautiful background for your code snapshot, you can also customize the background color with solid color or gradient color.
  • Multiple snapshot format: CodeSnap support multiple snapshot format, you can save snapshot as PNG, SVG and even HTML, or you want try ASCII code snapshot :)
  • Clipboard: CodeSnap can copy snapshot to clipboard directly, or read code snippet from clipboard to generate snapshots.
  • Breadcrumb: CodeSnap provide a breadcrumb for you to share your code snapshot with code path, it's really helpful if others want to know where the code snippet comes from.

💻 Getting started

CodeSnap provide two ways to use it, you can use it as a CLI tool or as a library in your own project.

CLI

For CLI tool, you can install it for different platforms:

Arch Linux

CodeSnap is available in the extra repository:

pacman -S codesnap
Nix/NixOS

CodeSnap is available in the nixpkgs:

nix-env -i codesnap
Cargo
cargo install codesnap-cli
Homebrew
brew install mistricky/tap/CodeSnap

Use codesnap command to generate code snapshot:

# Run codesnap to generate code snapshot by providing code file
codesnap -f ./code_snippet.hs -o "./output.png"

# Run codesnap --help to see more information
codesnap -h

Library

For library, add CodeSnap in your project using Cargo

cargo add codesnap

Use CodeSnap builder to generate code snapshot:

CodeSnap::default()
        .code("fn main() { println!(\"Hello, world!\"); }")
        .watermark(WatermarkBuilder::default().content("CodeSnap").build()?)
        .build()?
        .create_snapshot()?.raw_data()?.copy()?;

🌰 Examples

All examples can be found in examples.

hello

⚙️ Configuration

Codesnap can receive a JSON config as input, the config can be used to customize the snapshot, such as theme, background, watermark, etc.

If you are using Library, you can mount config to CodeSnap builder:

CodeSnap::from_config("Your config")?;

Or if you are using CLI tool, CodeSnap will generate a default config file for you under ~/.config/CodeSnap, you can modify the config file to customize the snapshot:

// Both "CaskaydiaCove Nerd Font" and "Pacifico" is pre-installed in CodeSnap, you can use them out of the box
{
  "window": {
    "mac_window_bar": true,
    "shadow": 20,
    "margin": {
      "x": 82,
      "y": 82
    }
  },
  "code": {
    "font_family": "CaskaydiaCove Nerd Font",
    // CodeSnap use candy theme by default, if you want to use other theme, please refer https://github.com/trishume/syntect
    "theme": "candy"
  },
  "watermark": {
    "content": "CodeSnap",
    "font_family": "Pacifico",
    "color": "#ffffff"
  },
  // If you want to use gradient color, you can provide stops like the following config
  // But if you want to use solid color, you can just provide a color string like: 
  // "background": "#6bcba5"
  "background": {
    "start": {
      "x": 0,
      "y": 0
    },
    "end": {
      "x": "max",
      "y": 0
    },
    "stops": [
      {
        "position": 0,
        "color": "#6bcba5"
      },
      {
        "position": 1,
        "color": "#caf4c2"
      }
    ]
  }
}

All configuration items can be found in config.rs

Linux Wayland

Copy screenshots directly into the clipboard is cool, however, it doesn't work well on wl-clipboard, because the wl-clipboard can't paste the content which come from exited processes. As Hyprland document say:

When we copy something on Wayland (using wl-clipboard) and close the application we copied from, the copied data disappears from the clipboard and we cannot paste it anymore. So to fix this problem we can use a program called as wl-clip-persist which will preserve the data in the clipboard after the application is closed.

If you are using CodeSnap on wl-clipboard, you can refer wl-clip-persist, it reads all the clipboard data into memory and then overwrites the clipboard with the data from our memory to persist copied data.

❤️ Related projects

📑 License

MIT.

About

🦀️📸 Pure Rust tool to generate beautiful code snapshots, provide CLI and Library

Resources

License

Stars

Watchers

Forks

Contributors 4

  •  
  •  
  •  
  •