Skip to content
aswin402Public

About

Instant offline code extractor for Linux (Wayland & GNOME) with smart OCR and multi-language auto-formatting

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

Β 

History

19 Commits

Folders and files

Repository files navigation

codesnap logo

Instant, offline code extractor for Linux (Wayland & GNOME).

Turn video tutorials, PDFs, conference talks, and IDE screenshots into clean, formatted code on your clipboard in milliseconds.


CI PyPI Release License: MIT Python: >=3.10 Platform: Wayland Code style: Ruff


Hotkey Trigger
Super + Shift + L
Selection
Native Desktop Portal Crosshair
Processing
Denoise + Invert + PSM 6 OCR
Formatting
Blazing-fast Ruff (Rust)
Result
Clean Code in Clipboard

🌟 Why codesnap?

Standard OCR tools stumble on code: dark themes fail contrast checks, indentation gets mangled, language keywords are misread, and Wayland compositors (like GNOME Mutter) block legacy capture utilities.

codesnap is purpose-built for developers:

Feature Standard OCR Utilities codesnap v2.4
Wayland Support ❌ Fails on Mutter / GNOME 40+ βœ… Native XDG Desktop Portal + slurp fallback
Dark Theme IDEs ❌ Poor contrast, missing symbols βœ… Automatic polarity detection & contrast inversion
OCR Code Cleaning ❌ Leaves stray |, broken def βœ… AST-safe token correction (preserves 100, utf8)
OCR Architecture ❌ Rigid single legacy engine βœ… Dual-Engine: RapidOCR (ONNX) + Tesseract fallback
Indentation & Formatting ❌ Random tabs, mangled blocks βœ… Multi-language auto-formatting (Ruff, rustfmt, gofmt, biome, prettier)
Resource Footprint ⚠️ Heavy wheels (numpy), 100MB+ βœ… Zero NumPy bloat β€” C-accelerated Pillow, <400ms runtime
Language Intelligence ❌ Plain unformatted text βœ… 11+ languages auto-detected & highlighted
Privacy & Security ⚠️ Often calls cloud APIs βœ… 100% offline and local β€” zero network requests

πŸš€ Quick Start

Option A: One-Liner Install (Recommended)

Run this single command in your terminal. It installs system tools, configures the environment with uv, and registers Super + Shift + L:

curl -sSL https://raw.githubusercontent.com/aswin402/codesnap/main/setup.sh | bash

Option B: Install via PyPI (pip / uv)

Install globally as a CLI tool:

# Using uv (fastest & recommended)
uv tool install codesnap-cli

# Run instantly without installing
uvx codesnap-cli

# Or using standard pip
pip install codesnap-cli

Option C: Install from Source

git clone https://github.com/aswin402/codesnap.git
cd codesnap
chmod +x setup.sh
./setup.sh

Verify your installation:

codesnap --version

2. Upgrading Existing Installations

If you already have codesnap installed, upgrade in-place without touching system packages:

git pull
./localupdate.sh

🎯 Usage

Keyboard Workflow

  1. Press Super + Shift + L anywhere on your system.
  2. Select any code block on your screen (press Esc anytime to cancel).
  3. codesnap extracts, cleans, normalizes, and auto-formats the code.
  4. A desktop notification confirms: Python β€’ 14 lines copied.
  5. Paste directly into your editor with Ctrl + V.

CLI & Power-User Options

# Extract code directly from an existing image file
codesnap /path/to/screenshot.png

# Save extracted code straight to a file
codesnap /path/to/screenshot.png -o snippet.py

# Pipe extracted code directly to stdout
codesnap /path/to/screenshot.png -c | bat -l python

# Override language auto-detection
codesnap /path/to/screenshot.png -l rust

# High-quality preprocessing for low-res or blurry screenshots
codesnap --high-quality

# Select OCR engine (auto, rapidocr, or tesseract)
codesnap --engine rapidocr

# Review and edit OCR output in $EDITOR before copying to clipboard
codesnap --interactive

# Inspect diagnostics, installed OCR engines, and system backend health
codesnap --version

🧠 System Architecture

