-1

I want to automatically press

  1. Enter (select generic shell command)
  2. Enter (Copy command to clipboard)
  3. Up (Move cursor to Exit)
  4. 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"

enter image description here

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 "\033\[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 "\033\[A" }
}
2
  • One question at a time. :) Commented Dec 5, 2024 at 14:18
  • @sexpect How do I deal with the issue that if I break up into multiple posts, the answerer may not get the full picture of what i already tried and have problems with? If I ask one question then try to follow up with more later, the thread may go a different direction than intended. Isn't it better to give the full context of the problem, which is abstracted at the level of the title, and the next person can get all the FAQ related to this title addressed at once? The next person seeking help would probably search a similar question as in the title, minus expect as a specific tool Commented Dec 5, 2024 at 15:26

1 Answer 1

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 "Exit\r"
  • 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 "Exit\r"
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.

Sign up to request clarification or add additional context in comments.

1 Comment

The code you provided seems to have syntax problems for taking in args. I tweaked it to use the same pattern as how i took in inputs, and saw that it's the same behaviour as what i'm getting in Example 1 in my question, where send commands that are not nested in interact are showing gibberish output ``` spawn gh copilot suggest list files ^[]11;rgb:1f1f/2424/2828^[\^[[36;1R^[ ```

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.