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

CLI: Add 'clear' command and command suggestion #342

Merged
merged 5 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions applications/services/cli/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <flipper_application/plugins/plugin_manager.h>
#include <loader/firmware_api/firmware_api.h>
#include <inttypes.h>
#include <stdlib.h>

#define TAG "CliSrv"

Expand Down Expand Up @@ -208,6 +209,31 @@ static void cli_execute_command(Cli* cli, CliCommand* command, FuriString* args)
}
}

static size_t cli_string_distance(const char* s1, const char* s2) {
size_t len1 = strlen(s1), len2 = strlen(s2);
Willy-JL marked this conversation as resolved.
Show resolved Hide resolved
size_t distance = 0;

for(size_t i = 0; i < MIN(len1, len2); i++) {
if(tolower(s1[i]) != tolower(s2[i])) distance++;
}
return distance + (len1 > len2 ? len1 - len2 : len2 - len1);
}

static void cli_find_similar_command(Cli* cli, const char* input, FuriString* suggestion) {
size_t min_distance = SIZE_MAX;
furi_string_reset(suggestion);

CliCommandTree_it_t it;
for(CliCommandTree_it(it, cli->commands); !CliCommandTree_end_p(it); CliCommandTree_next(it)) {
const char* cmd_name = furi_string_get_cstr(*CliCommandTree_ref(it)->key_ptr);
size_t distance = cli_string_distance(input, cmd_name);
if(distance < min_distance && distance <= 3) {
Willy-JL marked this conversation as resolved.
Show resolved Hide resolved
min_distance = distance;
furi_string_set(suggestion, cmd_name);
}
}
}

static void cli_handle_enter(Cli* cli) {
cli_normalize_line(cli);

Expand Down Expand Up @@ -245,9 +271,21 @@ static void cli_handle_enter(Cli* cli) {
} else {
furi_check(furi_mutex_release(cli->mutex) == FuriStatusOk);
cli_nl(cli);
printf(
"`%s` command not found, use `help` or `?` to list all available commands",
furi_string_get_cstr(command));
FuriString* suggestion = furi_string_alloc();
cli_find_similar_command(cli, furi_string_get_cstr(command), suggestion);

if(furi_string_size(suggestion) > 0) {
Willy-JL marked this conversation as resolved.
Show resolved Hide resolved
printf(
"`%s` command not found, did you mean `%s`? Use `help` or `?` to list all available commands",
furi_string_get_cstr(command),
furi_string_get_cstr(suggestion));
} else {
printf(
"`%s` command not found, use `help` or `?` to list all available commands",
furi_string_get_cstr(command));
}

furi_string_free(suggestion);
cli_putc(cli, CliKeyBell);
}

Expand Down
11 changes: 11 additions & 0 deletions applications/services/cli/cli_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,13 @@ void cli_command_i2c(Cli* cli, FuriString* args, void* context) {
furi_hal_i2c_release(&furi_hal_i2c_handle_external);
}

void cli_command_clear(Cli* cli, FuriString* args, void* context) {
UNUSED(cli);
UNUSED(args);
UNUSED(context);
printf("\e[2J\e[H");
}

CLI_PLUGIN_WRAPPER("info", cli_command_info)
CLI_PLUGIN_WRAPPER("src", cli_command_src)
CLI_PLUGIN_WRAPPER("neofetch", cli_command_neofetch)
Expand All @@ -693,6 +700,7 @@ CLI_PLUGIN_WRAPPER("vibro", cli_command_vibro)
CLI_PLUGIN_WRAPPER("led", cli_command_led)
CLI_PLUGIN_WRAPPER("gpio", cli_command_gpio)
CLI_PLUGIN_WRAPPER("i2c", cli_command_i2c)
CLI_PLUGIN_WRAPPER("clear", cli_command_clear)

void cli_commands_init(Cli* cli) {
cli_add_command(cli, "!", CliCommandFlagParallelSafe, cli_command_info_wrapper, (void*)true);
Expand Down Expand Up @@ -724,4 +732,7 @@ void cli_commands_init(Cli* cli) {
cli_add_command(cli, "led", CliCommandFlagDefault, cli_command_led_wrapper, NULL);
cli_add_command(cli, "gpio", CliCommandFlagDefault, cli_command_gpio_wrapper, NULL);
cli_add_command(cli, "i2c", CliCommandFlagDefault, cli_command_i2c_wrapper, NULL);

cli_add_command(cli, "clear", CliCommandFlagParallelSafe, cli_command_clear, NULL);
cli_add_command(cli, "cls", CliCommandFlagParallelSafe, cli_command_clear, NULL);
}