Skip to content

Instantly share code, notes, and snippets.

@asportnoy
Last active May 9, 2023 20:02
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save asportnoy/a55a7a5df34af008f7ae2fdbbef22f04 to your computer and use it in GitHub Desktop.
Save asportnoy/a55a7a5df34af008f7ae2fdbbef22f04 to your computer and use it in GitHub Desktop.
GitHub Copilot CLI for Fish Shell
# Requires the GitHub Copilot CLI from GitHub Next
# https://www.npmjs.com/package/@githubnext/github-copilot-cli#installation-and-setup
# This needs to be set up as a function in your fish config. You can use `funced -s q` to do this.
# Options:
# q -g --git: Use github-copilot-cli git-assist
# q -h --gh: Use github-copilot-cli gh-assist
# Defaults to using what-the-shell (general command assist) if neither option is provided
# Completions for your Fish config:
complete -c q -s g -l git -d "Use git assist"
complete -c q -s h -l gh -d "Use GitHub CLI assist"
# Function code starts here:
function q
argparse -si --name q g/git h/gh -- $argv
set query "$argv"
if not test "$query"
# prompt user for input if no query is provided
read --prompt "set_color --bold 6b75ff; echo -n \" What do you want to do? \"; set_color normal; set_color brblack; echo -n \u203a \"\"; set_color normal;" query
end
if not test "$query"
# no query provided, exit
return 1
end
set subcmd what-the-shell
if test "$_flag_g"
set subcmd git-assist
end
if test "$_flag_h"
set subcmd gh-assist
end
# generate a file for storing the output
set file (mktemp)
# run copilot
github-copilot-cli $subcmd --shellout $file "On Fish Shell: $query"
if test "$status" -ne 0
# copilot failed, exit
return 1
end
# read copilot output
set cmd (cat $file)
rm -rf $file
if not test "$cmd"
# no command from copilot, exit
return 1
end
# execute command
commandline $cmd
commandline -f execute
end
@WinkelCode
Copy link

Thanks, works great!

@knoopx
Copy link

knoopx commented Mar 30, 2023

works but output is bash-like, so most results won't run

@asportnoy
Copy link
Author

@knoopx I prepended "On Fish Shell" to try and combat this but it's not perfect. Let me know if you have a different prompt that's more effective.

@WinkelCode
Copy link

works but output is bash-like, so most results won't run

Could you please provide an example? I only used Copilot CLI for specific executable commands, so nothing shell specific.

@knoopx
Copy link

knoopx commented Mar 31, 2023

works but output is bash-like, so most results won't run

Could you please provide an example? I only used Copilot CLI for specific executable commands, so nothing shell specific.

Anything that loops over files like "convert all PNG to JPG"

@WinkelCode
Copy link

Anything that loops over files like "convert all PNG to JPG"

I get this

$ q convert all PNG to JPG

 ──────────────────── Command ────────────────────

for file in *.png; convert $file $file.jpg; end

 ────────────────── Explanation ──────────────────

○ The for loop iterates over a list of items and executes its body for each, using the loop variable $file.
  ◆ The list of items is *.png which means all files ending in .png in the current directory.
○ The loop body executes one command for each file:
  ◆ convert $file $file.jpg converts the current file from PNG to JPG.

Which seems correct?

@knoopx
Copy link

knoopx commented Apr 2, 2023

@WinkelCode certainly works now, ¯\_(ツ)_/¯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment