37 releases (10 breaking)

new 0.10.8 Apr 9, 2025
0.10.5 Feb 5, 2025
0.8.3 Dec 22, 2024
0.7.2 Nov 30, 2024

#216 in Images

Download history 251/week @ 2024-12-19 152/week @ 2024-12-26 179/week @ 2025-01-02 206/week @ 2025-01-09 5/week @ 2025-01-16 612/week @ 2025-01-30 312/week @ 2025-02-06 39/week @ 2025-02-13 6/week @ 2025-02-20 11/week @ 2025-02-27 3/week @ 2025-03-27 208/week @ 2025-04-03

220 downloads per month
Used in codesnap-cli

MIT license

3MB
3K SLoC

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

Or install via precompiled binary:

cargo binstall 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:

let code_content = Content::Code(
  CodeBuilder::from_t
  .content(r#"print "Hello, World!""#)
  .language("python")
  .build()?,
);

let snapshot = CodeSnap::from_default_theme()
  .content(code_content)
  .build()?
  .create_snapshot()?;

// Copy the snapshot data to the clipboard
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
{
  "theme": "candy",
  "window": {
    "mac_window_bar": true,
    "shadow": {
      "radius": 20,
      "color": "#00000040"
    },
    "margin": {
      "x": 82,
      "y": 82
    },
    "border": {
      "width": 1,
      "color": "#ffffff30"
    }
  },
  "code_config": {
    "font_family": "CaskaydiaCove Nerd Font",
    "breadcrumbs": {
      "separator": "/",
      "color": "#80848b",
      "font_family": "CaskaydiaCove Nerd Font"
    }
  },
  "watermark": {
    "content": "CodeSnap",
    "font_family": "Pacifico",
    "color": "#ffffff"
  },
  "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.

📑 License

MIT.


lib.rs:

CodeSnap is a tool to generate beautiful snapshots of your code snippets. It's a pure Rust library that provides a simple API to create snapshots of code snippets with syntax highlighting, line numbers, code theme and more.

Quick start

CodeSnap::default()
    .code(
        CodeBuilder::default()
            .language("haskell")
            .content(r#"print "Hello, CodeSnap!""#)
            .build()?,
    )
    .watermark(WatermarkBuilder::default().content("YYM").build()?)
    .build()?
    .create_snapshot()?
    .raw_data()?
    .copy()?;

Now try to paste the code snapshot to your friends! (Don't forget tell him that this was generated by CodeSnap! ^ ^)

Dependencies

~25–40MB
~571K SLoC