From ebbcc63d7ece9920cd09d0e556635f873d5123bb Mon Sep 17 00:00:00 2001 From: Marc Pervaz Boocha Date: Sat, 6 Jun 2026 13:31:45 +0530 Subject: Added OSC 52. http.server is also good enough --- .local/bin/http-static | 42 ---------------------------- .local/bin/osc-copy | 3 ++ .local/bin/osc-paste | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++ .profile | 2 +- .zfunc/bell | 3 -- .zfunc/e | 3 -- .zfunc/p | 3 -- .zfunc/v | 4 --- .zfunc/zsh-completions | 2 +- .zshrc | 3 +- 10 files changed, 80 insertions(+), 59 deletions(-) delete mode 100755 .local/bin/http-static create mode 100755 .local/bin/osc-copy create mode 100755 .local/bin/osc-paste delete mode 100644 .zfunc/bell delete mode 100644 .zfunc/e delete mode 100644 .zfunc/p delete mode 100644 .zfunc/v diff --git a/.local/bin/http-static b/.local/bin/http-static deleted file mode 100755 index cd5ed09..0000000 --- a/.local/bin/http-static +++ /dev/null @@ -1,42 +0,0 @@ -#!/usr/bin/env python3 - -from argparse import ArgumentParser -from collections.abc import Sequence -from pathlib import Path -from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer -from functools import partial - - -def serve(bind: str, port: int, directory: Path): - handler = partial(SimpleHTTPRequestHandler, directory=directory) - - with ThreadingHTTPServer((bind, port), handler) as httpd: - print(*httpd.socket.getsockname()) - try: - httpd.serve_forever() - except KeyboardInterrupt: - return - - -def main(argv: Sequence[str] | None = None) -> None: - parser = ArgumentParser() - _ = parser.add_argument( - "-b", "--bind", metavar="ADDRESS", default="", help="bind to this address" - ) - _ = parser.add_argument( - "-p", "--port", default=8080, type=int, help="bind to this port" - ) - _ = parser.add_argument( - "directory", - type=Path, - default=Path.cwd(), - nargs="?", - help="serve this directory", - ) - args = parser.parse_args(argv) - - serve(**vars(args)) - - -if __name__ == "__main__": - main() diff --git a/.local/bin/osc-copy b/.local/bin/osc-copy new file mode 100755 index 0000000..5ffbb94 --- /dev/null +++ b/.local/bin/osc-copy @@ -0,0 +1,3 @@ +#!/bin/sh + +printf "\033]52;c;%s\007" "$(base64 | tr -d '\r\n ')" > /dev/tty diff --git a/.local/bin/osc-paste b/.local/bin/osc-paste new file mode 100755 index 0000000..1580627 --- /dev/null +++ b/.local/bin/osc-paste @@ -0,0 +1,74 @@ +#!/usr/bin/env python3 + +import sys +import tty +import termios +import base64 +import os + + +def osc52_paste() -> str | None: + fd = os.open("/dev/tty", os.O_RDWR) + + old = termios.tcgetattr(fd) + _ = tty.setraw(fd) + + try: + _ = os.write(fd, b"\x1b]52;c;?\x1b\\") + + buf = bytearray() + + while True: + b = os.read(fd, 1) + if not b: + continue + + buf += b + + if buf.endswith(b"\x07") or buf.endswith(b"\x1b\\"): + break + + if not buf.startswith(b"\x1b]"): + return None + + if buf.endswith(b"\x07"): + inner = buf[2:-1] + elif buf.endswith(b"\x1b\\"): + inner = buf[2:-2] + else: + return None + + parts = inner.split(b";", 2) + if len(parts) != 3: + return None + + op, _, b64 = parts + if bytes(op) != b"52": + return None + + try: + data = base64.b64decode(b64, validate=False) + return data.decode(errors="replace") + except Exception: + return None + + finally: + termios.tcsetattr(fd, termios.TCSADRAIN, old) + os.close(fd) + + +def main() -> None | int: + try: + clipboard_text = osc52_paste() + if clipboard_text is not None: + _ = sys.stdout.write(clipboard_text) + _ = sys.stdout.flush() + else: + print("Error: Could not read clipboard via OSC 52.", file=sys.stderr) + return 1 + except KeyboardInterrupt: + return 1 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/.profile b/.profile index e64b4ef..fb8138f 100644 --- a/.profile +++ b/.profile @@ -5,7 +5,7 @@ prepend_path () { *:"$1":*) ;; *) - PATH="${PATH:+:$PATH}$1" + PATH="$1${PATH:+:$PATH}" esac } diff --git a/.zfunc/bell b/.zfunc/bell deleted file mode 100644 index cc01adc..0000000 --- a/.zfunc/bell +++ /dev/null @@ -1,3 +0,0 @@ -#!zsh - -echoti bel diff --git a/.zfunc/e b/.zfunc/e deleted file mode 100644 index c714d76..0000000 --- a/.zfunc/e +++ /dev/null @@ -1,3 +0,0 @@ -#!zsh - -"${EDITOR:-ed}" "$@" diff --git a/.zfunc/p b/.zfunc/p deleted file mode 100644 index 59115b7..0000000 --- a/.zfunc/p +++ /dev/null @@ -1,3 +0,0 @@ -#!zsh - -${PAGER:-less} "$@" diff --git a/.zfunc/v b/.zfunc/v deleted file mode 100644 index a223795..0000000 --- a/.zfunc/v +++ /dev/null @@ -1,4 +0,0 @@ -#!zsh - -"${VISUAL:-${EDITOR:-vi}}" "$@" - diff --git a/.zfunc/zsh-completions b/.zfunc/zsh-completions index e461417..dd83145 160000 --- a/.zfunc/zsh-completions +++ b/.zfunc/zsh-completions @@ -1 +1 @@ -Subproject commit e461417f4e20b20f84b9d48a7acd582145723929 +Subproject commit dd83145816fe2d90b1ab4154ed528050e94ac5e3 diff --git a/.zshrc b/.zshrc index 2db030e..598a1b5 100644 --- a/.zshrc +++ b/.zshrc @@ -54,8 +54,6 @@ autoload -Uz run-help (( ${+aliases[run-help]} )) && unalias run-help alias help=run-help -autoload -Uz e v p bell - zstyle ':vcs_info:*' check-for-changes true zstyle ':vcs_info:*' get-revision true zstyle ':vcs_info:*' formats "(%s)-[%b]%m%u%c" @@ -95,3 +93,4 @@ if command -v uvx > /dev/null then eval "$(uvx --generate-shell-completion zsh)" fi + -- cgit v1.3.1