I want to automatically press
- Enter (select
generic shell command
) - Enter (
Copy command to clipboard
) - Up (Move cursor to
Exit
) - Enter (Exit gh cli)
so I can Ctrl+V to paste the suggested terminal command.
Question
How do I use expect
(linux tool) to do that?
I can’t get the correct combination of keywords like timeout, interact, send.
I also suspect gh copilot copying to clipboard is messing with the interactivity design, or there are side-effects from a previous run of ./copilot_auto.sh how to list files
affecting current run.
Manual workflow to be automated
Input1: gh copilot suggest how to list files
Output1:
Welcome to GitHub Copilot in the CLI!
version 0.5.4-beta (2024-01-04)
I'm powered by AI, so surprises and mistakes are possible. Make sure to verify any generated code or suggestions, and share feedback so that we can learn and improve. For more information, see https://gh.io/gh-copilot-transparency
? What kind of command can I help you with? [Use arrows to move, type to filter]
> generic shell command
gh command
git command
Input 2: Press Enter to select generic shell command
Output 2: (2nd and 3rd options disappear from output 1 and suggestion appears)
? What kind of command can I help you with?
> generic shell command
Suggestion:
ls
? Select an option [Use arrows to move, type to filter]
> Copy command to clipboard
Explain command
Revise command
Rate response
Exit
Input 3: Press Enter to select Copy command to clipboard
Output 3: Command copied to clipboard
appears as confirmation, then options repeat
? Select an option
> Copy command to clipboard
Command copied to clipboard!
? Select an option [Use arrows to move, type to filter]
> Copy command to clipboard
Explain command
Revise command
Rate response
Exit
Automation Attempts
Currently I tried to use expect
:
Timeout is used to wait for copilot cli to get to the option screen. I tweaked expect code from copilot, I have weak understanding of expect mechanics.
#!/usr/bin/expect
set input_command [join $argv " "]
spawn gh copilot suggest $input_command
interact {
timeout 1 { send "r" }
}
# interact
# sleep 1
# send "r"
Questions on Expect mechanics
Example 1: Why does below not send Enter like the timeout example?
interact
sleep 1
send "r"
Example 2: Why does below timeout 1 fail like below but timeout 2 works to continuously send up key?
Why does Enter key have a shorter timeout required than Up key?
Where is this ['x1b' ']
below coming from? (This is where i suspect problems due to clipboard or side-effect of previous run)
Input code
#!/usr/bin/expect
set input_command [join $argv " "]
spawn gh copilot suggest $input_command
interact {
timeout 1 { send "33[A" }
}
Output
spawn gh copilot suggest how to list files
Welcome to GitHub Copilot in the CLI!
version 0.5.4-beta (2024-01-04)
I'm powered by AI, so surprises and mistakes are possible. Make sure to verify any generated code or suggestions, and share feedback so that we can learn and improve. For more information, see https://gh.io/gh-copilot-transparency
? What kind of command can I help you with? 1R [Use arrows to move, type to filter]
✗ Error: could not prompt: unexpected escape sequence from terminal: ['x1b' ']']
^[[7;1R^[]11;rgb:1f1f/2424/2828^[^[[11;1R%
Example 3: If I try to mix both Enter and Up keys to get closer to the 4 key sequence mentioned at the start, it only presses Up key and ignores Enter:
Why is interact infinite looping, and why is it only executing the last command?
#!/usr/bin/expect
set input_command [join $argv " "]
spawn gh copilot suggest $input_command
interact {
timeout 1 { send "r" }
timeout 2 { send "33[A" }
}
2
For everything that you spawn or send, you should expect some response.
For this scenario, you want to
- spawn the
gh
command with some arguments - expect to see the “what kind of command” prompt
- send “hit enter”
- expect to see the “select an option” prompt
- send “hit enter”
- expect to see the “select an option” prompt again
- send “hit Down 4 times and enter”
- or maybe typing the exit response is simpler: send “Exitr”
- expect the process to end
Translating that into (untested) code
spawn gh copilot suggest {*}$args
expect "What kind of command can I help you with"
send "r"
expect "Select an option"
send "r"
expect "Select an option"
send "Exitr"
expect eof
The problem with interact
is that it sends control away from the code into the user’s hands.
You can provide interact
some options to control it, such as “pattern action” pairs to do things when the user types something, or “timeout action” when the user doesn’t type anything.
But you haven’t provided any way to return control back to the program.
The interact
command is just sitting there waiting for you to type, hitting Enter every second that you don’t.