pi-code-copy
v0.1.2
Published
Clickable copy button on every code block in Pi, plus clean code block rendering without ``` fences
Maintainers
Readme
pi-code-copy
A copy button on every code block in Pi, and code blocks that look like code instead of raw Markdown.
── python ⧉ copy ──────────────────────────────── #1 ──
def hello():
print("Hello, world!")
──────────────────────────────────────────────────────────Without it, Pi prints code blocks with literal ```python fences and a two-space indent, so
selecting code with the mouse picks up both.
Features
- Click
⧉ copyon any code block — in the last answer or anywhere in the history — to copy exactly the code: no fences, no indent, no terminal wrapping. - Keyboard:
Alt+1…Alt+9copy block N of the last answer;/cc [N]does the same (/ccwithout a number opens a picker when there are several blocks). - Clean rendering: a titled rule with the language and block number, unindented, syntax-highlighted code, and a closing rule.
- A grey
✓ Copiedappears next to the button for two seconds — no popups. - Copies through Pi's own clipboard (
wl-copy,xclip,xsel,pbcopy, …) and sends the same text to the terminal as OSC 52, so apps that keep their own clipboard (for example VS Code running under XWayland) get it too. - No dependencies, no network access, never executes anything.
Install
pi install npm:pi-code-copyThen restart Pi.
Requirements
- Clickable button: Pi's fullscreen UI (
/settings→ TUI mode → fullscreen, or"tuiMode": "fullscreen"in~/.pi/agent/settings.json) and a terminal with OSC 8 hyperlinks (Konsole, VS Code, kitty, WezTerm, iTerm2, GNOME Terminal and most modern terminals). In the regular UI the terminal owns the mouse, so the button is hidden andAlt+N//ccare used instead. - Linux on Wayland: install
wl-clipboard(wl-copy). Pi needs it for every clipboard action; without it this extension falls back to OSC 52, which not every terminal honours.
How it works
- Pi has no public API for code block rendering, so the extension wraps
Markdown.prototype.renderTokenforcodetokens only. If a Pi update changes that method, rendering silently falls back to Pi's default and keyboard copy keeps working. - On Wayland the extension runs
wl-copy --type text/plain;charset=utf-8itself. Plainwl-copy(what Pi's own clipboard helper runs) guesses the MIME type from the content and offers only that type: multi-line Python can come out asapplication/x-ruby, and editors, VS Code and Klipper then paste the previous clipboard instead. - The button is an OSC 8 link
pi-copy:<hash>. In fullscreen mode Pi handles link clicks itself; the extension wraps that handler sopi-copy:links copy the block and every other link opens as usual.
Tested with Pi 0.87.1.
Development
npm test # self-check of the code block parser
pi -e ./extensions/code-copy/index.tsLicense
MIT
