Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for handling multiple tab autocomplete attempts #62

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Vpet95
Copy link

@Vpet95 Vpet95 commented Aug 16, 2022

What is this attempting to fix?

Prior to this PR, a user's input was taken into account only for the first autocompletion pass through; after which prompt-sync would fall back to cycling through the entire possible data set provided by the user, regardless of whether the initial search string yielded multiple possible autocomplete results.

Additionally, any subsequent autocomplete attempts would yield results from anywhere within the new result list, not the start.

How does this fix the problem?

This PR splits the responsibilities of the autocomplete search string and the main output string between two variables.
autoCompleteSearchTerm allows users to cycle through the results of the current search, while str allows current selection and terminal output to stay up-to-date as it did before.

Hitting any other key after the tab would clear the current search state.

Testing / Example Case

Simple demo program to demonstrate the fix:

const fullList = [
  "Apple",
  "Broil",
  "Cup",
  "Baseball",
  "Danger",
  "Cast",
  "Dork",
  "Create",
  "Ardvark",
  "Balloon",
  "Avocado",
  "Dog",
  "Aromatic",
  "Destiny",
  "Apperant"
];

const complete = (choices) => (str) =>
  choices.filter(
    (choice) => choice.toLowerCase().indexOf(str.toLowerCase()) === 0
  );

const prompt = require(".")({
  autocomplete: complete(fullList),
});

const resp = prompt("Enter a string then press TAB: ");

console.log(`You entered: ${resp}`);

In either version, running the program and entering "a", followed by TAB would yield the string: "Apple".

Prior to the fix, subsequent TAB inputs would yield the following results:

"Broil"
"Cup"
"Baseball"
"Danger"
etc...

After the fix, subsequent tab inputs yield all a-strings:

"Apperant"
"Apple"
"Ardvark"
"Aromatic"
"Avocado"

Demo


Before

Kapture 2022-08-15 at 21 56 09

After

Kapture 2022-08-15 at 21 57 51

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.

1 participant