DEV Community

Cover image for I put gofmt, Ruff and rustfmt in a browser tab so my code screenshots format themselves
Yashaswi Kumar Mishra
Yashaswi Kumar Mishra

Posted on Edited on AI-assisted

I put gofmt, Ruff and rustfmt in a browser tab so my code screenshots format themselves

every code screenshot i posted had the same three problems. it was blurry once twitter compressed it, it was indented however the file happened to be, and it never looked like my actual editor.

so i built codeshots. you paste code, and before it draws anything, it runs the formatter that language's own community uses. gofmt for go, ruff for python, rustfmt for rust, clang-format for c and c++, prettier for the web stack. all of it runs in your browser tab. nothing gets uploaded.

a typescript retry function in a vs code window, on a generated canvas

this post is about how that works, plus the few decisions that made the rest of the tool fall into place.

the obvious approach, and why it's wrong

the easy way to "format" pasted code is to guess: count braces, reindent, call it done. most code image tools either do that or don't format at all, so whatever you paste is what your audience sees.

guessing breaks the moment the code is real. go has opinions about alignment, python's indentation is the syntax, and a reindent that's slightly wrong is worse than no reindent, because now there's something wrong in a picture with your name on it.

so the rule i went with: either the real formatter parses it, or we don't touch it. a language with no formatter is marked "highlight only" and left exactly as pasted.

real formatters, in a browser tab

the nice thing about 2026 is that most of these tools already compile to webassembly. the @wasm-fmt packages ship gofmt, ruff, clang-format, zig fmt, stylua, taplo and dart format as wasm builds. prettier is plain javascript, so it just runs.

each formatter loads once, the first time someone actually formats that language, and never before:

const wasmReady = new Map<string, Promise<unknown>>();

function once<T>(key: string, load: () => Promise<T>): Promise<T> {
  if (!wasmReady.has(key)) wasmReady.set(key, load());
  return wasmReady.get(key) as Promise<T>;
}

async function gofmt() {
  const mod = await once("gofmt", async () => {
    const m = await import("@wasm-fmt/gofmt/web");
    await m.default("/wasm/gofmt.wasm");
    return m;
  });
  return (code: string) => mod.format(code);
}
✨ อัปเดตล่าสุด: pixperk/i put gofmt ruff and rustfmt in a browser tab so my code screenshots format themselves mn - Kapook Update 2025 ✨ อัปเดตล่าสุด: pixperk/i put gofmt ruff and rustfmt in a browser tab so my code screenshots format themselves mn - Kapook Update 2025

two details that bit me:

  • i serve the .wasm files from my own /wasm path and pass the url explicitly. bundlers treat .wasm imports differently in dev and in a production build, and an explicit url makes that stop mattering.
  • rustfmt is the heavy one. the wasm is about 6 MB, so it's shipped brotli-compressed at about 1.3 MB and decompressed in the browser. it also runs under a wasi shim, so unlike the others it's async every time. and you have to pass edition: "2021", because rustfmt's default is 2015, where async, await and dyn are parse errors.

the result is a format button that does exactly what go fmt would have done on your machine, without a server ever seeing your code.

the picture is svg, not a screenshot

the second decision: there's no "take a screenshot of the dom" step anywhere. every font codeshots ships is true monospace with a known advance width, so the position of every glyph is multiplication, not measurement. one function lays everything out as arithmetic and emits svg.

that means:

  • the preview is the export, just drawn at a different scale. no 1px drift, no missing webfont, no dropped shadow.
  • the text inside stays real text. it scales to any size and opens in figma as editable type.
  • a 6x png has no pixels to run out of.

it also draws six real editors, and they differ where the editors actually differ. neovim gets relative line numbers, tildes and a powerline statusline:

neovim with relative line numbers, tildes past the buffer and a powerline statusline

code, terminal and screenshot panes

once the renderer was just math, adding things around the code got cheap. you can split the window into panes: code beside code, a terminal pane stacked under it, or a screenshot pane next to the code that produced it.

a deploy script beside a picture of what it printed, in one editor window

a terminal pane showing a deploy

two languages side by side, on an ultrawide:

go and sql side by side on an ultrawide monitor

screenshots, and redaction that actually redacts

you can also paste any image instead of code. crop it, trim the edges, put it on a laptop, and mark it up with boxes, arrows, labels and highlights. marks are stored in normalised 0 to 1 coordinates against the image, so an arrow stays on its target when you resize or stretch.

the codeshots landing page, pasted in as a screenshot and dressed in a browser frame

a screenshot on a desktop display, with a box, an arrow and a label on it

the one mark that isn't a mark is redact. a black box drawn over an image is a covering, not a redaction: the original pixels are still in the file, and anyone can pull them back out. that has leaked real secrets more than once. so redact re-encodes the image with the area filled solid, and those pixels are gone. no blur or pixelation either, since pixelated monospace text has been brute-forced back before. the only undo is "revert", which reloads the image as it arrived.

a share link with no database

there's no backend storing compositions. the whole thing, every pane's code, theme, marks and settings, gets deflated with fflate and base64url'd into the url. a 40-line snippet packs to roughly 700 characters. links can't rot, need no account, and your code only ever lives on your machine and whoever you send it to.

the honest caveat

a picture of code can't be copied, searched or read by a screen reader. in a readme or docs you control, use a real code block, like the one above. images are for feeds, slides and messages. for those cases codeshots also gives you an embed, where the code stays real text with a copy button.

try it

codeshots.app is free, no account, and open source. six editor looks (vs code, jetbrains, xcode, zed, sublime, neovim), 242 languages, 13 formatters.

a snippet with one line focused and a diff marked around it

swiftui code on a phone screen, reflected in a glossy floor

i'd love to know what breaks, and which formatter you'd want added next. elixir is top of my list.

a few more, if you want to see

every one of these came straight out of codeshots, nothing edited afterwards.

a code image made with codeshots

a code image made with codeshots

a code image made with codeshots

a code image made with codeshots

a code image made with codeshots

a code image made with codeshots

a code image made with codeshots

a code image made with codeshots

a code image made with codeshots

a code image made with codeshots

Top comments (0)