From 6ee3449e0c47b9bc2b1d88820fd1b7341eb35a51 Mon Sep 17 00:00:00 2001 From: Fredrik Fornwall Date: Mon, 13 Jul 2015 19:50:44 -0400 Subject: [PATCH] command-not-found: handle multiple matches at same length --- packages/command-not-found/build.sh | 2 +- packages/command-not-found/command-not-found.c | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/packages/command-not-found/build.sh b/packages/command-not-found/build.sh index 821b2c09..7c870148 100644 --- a/packages/command-not-found/build.sh +++ b/packages/command-not-found/build.sh @@ -1,6 +1,6 @@ TERMUX_PKG_HOMEPAGE=http://termux.com TERMUX_PKG_DESCRIPTION="List of commands available for installation" -TERMUX_PKG_VERSION=0.1 +TERMUX_PKG_VERSION=0.2 termux_step_make_install () { TERMUX_SHARE_DIR=$TERMUX_PREFIX/share/termux diff --git a/packages/command-not-found/command-not-found.c b/packages/command-not-found/command-not-found.c index 1c83a844..08641a25 100644 --- a/packages/command-not-found/command-not-found.c +++ b/packages/command-not-found/command-not-found.c @@ -72,7 +72,29 @@ int main(int argc, char** argv) { printf("%s: command not found\n", command_not_found); } else { printf("No command '%s' found, did you mean:\n", command_not_found); - printf(" Command '%s' from package '%s'\n", best_command_guess, best_package_guess); + if (guesses_at_best_distance == 1) { + // Only one suggestion - show it: + printf(" Command '%s' from package '%s'\n", best_command_guess, best_package_guess); + } else { + // Multiple suggestions at the same distance - show them all: + rewind(commands_file); + while (true) { + size_t buffer_length = sizeof(current_line); + ssize_t read_bytes = getline(¤t_line, &buffer_length, commands_file); + if (read_bytes <= 1) break; + size_t line_length = strlen(current_line); + current_line[line_length-1] = 0; + if (current_line[0] == ' ') { // Binary + char* binary_name = current_line + 1; + int distance = termux_levenshtein_distance(command_not_found, binary_name); + if (best_distance == distance) { + printf(" Command '%s' from package '%s'\n", binary_name, current_package); + } + } else { // Package + strncpy(current_package, current_line, sizeof(current_package)); + } + } + } } } -- 2.11.0