55 lines
1.4 KiB
Bash
Executable File
55 lines
1.4 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
create-prompt() {
|
|
local title="$1"
|
|
|
|
if [[ -z "$title" ]]; then
|
|
echo "Error: Title cannot be empty."
|
|
echo "Usage: task create-prompt <title>"
|
|
return 1
|
|
fi
|
|
|
|
local prompts_dir="prompts"
|
|
if [[ ! -d "$prompts_dir" ]]; then
|
|
mkdir -p "$prompts_dir"
|
|
fi
|
|
|
|
local highest_num=0
|
|
for file in "$prompts_dir"/[0-9][0-9]_*.md; do
|
|
if [[ -f "$file" ]]; then
|
|
local num=${file#"$prompts_dir"/}
|
|
num=${num%%_*}
|
|
num=${num//[!0-9]/}
|
|
if [[ -n "$num" ]] && (( 10#$num > highest_num )); then
|
|
highest_num=$((10#$num))
|
|
fi
|
|
fi
|
|
done
|
|
|
|
local next_num=$((highest_num + 1))
|
|
local padded_num=$(printf "%02d" "$next_num")
|
|
|
|
local slug=$(echo "$title" | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | tr -cd '[:alnum:]-')
|
|
local filename="${padded_num}_${slug}.md"
|
|
local filepath="$prompts_dir/$filename"
|
|
|
|
if [[ -f "$filepath" ]]; then
|
|
echo "Error: File $filepath already exists."
|
|
return 1
|
|
fi
|
|
|
|
export PROMPT_TITLE="$title"
|
|
export PROMPT_ID="$padded_num"
|
|
envsubst < "$HOME/taskfile.build/prompt.md.template" > "$filepath"
|
|
|
|
if [[ -n "$EDITOR" ]]; then
|
|
"$EDITOR" "$filepath"
|
|
fi
|
|
|
|
echo 'File created:'
|
|
echo "$filepath"
|
|
|
|
# Copy path to clipboard
|
|
echo "$filepath" | copy-to-clipboard
|
|
echo "Path to prompt file copied to clipboard."
|
|
} |