CLI Autocomplete Prompt (Standalone)

0.8.1 · active · verified Wed Apr 22

Inquirer-autocomplete-standalone provides a robust and flexible command-line interface prompt for displaying dynamic, filterable choices to users. It functions as a standalone component built on the core principles of Inquirer.js, allowing developers to integrate autocomplete functionality without needing the full `inquirer-autocomplete-prompt` package. This package, currently at version 0.8.1, is designed as a native ECMAScript module (ESM), requiring Node.js environments that support ESM. It supports both synchronous and asynchronous data sources, making it ideal for scenarios where choices need to be fetched from external APIs or computed on-the-fly based on user input. Its key differentiator is its modular, standalone nature, aligning with the modern Inquirer.js approach of independent prompts. This contrasts with the legacy `inquirer-autocomplete-prompt` package, which transitioned to ESM in v3 and provided a CJS fallback in v2. Users seeking a modern, ESM-first autocomplete prompt should opt for this package. While no explicit release cadence is stated for this specific `standalone` package, its parent project and related `inquirer-autocomplete-prompt` indicate active maintenance.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates a basic asynchronous autocomplete prompt, allowing users to select a country from a dynamically filtered list, simulating an external API call.

import autocomplete from 'inquirer-autocomplete-standalone';

interface CountryChoice {
  value: string;
  description: string;
}

// Mock an asynchronous search function that simulates an API call
async function searchCountries(input?: string): Promise<string[]> {
  const allCountries = [
    'Norway', 'Sweden', 'Denmark', 'Finland', 'Iceland',
    'Germany', 'France', 'Spain', 'Italy', 'Portugal',
    'United Kingdom', 'Ireland', 'Canada', 'United States',
    'Mexico', 'Brazil', 'Argentina', 'Australia', 'New Zealand',
    'Japan', 'China', 'India', 'South Africa'
  ];
  const normalizedInput = (input || '').toLowerCase();
  await new Promise(resolve => setTimeout(resolve, 100)); // Simulate API delay
  return allCountries.filter(country =>
    country.toLowerCase().includes(normalizedInput)
  ).slice(0, 10); // Limit results to 10 for display
}

async function runPrompt() {
  console.log('Starting autocomplete prompt...');
  const answer = await autocomplete<string>({ // Specify the type for the selected value
    message: 'Travel from what country?',
    source: async (input) => {
      const filteredCountries = await searchCountries(input);
      return filteredCountries.map(country => ({
        value: country,
        description: `${country} is a great place to visit`,
      }));
    },
    // Optional settings:
    // default: 'Norway', // Pre-select a default value
    // suggestOnly: false, // If true, allows arbitrary input not in the list
  });

  console.log(`\nYou selected: ${answer}`);
  process.exit(0); // Ensure the process exits cleanly after prompt interaction
}

runPrompt().catch(console.error);

view raw JSON →