feat: add binary cat check

This commit is contained in:
2026-04-24 15:50:33 +02:00
parent e89098ec0a
commit 0da972ce7e
2 changed files with 74 additions and 0 deletions

43
binary_checker.py Executable file
View File

@@ -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>", 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()

31
zshrc
View File

@@ -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
}