Skip to content

Instantly share code, notes, and snippets.

@surajsharma
Created November 23, 2025 18:23
Show Gist options
  • Select an option

  • Save surajsharma/7925ced48b31c1138ffa024b4e7f3655 to your computer and use it in GitHub Desktop.

Select an option

Save surajsharma/7925ced48b31c1138ffa024b4e7f3655 to your computer and use it in GitHub Desktop.
#!/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