suggestCommands method

List<String> suggestCommands(
  1. String input, {
  2. int limit = 3,
})

Returns close matches for a command name (for smarter UX on typos).

Implementation

List<String> suggestCommands(String input, {int limit = 3}) {
  final trimmed = input.trim();
  if (trimmed.isEmpty) return const [];

  final names = commands.map((c) => c.name).toSet().toList();
  names.sort();

  final scored = <({String name, int score})>[];
  for (final name in names) {
    scored.add((name: name, score: _levenshtein(trimmed, name)));
  }

  scored.sort((a, b) => a.score.compareTo(b.score));
  return scored.take(limit).map((e) => e.name).toList();
}