|
| 1 | +#!/usr/bin/env bash |
| 2 | +# clip - Universal clipboard dispatcher |
| 3 | + |
| 4 | +set -euo pipefail |
| 5 | + |
| 6 | +# Determine DOTFILES_DIR from script location |
| 7 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 8 | +DOTFILES_DIR="$(dirname "$SCRIPT_DIR")" |
| 9 | + |
| 10 | +# Platform detection |
| 11 | +detect_platform() { |
| 12 | + if [[ "$OSTYPE" == "darwin"* ]]; then |
| 13 | + COPY_CMD="pbcopy" |
| 14 | + PASTE_CMD="pbpaste" |
| 15 | + HTML_SUPPORT=true |
| 16 | + HTML_CLIPBOARD="$DOTFILES_DIR/lib/html-clipboard" |
| 17 | + elif command -v wl-copy &>/dev/null; then |
| 18 | + COPY_CMD="wl-copy" |
| 19 | + PASTE_CMD="wl-paste" |
| 20 | + HTML_SUPPORT=false |
| 21 | + elif command -v xclip &>/dev/null; then |
| 22 | + COPY_CMD="xclip -selection clipboard -in" |
| 23 | + PASTE_CMD="xclip -selection clipboard -out" |
| 24 | + HTML_SUPPORT=false |
| 25 | + else |
| 26 | + echo "Error: No clipboard utility found" >&2 |
| 27 | + echo "Install pbcopy (macOS), wl-clipboard (Wayland), or xclip (X11)" >&2 |
| 28 | + exit 1 |
| 29 | + fi |
| 30 | +} |
| 31 | + |
| 32 | +# Helper function to check dependencies |
| 33 | +check_dependency() { |
| 34 | + local dep="$1" |
| 35 | + local cmd="${2:-$dep}" |
| 36 | + |
| 37 | + if ! command -v "$cmd" &>/dev/null; then |
| 38 | + echo "Error: Required dependency '$dep' not found" >&2 |
| 39 | + echo "Install it and try again" >&2 |
| 40 | + exit 1 |
| 41 | + fi |
| 42 | +} |
| 43 | + |
| 44 | +# Helper function to check HTML support |
| 45 | +require_html_support() { |
| 46 | + if [[ "$HTML_SUPPORT" != "true" ]]; then |
| 47 | + echo "Error: HTML clipboard operations are currently only supported on macOS" >&2 |
| 48 | + echo "Linux support is planned for future releases" >&2 |
| 49 | + exit 1 |
| 50 | + fi |
| 51 | + |
| 52 | + if [[ ! -x "$HTML_CLIPBOARD" ]]; then |
| 53 | + echo "Error: html-clipboard helper not found or not executable" >&2 |
| 54 | + echo "Expected location: $HTML_CLIPBOARD" >&2 |
| 55 | + exit 1 |
| 56 | + fi |
| 57 | +} |
| 58 | + |
| 59 | +# Subcommand implementations |
| 60 | + |
| 61 | +cmd_get() { |
| 62 | + $PASTE_CMD |
| 63 | +} |
| 64 | + |
| 65 | +cmd_set() { |
| 66 | + $COPY_CMD |
| 67 | +} |
| 68 | + |
| 69 | +cmd_copy() { |
| 70 | + cmd_set "$@" |
| 71 | +} |
| 72 | + |
| 73 | +cmd_paste() { |
| 74 | + cmd_get "$@" |
| 75 | +} |
| 76 | + |
| 77 | +cmd_markdownify() { |
| 78 | + require_html_support |
| 79 | + check_dependency pandoc |
| 80 | + check_dependency unescape-markdown |
| 81 | + |
| 82 | + # Set PATH and locale for consistent behavior |
| 83 | + export PATH="/opt/homebrew/bin:/usr/local/bin:$PATH" |
| 84 | + export LC_ALL=en_US.UTF-8 |
| 85 | + export LANG=en_US.UTF-8 |
| 86 | + |
| 87 | + "$HTML_CLIPBOARD" get | \ |
| 88 | + pandoc -f html -t gfm-raw_html | \ |
| 89 | + unescape-markdown | \ |
| 90 | + $COPY_CMD |
| 91 | +} |
| 92 | + |
| 93 | +cmd_normalize() { |
| 94 | + require_html_support |
| 95 | + check_dependency pandoc |
| 96 | + |
| 97 | + # Set PATH and locale for consistent behavior |
| 98 | + export PATH="/opt/homebrew/bin:/usr/local/bin:$PATH" |
| 99 | + export LC_ALL=en_US.UTF-8 |
| 100 | + export LANG=en_US.UTF-8 |
| 101 | + |
| 102 | + "$HTML_CLIPBOARD" get | \ |
| 103 | + pandoc -f html -t gfm-raw_html | \ |
| 104 | + pandoc -f gfm -t html | \ |
| 105 | + "$HTML_CLIPBOARD" set |
| 106 | +} |
| 107 | + |
| 108 | +cmd_import() { |
| 109 | + local format="$1" |
| 110 | + shift |
| 111 | + |
| 112 | + case "$format" in |
| 113 | + csv) |
| 114 | + check_dependency csvcat |
| 115 | + csvcat "$@" | $COPY_CMD |
| 116 | + ;; |
| 117 | + xlsx) |
| 118 | + check_dependency xlcat |
| 119 | + xlcat "$@" | $COPY_CMD |
| 120 | + ;; |
| 121 | + *) |
| 122 | + echo "Error: Unknown import format '$format'" >&2 |
| 123 | + echo "Supported formats: csv, xlsx" >&2 |
| 124 | + exit 1 |
| 125 | + ;; |
| 126 | + esac |
| 127 | +} |
| 128 | + |
| 129 | +cmd_wrap() { |
| 130 | + check_dependency wrap |
| 131 | + |
| 132 | + # Get clipboard, pipe through wrap, set clipboard |
| 133 | + $PASTE_CMD | wrap "$@" | $COPY_CMD |
| 134 | +} |
| 135 | + |
| 136 | +cmd_merge() { |
| 137 | + check_dependency clipmerge |
| 138 | + |
| 139 | + # Check for Alfred database (clipmerge will provide its own error if missing) |
| 140 | + # Just pass through to clipmerge |
| 141 | + exec clipmerge "$@" |
| 142 | +} |
| 143 | + |
| 144 | +cmd_help() { |
| 145 | + cat <<'EOF' |
| 146 | +clip - Universal clipboard dispatcher |
| 147 | +
|
| 148 | +USAGE: |
| 149 | + clip <subcommand> [args...] |
| 150 | +
|
| 151 | +SUBCOMMANDS: |
| 152 | + get Get text from clipboard |
| 153 | + set Set clipboard from stdin |
| 154 | + copy Alias for 'set' |
| 155 | + paste Alias for 'get' |
| 156 | +
|
| 157 | + markdownify Convert HTML in clipboard to Markdown (macOS only) |
| 158 | + normalize Clean up HTML in clipboard (macOS only) |
| 159 | +
|
| 160 | + import csv [FILE] [-d DIR] Import CSV to clipboard |
| 161 | + import xlsx [FILE] [-d DIR] Import Excel to clipboard |
| 162 | +
|
| 163 | + wrap TAG [...args] Wrap clipboard content in XML tags |
| 164 | + (equivalent to: clip get | wrap TAG | clip set) |
| 165 | +
|
| 166 | + merge [...args] Merge entries from Alfred clipboard history (macOS only) |
| 167 | +
|
| 168 | + help Show this help message |
| 169 | +
|
| 170 | +EXAMPLES: |
| 171 | + # Basic clipboard operations |
| 172 | + echo "Hello" | clip set |
| 173 | + clip get |
| 174 | +
|
| 175 | + # HTML operations (macOS only) |
| 176 | + clip markdownify # Convert HTML clipboard to Markdown |
| 177 | + clip normalize # Clean up HTML formatting |
| 178 | +
|
| 179 | + # Import data |
| 180 | + clip import csv data.csv |
| 181 | + clip import xlsx -d ~/Downloads |
| 182 | +
|
| 183 | + # Wrap content in tags |
| 184 | + clip wrap content -a type=text |
| 185 | + clip wrap greeting --inline |
| 186 | +
|
| 187 | + # Merge clipboard history |
| 188 | + clip merge |
| 189 | + clip merge last 5 |
| 190 | +
|
| 191 | +PLATFORM SUPPORT: |
| 192 | + - macOS: Full support (pbcopy/pbpaste + HTML operations) |
| 193 | + - Linux (Wayland): wl-copy/wl-paste (HTML support planned) |
| 194 | + - Linux (X11): xclip (HTML support planned) |
| 195 | +
|
| 196 | +For subcommand-specific help, run the underlying tool with --help: |
| 197 | + wrap --help |
| 198 | + csvcat --help |
| 199 | + xlcat --help |
| 200 | + clipmerge --help |
| 201 | +EOF |
| 202 | +} |
| 203 | + |
| 204 | +# Main dispatcher |
| 205 | +main() { |
| 206 | + if [[ $# -eq 0 ]]; then |
| 207 | + cmd_help |
| 208 | + exit 0 |
| 209 | + fi |
| 210 | + |
| 211 | + local subcommand="$1" |
| 212 | + shift |
| 213 | + |
| 214 | + # Show help without requiring clipboard utilities |
| 215 | + if [[ "$subcommand" == "help" || "$subcommand" == "--help" || "$subcommand" == "-h" ]]; then |
| 216 | + cmd_help |
| 217 | + exit 0 |
| 218 | + fi |
| 219 | + |
| 220 | + # Detect platform for all other commands |
| 221 | + detect_platform |
| 222 | + |
| 223 | + case "$subcommand" in |
| 224 | + get|paste) |
| 225 | + cmd_get "$@" |
| 226 | + ;; |
| 227 | + set|copy) |
| 228 | + cmd_set "$@" |
| 229 | + ;; |
| 230 | + markdownify) |
| 231 | + cmd_markdownify "$@" |
| 232 | + ;; |
| 233 | + normalize) |
| 234 | + cmd_normalize "$@" |
| 235 | + ;; |
| 236 | + import) |
| 237 | + cmd_import "$@" |
| 238 | + ;; |
| 239 | + wrap) |
| 240 | + cmd_wrap "$@" |
| 241 | + ;; |
| 242 | + merge) |
| 243 | + cmd_merge "$@" |
| 244 | + ;; |
| 245 | + *) |
| 246 | + echo "Error: Unknown subcommand '$subcommand'" >&2 |
| 247 | + echo "Run 'clip help' for usage information" >&2 |
| 248 | + exit 1 |
| 249 | + ;; |
| 250 | + esac |
| 251 | +} |
| 252 | + |
| 253 | +main "$@" |
0 commit comments