From 5000424c42ceeede21d581b493c83fcdbed3b526 Mon Sep 17 00:00:00 2001 From: gornekich Date: Tue, 18 May 2021 12:30:04 +0300 Subject: [PATCH] [FL-1239] CLI commands readable list #464 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: あく --- applications/cli/cli.c | 8 ++++---- applications/cli/cli_commands.c | 22 +++++++++++++++++----- applications/cli/cli_i.h | 12 ++++++++++-- 3 files changed, 31 insertions(+), 11 deletions(-) mode change 100644 => 100755 applications/cli/cli_i.h diff --git a/applications/cli/cli.c b/applications/cli/cli.c index b87404ce..06f53814 100644 --- a/applications/cli/cli.c +++ b/applications/cli/cli.c @@ -5,7 +5,7 @@ Cli* cli_alloc() { Cli* cli = furi_alloc(sizeof(Cli)); - CliCommandDict_init(cli->commands); + CliCommandTree_init(cli->commands); cli->mutex = osMutexNew(NULL); furi_check(cli->mutex); @@ -126,7 +126,7 @@ void cli_enter(Cli* cli) { // Search for command furi_check(osMutexAcquire(cli->mutex, osWaitForever) == osOK); - CliCommand* cli_command = CliCommandDict_get(cli->commands, command); + CliCommand* cli_command = CliCommandTree_get(cli->commands, command); furi_check(osMutexRelease(cli->mutex) == osOK); if(cli_command) { cli_nl(); @@ -190,7 +190,7 @@ void cli_add_command(Cli* cli, const char* name, CliCallback callback, void* con c.context = context; furi_check(osMutexAcquire(cli->mutex, osWaitForever) == osOK); - CliCommandDict_set_at(cli->commands, name_str, c); + CliCommandTree_set_at(cli->commands, name_str, c); furi_check(osMutexRelease(cli->mutex) == osOK); string_clear(name_str); @@ -207,7 +207,7 @@ void cli_delete_command(Cli* cli, const char* name) { } while(name_replace != STRING_FAILURE); furi_check(osMutexAcquire(cli->mutex, osWaitForever) == osOK); - CliCommandDict_erase(cli->commands, name_str); + CliCommandTree_erase(cli->commands, name_str); furi_check(osMutexRelease(cli->mutex) == osOK); string_clear(name_str); diff --git a/applications/cli/cli_commands.c b/applications/cli/cli_commands.c index 38e544fa..99c6cff5 100644 --- a/applications/cli/cli_commands.c +++ b/applications/cli/cli_commands.c @@ -10,11 +10,23 @@ void cli_command_help(string_t args, void* context) { printf("Commands we have:"); furi_check(osMutexAcquire(cli->mutex, osWaitForever) == osOK); - CliCommandDict_it_t it; - for(CliCommandDict_it(it, cli->commands); !CliCommandDict_end_p(it); CliCommandDict_next(it)) { - CliCommandDict_itref_t* ref = CliCommandDict_ref(it); - printf(" "); - printf(string_get_cstr(ref->key)); + // Get the middle element + CliCommandTree_it_t it_mid; + uint8_t cmd_num = CliCommandTree_size(cli->commands); + uint8_t i = cmd_num / 2 + cmd_num % 2; + for(CliCommandTree_it(it_mid, cli->commands); i; --i, CliCommandTree_next(it_mid)) + ; + // Use 2 iterators from start and middle to show 2 columns + CliCommandTree_it_t it_i; + CliCommandTree_it_t it_j; + for(CliCommandTree_it(it_i, cli->commands), CliCommandTree_it_set(it_j, it_mid); + !CliCommandTree_it_equal_p(it_i, it_mid); + CliCommandTree_next(it_i), CliCommandTree_next(it_j)) { + CliCommandTree_itref_t* ref = CliCommandTree_ref(it_i); + printf("\r\n"); + printf("%-30s", string_get_cstr(ref->key_ptr[0])); + ref = CliCommandTree_ref(it_j); + printf(string_get_cstr(ref->key_ptr[0])); }; furi_check(osMutexRelease(cli->mutex) == osOK); diff --git a/applications/cli/cli_i.h b/applications/cli/cli_i.h old mode 100644 new mode 100755 index 578131e8..98e4e00c --- a/applications/cli/cli_i.h +++ b/applications/cli/cli_i.h @@ -6,15 +6,23 @@ #include #include +#include #define CLI_LINE_SIZE_MAX +#define CLI_COMMANDS_TREE_RANK 4 typedef struct { CliCallback callback; void* context; } CliCommand; -DICT_DEF2(CliCommandDict, string_t, STRING_OPLIST, CliCommand, M_POD_OPLIST) +BPTREE_DEF2( + CliCommandTree, + CLI_COMMANDS_TREE_RANK, + string_t, + STRING_OPLIST, + CliCommand, + M_POD_OPLIST) typedef enum { CliSymbolAsciiSOH = 0x01, @@ -31,7 +39,7 @@ typedef enum { } CliSymbols; struct Cli { - CliCommandDict_t commands; + CliCommandTree_t commands; osMutexId_t mutex; string_t line; };