From 0da972ce7efc10f7e43400b7d048139beaf6fff7 Mon Sep 17 00:00:00 2001 From: Yannik Schmidt Date: Fri, 24 Apr 2026 15:50:33 +0200 Subject: [PATCH] feat: add binary cat check --- binary_checker.py | 43 +++++++++++++++++++++++++++++++++++++++++++ zshrc | 31 +++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100755 binary_checker.py diff --git a/binary_checker.py b/binary_checker.py new file mode 100755 index 0000000..64d6dbb --- /dev/null +++ b/binary_checker.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 +import sys + +def is_binary(path, chunk_size=1024): + try: + with open(path, 'rb') as f: + chunk = f.read(chunk_size) + if not chunk: + return False # empty file → treat as text + + # If there are null bytes, it's very likely binary + if b'\x00' in chunk: + return True + + # Define what we consider "text" bytes + text_bytes = bytearray(range(32, 127)) + b'\n\r\t\f\b' + + # Count non-text bytes + nontext = sum(byte not in text_bytes for byte in chunk) + + # If more than 30% are non-text, treat as binary + return (nontext / len(chunk)) > 0.3 + + except Exception: + # On error (e.g., file not readable), treat as binary + return True + + +def main(): + if len(sys.argv) != 2: + print(f"Usage: {sys.argv[0]} ", file=sys.stderr) + sys.exit(1) + + path = sys.argv[1] + + if is_binary(path): + sys.exit(1) + else: + sys.exit(0) + + +if __name__ == "__main__": + main() diff --git a/zshrc b/zshrc index 7630594..3e9eaf2 100644 --- a/zshrc +++ b/zshrc @@ -230,3 +230,34 @@ alias connect_synology="ssh -f -o ExitOnForwardFailure=yes -i ~/.ssh/sheppy-mast ths_ssh="ssh -f -o ExitOnForwardFailure=yes -i .ssh/sheppy-master -L 8000:host.docker.internal:22 root@172.16.1.4 sleep 3600 && ssh cheffe@localhost -p 8000" #trap ctrl_c INT; function ctrl_c() {}; alias sss='ssh root@192.168.1.48 -t "systemctl suspend; exit"' + +cat() { + + # no args + if [[ $# -eq 0 ]]; then + command cat + return + fi + + for file in "$@"; do + + # Skip non-regular files (pipes, etc.) + if [[ ! -f "$file" ]]; then + command cat "$file" + continue + fi + + ~/.config/binary_checker.py "$file" + if [[ $? -ne 0 ]] then + echo -n "'$file' looks a bit binary, you sure you wanna cat that? [y/N] " + read reply + + if [[ ! "$reply" =~ ^[Yy]$ ]]; then + echo "Skipping '$file'" + continue + fi + fi + + command cat "$file" + done +}