flowchart TD
    A["⌨️ Super + Shift + L"] --> B{"Display Server & Desktop"}
    B -->|"GNOME Wayland"| C["XDG Desktop Portal (jeepney D-Bus)"]
    B -->|"wlroots (Sway/Hyprland)"| D["slurp + grim"]
    B -->|"X11 Session"| E["gnome-screenshot -a"]

    C --> F["Raw Screenshot PNG"]
    D --> F
    E --> F

    F --> G["Image Preprocessor"]
    G --> H["Dark Mode Detection & Inversion"]
    H --> I["Contrast Enhancement & Denoising"]
    I --> J["Tesseract OCR (--psm 6 uniform code)"]
    
    J --> K["Smart Character Cleaner"]
    K --> L["Multi-Language Detector (11+ Languages)"]
    L --> M{"Formatter Available?"}
    M -->|"Python"| N1["Ruff (Rust engine)"]
    M -->|"Rust"| N2["rustfmt"]
    M -->|"Go"| N3["gofmt"]
    M -->|"JS / TS / JSON"| N4["biome / prettier"]
    M -->|"Bash"| N5["shfmt"]
    M -->|"C / C++"| N6["clang-format"]
    M -->|"HTML / CSS"| N7["prettier / biome"]
    M -->|"Fallback"| O["Normalized Clean Code"]

    N1 --> P["wl-copy / xclip Clipboard"]
    N2 --> P
    N3 --> P
    N4 --> P
    N5 --> P
    N6 --> P
    N7 --> P
    O --> P
    P --> Q["πŸ”” Desktop Notification"]
Loading

Supported Formatters

Language Primary Formatter Fallback Formatter Offline / Auto-fallback
Python ruff (bundled in venv) β€” βœ… Guaranteed
Rust rustfmt β€” βœ… Graceful passthrough
Go gofmt β€” βœ… Graceful passthrough
JavaScript / TypeScript / JSON biome prettier βœ… Graceful passthrough
Bash / Shell shfmt β€” βœ… Graceful passthrough
C / C++ clang-format β€” βœ… Graceful passthrough
HTML / CSS prettier biome βœ… Graceful passthrough

πŸ’» Desktop Compatibility Matrix

Environment Display Protocol Capture Backend Status
Ubuntu 24.04 / 26.04 (GNOME 46+) Wayland XDG Desktop Portal (jeepney) 🟒 Native
Sway / Hyprland Wayland slurp + grim 🟒 Supported
Ubuntu / Debian (GNOME / XFCE) X11 gnome-screenshot / xclip 🟒 Supported
KDE Plasma Wayland / X11 Portal / spectacle 🟑 Compatible

πŸ› οΈ Project Structure

codesnap/
β”œβ”€β”€ pyproject.toml        # Modern build metadata & dependency manifest (hatchling)
β”œβ”€β”€ codesnap.py           # Compatibility proxy CLI entrypoint
β”œβ”€β”€ setup.sh              # First-time installation & hotkey registration script
β”œβ”€β”€ localupdate.sh        # Fast in-place upgrade script for existing installs
β”œβ”€β”€ src/codesnap/
β”‚   β”œβ”€β”€ __init__.py       # Package metadata & version
β”‚   β”œβ”€β”€ cli.py            # CLI entrypoint & pipeline orchestration
β”‚   β”œβ”€β”€ capture.py        # Wayland XDG Desktop Portal & slurp capture engine
β”‚   β”œβ”€β”€ image.py          # Dark theme polarity detection & image enhancement
β”‚   β”œβ”€β”€ ocr.py            # Tesseract OCR engine with uniform code tuning
β”‚   β”œβ”€β”€ cleaner.py        # Token-safe character normalization
β”‚   β”œβ”€β”€ languages.py      # Heuristic language pattern detection
β”‚   β”œβ”€β”€ formatters.py     # Rust-based Ruff code formatter integration
β”‚   β”œβ”€β”€ clipboard.py      # wl-copy / xclip & desktop notification handlers
β”‚   └── exceptions.py     # Typed domain exceptions
└── tests/                # 37 automated unit tests
    β”œβ”€β”€ test_capture.py
    β”œβ”€β”€ test_cleaner.py
    β”œβ”€β”€ test_cli.py
    β”œβ”€β”€ test_formatters.py
    β”œβ”€β”€ test_image.py
    β”œβ”€β”€ test_languages.py
    └── test_ocr.py

πŸ§ͺ Development & Quality Assurance

All features and bug fixes are accompanied by automated tests:

# Run unit test suite (37 tests)
uv run pytest

# Check code formatting & style with Ruff
uv run ruff check .
uv run ruff format --check .

# Auto-format codebase
uv run ruff format .

πŸ“œ Changelog

Detailed release notes and migration guides are documented in CHANGELOG.md.


πŸ—‘οΈ Uninstall

rm -rf ~/.local/share/codesnap
rm ~/.local/bin/codesnap

Then remove the custom shortcut in Settings β†’ Keyboard β†’ Keyboard Shortcuts β†’ Custom Shortcuts.


vibe coded by Aswin

About

Instant offline code extractor for Linux (Wayland & GNOME) with smart OCR and multi-language auto-formatting

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages