diff options
| author | elioat <elioat@tilde.institute> | 2025-09-14 15:11:00 -0400 |
|---|---|---|
| committer | elioat <elioat@tilde.institute> | 2025-09-14 15:11:00 -0400 |
| commit | 765947f0a2d17f5e2474de187a498bc8b90f7ea8 (patch) | |
| tree | 3557d0581bb4454c246ec2e65b8d1fec932c1dff /bash | |
| parent | 0cb7de59489c8e0850bca495d91c4865c154eb28 (diff) | |
| download | tour-765947f0a2d17f5e2474de187a498bc8b90f7ea8.tar.gz | |
*
Diffstat (limited to 'bash')
| -rw-r--r-- | bash/snip/.gitignore | 22 | ||||
| -rw-r--r-- | bash/snip/README.md | 128 | ||||
| -rwxr-xr-x | bash/snip/setup | 58 | ||||
| -rwxr-xr-x | bash/snip/snip | 369 |
4 files changed, 577 insertions, 0 deletions
diff --git a/bash/snip/.gitignore b/bash/snip/.gitignore new file mode 100644 index 0000000..6a5adb6 --- /dev/null +++ b/bash/snip/.gitignore @@ -0,0 +1,22 @@ +# Git ignore file for snippets directory +# This file should be copied to ~/.snippets/.gitignore + +# OS generated files +.DS_Store +.DS_Store? +._* +.Spotlight-V100 +.Trashes +ehthumbs.db +Thumbs.db + +# Editor files +*~ +*.swp +*.swo +.vscode/ +.idea/ + +# Temporary files +*.tmp +*.temp diff --git a/bash/snip/README.md b/bash/snip/README.md new file mode 100644 index 0000000..2165c2c --- /dev/null +++ b/bash/snip/README.md @@ -0,0 +1,128 @@ +# snip - Minimal POSIX Snippet Manager + +A simple, POSIX-compatible snippet manager that follows the Unix philosophy. Store, search, and quickly access code snippets with cross-platform clipboard support. + +## Features + +- **POSIX Compatible**: Works on any Unix-like system +- **Git Integration**: Snippets directory is git-tracked for version control +- **Cross-Platform**: Supports macOS and Linux clipboard operations +- **Unix Philosophy**: Pipes in/out, does one thing well +- **Minimal**: Single script, no dependencies beyond standard Unix tools + +## Installation + +1. Clone or download this repository +2. Run the setup script: + ```bash + ./setup + ``` + +This will: +- Create `~/.snippets` directory +- Initialize git repository +- Set up proper `.gitignore` +- Add example snippets + +## Usage + +### Basic Commands + +```bash +# List all snippets +./snip list + +# Search snippets +./snip search "hello world" + +# Interactive selection (copies to clipboard) +./snip select + +# Add new snippet +./snip add bash hello 'echo "Hello, World!"' + +# Initialize snippets directory +./snip init +``` + +### Directory Structure + +Snippets are organized in `~/.snippets/`: + +``` +~/.snippets/ +├── .git/ +├── .gitignore +├── bash/ +│ ├── hello-world.txt +│ └── loop-example.txt +├── python/ +│ └── fibonacci.txt +└── git/ + └── commit-message.txt +``` + +### Adding Snippets + +```bash +# Add to existing category +./snip add bash "loop-example" 'for i in {1..10}; do echo $i; done' + +# Create new category +./snip add python "fibonacci" 'def fib(n): return n if n <= 1 else fib(n-1) + fib(n-2)' +``` + +### Searching and Selecting + +```bash +# Search and display results +./snip search "fibonacci" + +# Interactive selection with search +./snip select "bash" + +# Interactive selection of all snippets +./snip select +``` + +## Clipboard Support + +- **macOS**: Uses `pbcopy` +- **Linux**: Uses `xclip` or `xsel` (whichever is available) + +## Git Integration + +The snippets directory is a git repository, allowing you to: +- Track changes to snippets +- Sync across machines +- Collaborate with others +- Maintain version history + +## Design Principles + +- **Single Responsibility**: One script, one purpose +- **Composability**: Pipes in/out with other Unix tools +- **Simplicity**: Minimal code, maximum functionality +- **Portability**: POSIX shell, no external dependencies +- **Immutability**: Snippets are files, not database entries + +## Examples + +```bash +# Quick workflow +./snip search "commit" | head -5 +./snip select "git" + +# Pipe snippets to other tools +./snip list | grep "python" | xargs cat + +# Add snippet from file +./snip add bash "my-script" "$(cat my-script.sh)" +``` + +## Requirements + +- POSIX-compatible shell (`/bin/sh`) +- `find`, `grep`, `sort` (standard Unix tools) +- `git` (for version control) +- Clipboard utility: `pbcopy` (macOS) or `xclip`/`xsel` (Linux) diff --git a/bash/snip/setup b/bash/snip/setup new file mode 100755 index 0000000..7f37919 --- /dev/null +++ b/bash/snip/setup @@ -0,0 +1,58 @@ +#!/bin/sh + +# Setup script for snip snippet manager +# This script initializes the ~/.snippets directory with proper git setup + +set -e + +SNIPPETS_DIR="${HOME}/.snippets" +SCRIPT_DIR="$(dirname "$0")" + +echo "Setting up snip snippet manager..." + +# Create snippets directory +mkdir -p "$SNIPPETS_DIR" + +# Copy .gitignore to snippets directory +cp "$SCRIPT_DIR/.gitignore" "$SNIPPETS_DIR/.gitignore" + +# Initialize git repository +cd "$SNIPPETS_DIR" +if [ ! -d ".git" ]; then + git init + echo "Initialized git repository in $SNIPPETS_DIR" +fi + +# Add .gitignore to git +git add .gitignore +git commit -m "Initial commit: Add .gitignore" || true + +# Create example snippets +mkdir -p "$SNIPPETS_DIR/bash" +mkdir -p "$SNIPPETS_DIR/git" + +# Add example bash snippet +cat > "$SNIPPETS_DIR/bash/hello-world.txt" << 'EOF' +#!/bin/bash +echo "Hello, World!" +EOF + +# Add example git snippet +cat > "$SNIPPETS_DIR/git/commit-message.txt" << 'EOF' +feat: add new feature + +- Detailed description of changes +- List of specific changes made +- Any breaking changes or considerations +EOF + +# Add example snippets to git +git add . +git commit -m "Add example snippets" || true + +echo "Setup complete!" +echo "Snippets directory: $SNIPPETS_DIR" +echo "Example usage:" +echo " $SCRIPT_DIR/snip list" +echo " $SCRIPT_DIR/snip select" +echo " $SCRIPT_DIR/snip add python 'fibonacci' 'def fib(n): return n if n <= 1 else fib(n-1) + fib(n-2)'" diff --git a/bash/snip/snip b/bash/snip/snip new file mode 100755 index 0000000..407bd74 --- /dev/null +++ b/bash/snip/snip @@ -0,0 +1,369 @@ +#!/bin/sh + +# snip - A little snippet manager +# Follows Unix philosophy: do one thing well, pipe in/out +# Examples of how to pipes things in and out +# ./snip list | grep "python" | xargs cat +# ./snip search "fibonacci" | head -5 +# ./snip select "git" +# ./snip add bash "my-script" "$(cat my-script.sh)" +# ./snip edit bash "my-script" --editor vim +# ./snip search "fibonacci" | head -5 | ./snip select + +set -e + +# Configuration +SNIPPETS_DIR="${HOME}/.snippets" +SCRIPT_DIR="$(dirname "$0")" + +# Colors for output (POSIX compatible) +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +NC='\033[0m' # No Color + +# Ensure snippets directory exists +ensure_snippets_dir() { + if [ ! -d "$SNIPPETS_DIR" ]; then + mkdir -p "$SNIPPETS_DIR" + echo "Created snippets directory: $SNIPPETS_DIR" + fi +} + +# Initialize git repository in snippets directory if it doesn't exist +init_git() { + if [ ! -d "$SNIPPETS_DIR/.git" ]; then + cd "$SNIPPETS_DIR" + git init + echo "Initialized git repository in $SNIPPETS_DIR" + fi +} + +# Cross-platform clipboard copy +copy_to_clipboard() { + local content="$1" + + # Detect OS and use appropriate clipboard command + case "$(uname -s)" in + Darwin*) + # macOS + printf '%s' "$content" | pbcopy + ;; + Linux*) + # Linux - try different clipboard utilities + if command -v xclip >/dev/null 2>&1; then + printf '%s' "$content" | xclip -selection clipboard + elif command -v xsel >/dev/null 2>&1; then + printf '%s' "$content" | xsel --clipboard --input + else + echo "Error: No clipboard utility found. Install xclip or xsel." + exit 1 + fi + ;; + *) + echo "Error: Unsupported operating system: $(uname -s)" + exit 1 + ;; + esac +} + +# List all snippet files +list_snippets() { + find "$SNIPPETS_DIR" -type f -name "*.txt" | sort +} + +# Search snippets by content or filename +search_snippets() { + local query="$1" + if [ -z "$query" ]; then + echo "Usage: $0 search <query>" + exit 1 + fi + + # Search in both filename and content + find "$SNIPPETS_DIR" -type f -name "*.txt" | while read -r file; do + if grep -l "$query" "$file" >/dev/null 2>&1 || basename "$file" | grep -q "$query"; then + echo "$file" + fi + done +} + +# Get editor command +get_editor() { + local editor="$1" + + if [ -n "$editor" ]; then + echo "$editor" + elif [ -n "$EDITOR" ]; then + echo "$EDITOR" + elif command -v nvim >/dev/null 2>&1; then + echo "nvim" + elif command -v mg >/dev/null 2>&1; then + echo "mg" + elif command -v micro >/dev/null 2>&1; then + echo "micro" + elif command -v vim >/dev/null 2>&1; then + echo "vim" + elif command -v nano >/dev/null 2>&1; then + echo "nano" + elif command -v vi >/dev/null 2>&1; then + echo "vi" + else + echo "Error: No editor found. Set EDITOR environment variable or specify editor." + exit 1 + fi +} + +# Add a new snippet +add_snippet() { + local category="$1" + local name="$2" + local content="$3" + local editor="$4" + + if [ -z "$category" ] || [ -z "$name" ]; then + echo "Usage: $0 add <category> <name> [content] [--editor <editor>]" + echo "Example: $0 add bash 'hello world' 'echo \"Hello, World!\"'" + echo "Example: $0 add bash 'hello world' --editor vim" + echo "Example: $0 add bash 'hello world' # Opens editor if no content provided" + exit 1 + fi + + local category_dir="$SNIPPETS_DIR/$category" + local snippet_file="$category_dir/$name.txt" + + # Create category directory if it doesn't exist + mkdir -p "$category_dir" + + # If content is provided, write it directly + if [ -n "$content" ]; then + printf '%s' "$content" > "$snippet_file" + echo "Added snippet: $snippet_file" + else + # Open editor to create snippet + local editor_cmd + editor_cmd=$(get_editor "$editor") + + # Create empty file first + touch "$snippet_file" + + # Open editor + $editor_cmd "$snippet_file" + + # Check if file has content + if [ -s "$snippet_file" ]; then + echo "Added snippet: $snippet_file" + else + echo "No content added. Removing empty file." + rm "$snippet_file" + exit 1 + fi + fi +} + +# Edit an existing snippet +edit_snippet() { + local category="$1" + local name="$2" + local editor="$3" + + if [ -z "$category" ] || [ -z "$name" ]; then + echo "Usage: $0 edit <category> <name> [--editor <editor>]" + echo "Example: $0 edit bash 'hello world'" + echo "Example: $0 edit bash 'hello world' --editor vim" + exit 1 + fi + + local snippet_file="$SNIPPETS_DIR/$category/$name.txt" + + if [ ! -f "$snippet_file" ]; then + echo "Snippet not found: $snippet_file" + exit 1 + fi + + # Get editor command + local editor_cmd + editor_cmd=$(get_editor "$editor") + + # Open editor + $editor_cmd "$snippet_file" + + echo "Updated snippet: $snippet_file" +} + +# Interactive snippet selection +select_snippet() { + local query="$1" + local snippets + + if [ -n "$query" ]; then + snippets=$(search_snippets "$query") + else + snippets=$(list_snippets) + fi + + if [ -z "$snippets" ]; then + echo "No snippets found." + exit 1 + fi + + # Create a simple selection menu + local count=0 + local selected_file="" + + echo "Available snippets:" + echo "$snippets" | while read -r file; do + count=$((count + 1)) + local relative_path="${file#$SNIPPETS_DIR/}" + local category=$(dirname "$relative_path") + local snippet_name=$(basename "$file" .txt) + echo "$count) [$category] $snippet_name" + done + + echo + printf "Select snippet (1-%d): " "$count" + read -r selection + + # Validate selection + if [ "$selection" -lt 1 ] || [ "$selection" -gt "$count" ]; then + echo "Invalid selection." + exit 1 + fi + + # Get the selected file + selected_file=$(echo "$snippets" | sed -n "${selection}p") + + # Copy to clipboard + local content + content=$(cat "$selected_file") + copy_to_clipboard "$content" + + echo "Copied to clipboard: $(basename "$selected_file" .txt)" +} + +# Show help +show_help() { + cat << EOF +snip - A little snippet manager + +USAGE: + $0 <command> [options] + +COMMANDS: + add <category> <name> [content] [--editor <editor>] Add a new snippet + edit <category> <name> [--editor <editor>] Edit an existing snippet + search <query> Search snippets by content or name + list List all snippets + select [query] Interactive snippet selection + init Initialize snippets directory and git repo + help Show this help message + +EXAMPLES: + $0 add bash hello 'echo "Hello, World!"' # Add with content + $0 add bash hello # Add with editor + $0 add bash hello --editor vim # Add with specific editor + $0 edit bash hello # Edit existing snippet + $0 edit bash hello --editor nano # Edit with specific editor + $0 search "hello world" # Search snippets + $0 select # Interactive selection + $0 select "bash" # Interactive selection with filter + +EDITOR SELECTION: + The script will use the first available editor in this order: + 1. --editor parameter + 2. \$EDITOR environment variable + 3. nvim, mg, micro, vim, nano, vi + +SNIPPETS DIRECTORY: + Snippets are stored in: $SNIPPETS_DIR + Each snippet is a .txt file in a category subdirectory + The directory is git-tracked for version control + +EOF +} + +# Parse arguments for commands that support --editor +parse_editor_args() { + local args="$*" + local category="" + local name="" + local content="" + local editor="" + + # Parse arguments + while [ $# -gt 0 ]; do + case "$1" in + --editor) + editor="$2" + shift 2 + ;; + *) + if [ -z "$category" ]; then + category="$1" + elif [ -z "$name" ]; then + name="$1" + elif [ -z "$content" ]; then + content="$1" + fi + shift + ;; + esac + done + + echo "$category|$name|$content|$editor" +} + +# Main script logic +main() { + ensure_snippets_dir + + case "${1:-help}" in + add) + shift + # Parse arguments for add command + local parsed_args + parsed_args=$(parse_editor_args "$@") + local category=$(echo "$parsed_args" | cut -d'|' -f1) + local name=$(echo "$parsed_args" | cut -d'|' -f2) + local content=$(echo "$parsed_args" | cut -d'|' -f3) + local editor=$(echo "$parsed_args" | cut -d'|' -f4) + add_snippet "$category" "$name" "$content" "$editor" + ;; + edit) + shift + # Parse arguments for edit command + local parsed_args + parsed_args=$(parse_editor_args "$@") + local category=$(echo "$parsed_args" | cut -d'|' -f1) + local name=$(echo "$parsed_args" | cut -d'|' -f2) + local editor=$(echo "$parsed_args" | cut -d'|' -f4) + edit_snippet "$category" "$name" "$editor" + ;; + search) + shift + search_snippets "$@" + ;; + list) + list_snippets + ;; + select) + shift + select_snippet "$@" + ;; + init) + init_git + ;; + help|--help|-h) + show_help + ;; + *) + echo "Unknown command: $1" + echo "Run '$0 help' for usage information." + exit 1 + ;; + esac +} + +# Run main function with all arguments +main "$@" |