Created
November 23, 2025 18:23
-
-
Save surajsharma/7925ced48b31c1138ffa024b4e7f3655 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| LAST_HASH="" | |
| # Detect clipboard tool | |
| if command -v wl-paste &> /dev/null; then | |
| CLIP_COPY="wl-copy" | |
| CLIP_PASTE="wl-paste" | |
| elif command -v xsel &> /dev/null; then | |
| CLIP_COPY="xsel --clipboard --input" | |
| CLIP_PASTE="xsel --clipboard --output" | |
| elif command -v xclip &> /dev/null; then | |
| CLIP_COPY="xclip -selection clipboard" | |
| CLIP_PASTE="xclip -o -selection clipboard" | |
| else | |
| echo "ERROR: Install xsel, xclip, or wl-clipboard" | |
| exit 1 | |
| fi | |
| is_code() { | |
| local text="$1" | |
| echo "$text" | grep -qE '(function|class|def|public|private|void|int|const|let|var|import|include|fn |impl |struct |package )' && return 0 | |
| echo "$text" | grep -qE '(\{|\}|;|=>|->|::).*(\{|\}|;|=>|->|::)' && return 0 | |
| echo "$text" | grep -qE '^[[:space:]]{2,}' && [ $(echo "$text" | wc -l) -gt 3 ] && return 0 | |
| return 1 | |
| } | |
| minify() { | |
| # Remove all newlines and collapse whitespace to single spaces | |
| tr '\n' ' ' | sed -E 's/[[:space:]]+/ /g; s/^[[:space:]]+//; s/[[:space:]]+$//' | |
| } | |
| echo "π Code minifier daemon started (using $CLIP_PASTE)" | |
| echo "Copy some code to test..." | |
| while true; do | |
| CLIP=$($CLIP_PASTE 2>/dev/null) | |
| if [ -z "$CLIP" ]; then | |
| sleep 0.5 | |
| continue | |
| fi | |
| CURRENT_HASH=$(echo -n "$CLIP" | md5sum | cut -d' ' -f1) | |
| if [ "$CURRENT_HASH" = "$LAST_HASH" ]; then | |
| sleep 0.5 | |
| continue | |
| fi | |
| LINES=$(echo "$CLIP" | wc -l) | |
| echo "π New clipboard content (${#CLIP} chars, $LINES lines)" | |
| if is_code "$CLIP"; then | |
| echo "β Code detected! Minifying..." | |
| MINIFIED=$(echo "$CLIP" | minify) | |
| printf "%s" "$MINIFIED" | $CLIP_COPY | |
| LAST_HASH=$(echo -n "$MINIFIED" | md5sum | cut -d' ' -f1) | |
| echo "β MINIFIED: $LINES lines β 1 line (${#MINIFIED} chars)" | |
| else | |
| echo "βΉοΈ Not code, skipping" | |
| LAST_HASH="$CURRENT_HASH" | |
| fi | |
| sleep 0.5 | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment