Skip to content

Conversation

@dbohdan
Copy link
Contributor

@dbohdan dbohdan commented Apr 5, 2025

This implementation avoids adding a new readline library by using golang.org/x/term, which is already an indirect dependency. It is basic (doesn't display a list; keys like Home, End, and even Delete don't work) but nevertheless useful and closes the gap with the Python original.

Completion builds on the existing function recvCodeCompletion, which has been refactored to remove a return value and unused arguments specific to Cobra. It works on the last code word of the input line in one of three modes:

  • If the current word is a complete code word from the word list and the cursor is at the end of the line, we replace the word with the next word on the list.
  • If the current word is not a complete code word from the word list and the cursor is at the end of the line, we complete the word with the first completion from the list.
  • If the cursor is in the middle of the word, we replace everything after the cursor with the next possible completion.

There is a fallback for systems where an interactive terminal isn't detected. Empty and whitespace input cancels the receive action without an error. This is because pressing Ctrl-C in ReadLine results in empty input. It seems like good UX anyway.

Resolves #105.

@dbohdan
Copy link
Contributor Author

dbohdan commented Apr 5, 2025

It seems like term passes the same rune to the callback for Home, End, and Delete. If so, those keys aren't possible to implement through the callback.

@dbohdan
Copy link
Contributor Author

dbohdan commented Apr 5, 2025

Sadly, ReadLine from term doesn't seem to work on Windows. It hangs in my Windows 10 VM and when I use Wine. I have added a runtime check to disable it. This can be changed to compile-time if you prefer.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feature request: wormhole code completion

1 participant