-
-
Save Pl8tinium/3702c356a83b7363f3ab769d6ec47e2a to your computer and use it in GitHub Desktop.
Github copilot CLI - direct command execution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# used to make github copilot cli directly suggest you the command for fast execution | |
# usage "ghc please tell me how to get the status of a git repo" | |
# output | |
# Recommended command: git status | |
# Do you want to execute this command? [Y/n]: | |
# Executing: git status | |
# On branch main | |
# ... | |
# you may wanna adjust the timeout_value in line 20 to speed things up or slow down the process in case github copilot is to slow suggesting a command | |
# When this value is choosen too low, github copilots suggestion may not be ready, too large, you unnecessarily wait for your result | |
# also you may wanna add this to your ~/.bashrc config | |
# tested on WSL Ubuntu | |
function ghc() { | |
commands=$@ | |
tmpfile=$(mktemp) | |
timeout_value=4s | |
# Start the screen session and run the gh copilot suggest command | |
screen -dmS ghcopilot bash -c "timeout $timeout_value script -q -c 'gh copilot suggest -t shell --shell-out $commands' $tmpfile" | |
# Wait for the screen session to finish | |
while screen -list | grep -q "ghcopilot"; do | |
sleep 1 # Check every second if the session is still running | |
done | |
# Now read the output from the temporary file | |
suggestion_output=$(cat "$tmpfile") | |
# Clean up the temporary file | |
rm "$tmpfile" | |
# Strip ANSI escape sequences from the output | |
clean_output=$(echo "$suggestion_output" | sed -r 's/\x1B\[[0-9;]*[a-zA-Z]//g') | |
# Use regex to extract the command from the clean output and trim any leading/trailing spaces/newlines | |
recommended_command=$(echo "$clean_output" | sed -n '/Suggestion:/,/Select an option/p' | grep -oP '(?<=^ ).*' | tr -d '\n' | xargs) | |
# Output the captured suggestion | |
if [ -n "$recommended_command" ]; then | |
echo "Recommended command: $recommended_command" | |
# Prompt the user to execute the command | |
read -p "Do you want to execute this command? [Y/n]: " -r | |
if [[ $REPLY =~ ^[Nn]$ ]]; then | |
echo "Command not executed." | |
else | |
# Execute the command if the user agrees (default is Yes) | |
echo "Executing: $recommended_command" | |
eval "$recommended_command" | |
fi | |
else | |
echo "No recommendation was found or the command timed out." | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
3 seconds is the sweet spot for the timeout for me