[FL-1239] CLI commands readable list #464

Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
gornekich 2021-05-18 12:30:04 +03:00 committed by GitHub
parent 734820c137
commit 5000424c42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 11 deletions

View File

@ -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);

View File

@ -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);

12
applications/cli/cli_i.h Normal file → Executable file
View File

@ -6,15 +6,23 @@
#include <api-hal.h>
#include <m-dict.h>
#include <m-bptree.h>
#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;
};