aboutsummaryrefslogtreecommitdiffstats
path: root/bash
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2025-09-14 16:30:02 -0400
committerelioat <elioat@tilde.institute>2025-09-14 16:30:02 -0400
commit592cd845b4f56ce1d7d3a40d38456acc6124010c (patch)
tree844b57d360060e5f8abae30e4c7d2c95fb230446 /bash
parent765947f0a2d17f5e2474de187a498bc8b90f7ea8 (diff)
downloadtour-592cd845b4f56ce1d7d3a40d38456acc6124010c.tar.gz
*
Diffstat (limited to 'bash')
-rw-r--r--bash/snip/README.md21
-rwxr-xr-xbash/snip/snip145
2 files changed, 155 insertions, 11 deletions
diff --git a/bash/snip/README.md b/bash/snip/README.md
index 2165c2c..95e31bb 100644
--- a/bash/snip/README.md
+++ b/bash/snip/README.md
@@ -75,9 +75,13 @@ Snippets are organized in `~/.snippets/`:
### Searching and Selecting
```bash
-# Search and display results
+# Search all snippets with content preview
./snip search "fibonacci"
+# Search only in specific category
+./snip search "bash:hello"
+./snip search "python:fibonacci"
+
# Interactive selection with search
./snip select "bash"
@@ -85,6 +89,21 @@ Snippets are organized in `~/.snippets/`:
./snip select
```
+The search command now shows:
+- Content matches with relevant line previews and context
+- Category-specific search using `category:search-terms` syntax
+- Direct select commands for each result (e.g., `./snip select html:boilerplate`)
+
+### Direct Selection
+
+You can directly select snippets using the `category:file` pattern:
+
+```bash
+# Direct selection without interactive menu
+./snip select "html:boilerplate"
+./snip select "bash:hello-world"
+```
+
## Clipboard Support
- **macOS**: Uses `pbcopy`
diff --git a/bash/snip/snip b/bash/snip/snip
index 407bd74..c14821b 100755
--- a/bash/snip/snip
+++ b/bash/snip/snip
@@ -73,18 +73,76 @@ list_snippets() {
find "$SNIPPETS_DIR" -type f -name "*.txt" | sort
}
-# Search snippets by content or filename
+# Search snippets by content or filename with preview
search_snippets() {
local query="$1"
if [ -z "$query" ]; then
echo "Usage: $0 search <query>"
+ echo " $0 search category:search-terms"
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"
+ local search_dir="$SNIPPETS_DIR"
+ local search_pattern="$query"
+
+ # Check if query has category: pattern
+ if echo "$query" | grep -q ":"; then
+ local category=$(echo "$query" | cut -d':' -f1)
+ local search_terms=$(echo "$query" | cut -d':' -f2-)
+ search_dir="$SNIPPETS_DIR/$category"
+
+ if [ ! -d "$search_dir" ]; then
+ echo "Category '$category' not found."
+ exit 1
+ fi
+
+ search_pattern="$search_terms"
+ fi
+
+ # Search in both filename and content with preview
+ find "$search_dir" -type f -name "*.txt" 2>/dev/null | while read -r file; do
+ local relative_path="${file#$SNIPPETS_DIR/}"
+ local category=$(dirname "$relative_path")
+ local snippet_name=$(basename "$file" .txt)
+
+ # Check if query matches filename
+ if basename "$file" | grep -q "$search_pattern"; then
+ echo "[$category] $snippet_name"
+ echo " File: $relative_path"
+ echo " Select: $0 select $category:$snippet_name"
+ echo
+ # Check if query matches content
+ elif grep -l "$search_pattern" "$file" >/dev/null 2>&1; then
+ echo "[$category] $snippet_name"
+ echo " File: $relative_path"
+ echo " Select: $0 select $category:$snippet_name"
+
+ # Show relevant lines with context (before and after)
+ grep -n "$search_pattern" "$file" | head -2 | while read -r line; do
+ local line_num=$(echo "$line" | cut -d':' -f1)
+ local line_content=$(echo "$line" | cut -d':' -f2-)
+ local total_lines=$(wc -l < "$file")
+
+ # Show context lines if they exist
+ # Line before (if not first line)
+ if [ "$line_num" -gt 1 ]; then
+ local prev_line_num=$((line_num - 1))
+ local prev_content=$(sed -n "${prev_line_num}p" "$file")
+ echo " Line $prev_line_num: $prev_content"
+ fi
+
+ # The matching line
+ echo " Line $line_num: $line_content"
+
+ # Line after (if not last line)
+ if [ "$line_num" -lt "$total_lines" ]; then
+ local next_line_num=$((line_num + 1))
+ local next_content=$(sed -n "${next_line_num}p" "$file")
+ echo " Line $next_line_num: $next_content"
+ fi
+
+ echo
+ done
fi
done
}
@@ -197,6 +255,26 @@ select_snippet() {
local query="$1"
local snippets
+ # Check if query is in category:file format
+ if echo "$query" | grep -q ":"; then
+ local category=$(echo "$query" | cut -d':' -f1)
+ local snippet_name=$(echo "$query" | cut -d':' -f2-)
+ local snippet_file="$SNIPPETS_DIR/$category/$snippet_name.txt"
+
+ if [ ! -f "$snippet_file" ]; then
+ echo "Snippet not found: $category:$snippet_name"
+ exit 1
+ fi
+
+ # Copy to clipboard
+ local content
+ content=$(cat "$snippet_file")
+ copy_to_clipboard "$content"
+
+ echo "Copied to clipboard: [$category] $snippet_name"
+ return
+ fi
+
if [ -n "$query" ]; then
snippets=$(search_snippets "$query")
else
@@ -253,21 +331,50 @@ USAGE:
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
+ search <query> Search snippets with content preview
list List all snippets
- select [query] Interactive snippet selection
+ select [query] Select snippet to clipboard
init Initialize snippets directory and git repo
help Show this help message
+SEARCH PATTERNS:
+ search <term> Search all snippets for term
+ search "category:term" Search only in specific category
+ search "category:" List all snippets in category
+
+SELECTION MODES:
+ select Interactive menu selection
+ select <term> Interactive selection with filter
+ select "category:name" Direct selection by category:name
+
EXAMPLES:
+ # Adding snippets
$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
+
+ # Searching snippets
+ $0 search "hello world" # Search all snippets
+ $0 search "bash:hello" # Search only in bash category
+ $0 search "python:" # List all python snippets
+
+ # Selecting snippets
$0 select # Interactive selection
$0 select "bash" # Interactive selection with filter
+ $0 select "html:boilerplate" # Direct selection by category:file
+
+ # Management
+ $0 list # List all snippets
+ $0 init # Initialize snippets directory
+
+FEATURES:
+ • Content previews with context lines in search results
+ • Direct selection commands shown in search output
+ • Category-specific search and selection
+ • Cross-platform clipboard support (macOS/Linux)
+ • Git-tracked snippets directory for version control
+ • POSIX-compatible shell script
EDITOR SELECTION:
The script will use the first available editor in this order:
@@ -314,11 +421,29 @@ parse_editor_args() {
echo "$category|$name|$content|$editor"
}
+# Show brief usage
+show_brief_usage() {
+ cat << EOF
+snip - A little snippet manager
+
+USAGE:
+ $0 <command> [options]
+
+COMMANDS:
+ add, edit, search, list, select, init, help
+
+Run '$0 help' for detailed usage information.
+EOF
+}
+
# Main script logic
main() {
ensure_snippets_dir
- case "${1:-help}" in
+ case "${1:-}" in
+ "")
+ show_brief_usage
+ ;;
add)
shift
# Parse arguments for add command