-
-
Save wrgoldstein/c4b3c6351344a88189195574cef28202 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 | |
| # Create frags directory if it doesn't exist | |
| FRAG_DIR="$HOME/frags" | |
| mkdir -p "$FRAG_DIR" | |
| # Generate timestamp for filename | |
| TIMESTAMP=$(date +"%Y%m%d_%H%M%S") | |
| FILENAME="$FRAG_DIR/frag_${TIMESTAMP}.txt" | |
| # Get clipboard content and save to file | |
| pbpaste > "$FILENAME" | |
| # Check if clipboard was empty | |
| if [ ! -s "$FILENAME" ]; then | |
| echo "Warning: Clipboard was empty or could not be read" | |
| rm "$FILENAME" | |
| exit 1 | |
| fi | |
| # Get file size and line count for feedback | |
| FILE_SIZE=$(wc -c < "$FILENAME" | tr -d ' ') | |
| LINE_COUNT=$(wc -l < "$FILENAME" | tr -d ' ') | |
| echo "Fragment saved to: $FILENAME" | |
| echo "Size: $FILE_SIZE bytes, Lines: $LINE_COUNT" | |
| # Show first few lines as preview | |
| echo "---Preview---" | |
| head -n 3 "$FILENAME" | |
| if [ "$LINE_COUNT" -gt 3 ]; then | |
| echo "..." | |
| fi |
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 | |
| FRAG_DIR="$HOME/frags" | |
| if [ ! -d "$FRAG_DIR" ] || [ -z "$(ls -A "$FRAG_DIR" 2>/dev/null)" ]; then | |
| echo "No frags found in $FRAG_DIR" | |
| exit 1 | |
| fi | |
| # Delete most recent frag | |
| if [ "$1" = "-d" ] || [ "$1" = "--delete-last" ]; then | |
| latest=$(ls -t "$FRAG_DIR"/*.txt 2>/dev/null | head -1) | |
| if [ -n "$latest" ]; then | |
| preview=$(head -c 60 "$latest" | tr '\n' ' ') | |
| echo "Deleting: $(basename "$latest")" | |
| echo "Preview: $preview..." | |
| rm "$latest" | |
| fi | |
| exit 0 | |
| fi | |
| # Helper function to list frags (used for reload) | |
| list_frags() { | |
| ls -t "$FRAG_DIR"/*.txt 2>/dev/null | while read -r file; do | |
| filename=$(basename "$file") | |
| if [[ "$filename" =~ frag_([0-9]{4})([0-9]{2})([0-9]{2})_([0-9]{2})([0-9]{2})([0-9]{2})\.txt ]]; then | |
| formatted_date="${BASH_REMATCH[1]}-${BASH_REMATCH[2]}-${BASH_REMATCH[3]} ${BASH_REMATCH[4]}:${BASH_REMATCH[5]}" | |
| else | |
| formatted_date="$filename" | |
| fi | |
| preview=$(head -c 60 "$file" | tr '\n' ' ' | sed 's/ */ /g') | |
| echo "$file|$formatted_date|$preview" | |
| done | |
| } | |
| export -f list_frags | |
| export FRAG_DIR | |
| selected=$(list_frags | fzf \ | |
| --delimiter='|' \ | |
| --with-nth=2,3 \ | |
| --preview='bat --style=plain --color=always {1}' \ | |
| --preview-window='right:60%:wrap' \ | |
| --height='100%' \ | |
| --layout=reverse \ | |
| --border \ | |
| --header='Enter: copy | ctrl-d: delete | ctrl-o: open | Esc: cancel' \ | |
| --prompt='Select frag > ' \ | |
| --bind='ctrl-d:execute-silent(rm {1})+reload(list_frags)' \ | |
| --bind='ctrl-o:execute(${EDITOR:-vim} {1} < /dev/tty > /dev/tty)+reload(list_frags)') | |
| if [ -n "$selected" ]; then | |
| file_path=$(echo "$selected" | cut -d'|' -f1) | |
| cat "$file_path" | pbcopy | |
| echo "Copied to clipboard: $(basename "$file_path")" | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment