Skip to content

Instantly share code, notes, and snippets.

@pedramamini
Created March 13, 2025 00:50
Show Gist options
  • Save pedramamini/8a6910df017893201644522ca2fabd1b to your computer and use it in GitHub Desktop.
Save pedramamini/8a6910df017893201644522ca2fabd1b to your computer and use it in GitHub Desktop.
#!/bin/bash
# pop-working: Pops the top block of multiline content from the "working" file.
# It reads lines until it finds a line with at least 3 hyphens (e.g. "------").
# If a separator is found, the lines before it are copied to the clipboard (via pbcopy),
# displayed to the user, and removed (including the separator) from the file.
# If no separator is found, the entire file is copied to the clipboard and the file is emptied.
# If the file exists but is empty, the script exits with a message.
# Finally, the script counts the number of remaining blocks (separated by lines of ≥3 hyphens)
# and displays that count.
WORKING_FILE="working"
# Check if the working file exists.
if [ ! -f "$WORKING_FILE" ]; then
echo "Error: '$WORKING_FILE' does not exist."
exit 1
fi
# Check if the file is empty.
if [ ! -s "$WORKING_FILE" ]; then
echo "No pending work on the stack"
exit 0
fi
popped=""
remaining=""
separator_found=0
# Process the file line by line.
while IFS= read -r line || [ -n "$line" ]; do
# Trim whitespace for separator matching.
trimmed_line=$(echo "$line" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//')
if [ $separator_found -eq 0 ] && echo "$trimmed_line" | grep -Eq '^-{3,}$'; then
separator_found=1
continue # Skip the separator line.
fi
if [ $separator_found -eq 0 ]; then
popped="${popped}${line}"$'\n'
else
remaining="${remaining}${line}"$'\n'
fi
done < "$WORKING_FILE"
# If no separator was found, treat the entire file as the block to pop.
if [ $separator_found -eq 0 ]; then
popped=$(cat "$WORKING_FILE")
remaining=""
fi
# Use Perl to trim leading and trailing whitespace (including newlines) from the popped content.
popped_trimmed=$(echo "$popped" | perl -0777 -pe 's/^\s+//; s/\s+\z//')
# Copy the trimmed content to the clipboard (OSX pbcopy)
echo -n "$popped_trimmed" | pbcopy
# Function to count blocks in the remaining content.
# A block is defined as a non-empty section separated by lines that are at least 3 hyphens.
count_blocks() {
local content="$1"
local block_count=0
local block=""
# Remove leading/trailing whitespace from the entire content.
local trimmed=$(echo "$content" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//')
# Process each line.
while IFS= read -r line || [ -n "$line" ]; do
local tline=$(echo "$line" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//')
if echo "$tline" | grep -Eq '^-{3,}$'; then
# When a separator is encountered, if there is a non-empty block, count it.
if [ -n "$(echo "$block" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//')" ]; then
block_count=$((block_count+1))
block=""
fi
else
block="${block}${line}"$'\n'
fi
done <<< "$trimmed"
# Count any remaining non-empty block.
if [ -n "$(echo "$block" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//')" ]; then
block_count=$((block_count+1))
fi
echo "$block_count"
}
# Count the remaining blocks.
blocks_remaining=0
if [ -n "$remaining" ]; then
blocks_remaining=$(count_blocks "$remaining")
fi
# Display the message with the block count.
echo "$blocks_remaining blocks remaining, popped to clipboard:"
echo "$popped_trimmed"
# Overwrite the working file with the remaining content.
echo -n "$remaining" > "$WORKING_FILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment