| #!/usr/bin/env zsh |
|
|
| |
|
|
| |
| function _forge_action_editor() { |
| local initial_text="$1" |
| echo |
| |
| |
| local editor_cmd="${FORGE_EDITOR:-${EDITOR:-nano}}" |
| |
| |
| if ! command -v "${editor_cmd%% *}" &>/dev/null; then |
| _forge_log error "Editor not found: $editor_cmd (set FORGE_EDITOR or EDITOR)" |
| return 1 |
| fi |
| |
| |
| local forge_dir=".forge" |
| if [[ ! -d "$forge_dir" ]]; then |
| mkdir -p "$forge_dir" || { |
| _forge_log error "Failed to create .forge directory" |
| return 1 |
| } |
| fi |
| |
| |
| local temp_file="${forge_dir}/FORGE_EDITMSG.md" |
| touch "$temp_file" || { |
| _forge_log error "Failed to create temporary file" |
| return 1 |
| } |
| |
| |
| trap "rm -f '$temp_file'" EXIT INT TERM |
| |
| |
| if [[ -n "$initial_text" ]]; then |
| echo "$initial_text" > "$temp_file" |
| fi |
| |
| |
| (eval "$editor_cmd '$temp_file'" </dev/tty >/dev/tty 2>&1) |
| local editor_exit_code=$? |
| |
| if [ $editor_exit_code -ne 0 ]; then |
| _forge_log error "Editor exited with error code $editor_exit_code" |
| _forge_reset |
| return 1 |
| fi |
| |
| |
| local content |
| content=$(cat "$temp_file" | tr -d '\r') |
| |
| if [ -z "$content" ]; then |
| _forge_log info "Editor closed with no content" |
| BUFFER="" |
| CURSOR=0 |
| zle reset-prompt |
| return 0 |
| fi |
| |
| |
| BUFFER=": $content" |
| CURSOR=${#BUFFER} |
| |
| zle reset-prompt |
| } |
|
|
| |
| |
| function _forge_action_suggest() { |
| local description="$1" |
| |
| if [[ -z "$description" ]]; then |
| _forge_log error "Please provide a command description" |
| return 0 |
| fi |
| |
| echo |
|
|
| |
| local generated_command |
| generated_command=$(FORCE_COLOR=true CLICOLOR_FORCE=1 _forge_exec suggest "$description") |
|
|
| if [[ -n "$generated_command" ]]; then |
| |
| BUFFER="$generated_command" |
| CURSOR=${#BUFFER} |
| zle reset-prompt |
| else |
| _forge_log error "Failed to generate command" |
| fi |
| } |
|
|