closedloop-technologies / autocomplete-sh

Large language model in the terminal! Less `--help` and `man` and more getting stuff done
MIT License
42 stars 2 forks source link

Features requested from Reddit post #6

Closed closedLoop closed 1 month ago

closedLoop commented 1 month ago

Feedback from my Reddit post:

A lot of love went into this, I'm glad you found this thread and posted! It's a great baseline to study your prompts and logic, it's given me some great ideas for my own implementation.

Here's what I'm thinking for making it faster for myself:

Launch it system wide by pressing CAPS_LOCK

Get the currently selected line contents (if my editor is the active window)

Query the LLM and display the code results in a drop-down menu (dmenu OR rofi)

Selecting a result adds it to the clipboard (xclip), or inserts it into the file (with xdotool - if my editor is the active window)

Since these queries are likely to be simple enough for an LLM to complete, I'll use GroqCloud's API instead which is blazingly fast and currently free.

closedLoop commented 1 month ago

This is the best example of getting the dropdown menu working, however I cannot get the command to populate as READLINE_LINE doesn't appear to update the text around the cursor

#!/bin/bash

# Function to capture key presses
get_key() {
  IFS= read -rsn1 key 2>/dev/null >&2
  if [[ $key == $'\x1b' ]]; then
    read -rsn2 key
    if [[ $key == [A ]]; then echo up; fi
    if [[ $key == [B ]]; then echo down; fi
  fi
}

# Function to display the menu
show_menu() {
  echo "Choose an option:"
  for i in "${!options[@]}"; do
    if [[ $i == $selected ]]; then
      echo -e "\e[1;32m> ${options[i]}\e[0m"
    else
      echo "  ${options[i]}"
    fi
  done
}

# Options array
options=("ls" "git push" "htop" "echo Hello, World!")

# Initial selected option
selected=0

# Save the current cursor position
tput sc

# Infinite loop to handle user input
while true; do
  # Restore the cursor position
  tput rc
  tput ed
  show_menu
  key=$(get_key)
  case $key in
    up)
      ((selected--))
      if ((selected < 0)); then
        selected=$((${#options[@]} - 1))
      fi
      ;;
    down)
      ((selected++))
      if ((selected >= ${#options[@]})); then
        selected=0
      fi
      ;;
    "")
      break
      ;;
  esac
done

# Send the selected command to the terminal input buffer
selected_command="${options[selected]}"
printf "\r\033[K%s" "$selected_command"
stty -echo # Turn off terminal echo
read -r -d '' -s -n 1 -p "$selected_command" # Read the input silently
stty echo # Turn terminal echo back on
eval "$selected_command"
closedLoop commented 1 month ago

get currently selected line contents or clipboard?

Closing this and moving above to #1