From bce73a9398f1f1b698d9e03afa29ae4fc4be2cc3 Mon Sep 17 00:00:00 2001 From: Peter Feerick Date: Sat, 3 Sep 2022 09:01:46 +1000 Subject: [PATCH] feat(B&W): Lua tools list sorted alphabetically (#2258) Resolves #1886 --- radio/src/gui/common/stdlcd/radio_tools.cpp | 53 +++++++++++++++++++-- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/radio/src/gui/common/stdlcd/radio_tools.cpp b/radio/src/gui/common/stdlcd/radio_tools.cpp index fa44b47e5..4f17601c1 100644 --- a/radio/src/gui/common/stdlcd/radio_tools.cpp +++ b/radio/src/gui/common/stdlcd/radio_tools.cpp @@ -20,10 +20,22 @@ */ #include +#include #include "opentx.h" extern uint8_t g_moduleIdx; +struct LuaScript { + std::string path; + std::string label; + bool operator<(const LuaScript &a) { return label < a.label; } +}; + +inline bool LuaScript_compare_nocase(LuaScript first, LuaScript second) +{ + return strcasecmp(first.label.c_str(), second.label.c_str()) < 0; +} + bool addRadioTool(uint8_t index, const char * label) { if (index >= menuVerticalOffset) { @@ -53,6 +65,21 @@ void addRadioModuleTool(uint8_t index, const char * label, void (* tool)(event_t } #if defined(LUA) +void addRadioScriptTool(std::vector luaScripts) +{ + uint8_t index = 0; + for (auto luaScript : luaScripts) { + if (addRadioTool(index++, luaScript.label.c_str())) { + char toolPath[FF_MAX_LFN + 1]; + strncpy(toolPath, luaScript.path.c_str(), sizeof(toolPath) - 1); + *((char *)getBasename(toolPath) - 1) = '\0'; + f_chdir(toolPath); + + luaExec(luaScript.path.c_str()); + } + } +} + void addRadioScriptTool(uint8_t index, const char * path) { char toolName[RADIO_TOOL_NAME_MAXLEN + 1]; @@ -88,16 +115,17 @@ void menuRadioTools(event_t event) uint8_t index = 0; - - #if defined(LUA) FILINFO fno; DIR dir; FRESULT res = f_opendir(&dir, SCRIPTS_TOOLS_PATH); if (res == FR_OK) { + std::vector luaScripts; // gather and sort before adding to menu + for (;;) { - TCHAR path[FF_MAX_LFN+1] = SCRIPTS_TOOLS_PATH "/"; + TCHAR path[FF_MAX_LFN + 1] = SCRIPTS_TOOLS_PATH "/"; + res = f_readdir(&dir, &fno); /* Read a directory item */ if (res != FR_OK || fno.fname[0] == 0) break; /* Break on error or end of dir */ if (fno.fattrib & AM_DIR) continue; /* Skip subfolders */ @@ -105,10 +133,25 @@ void menuRadioTools(event_t event) if (fno.fattrib & AM_SYS) continue; /* Skip system files */ strcat(path, fno.fname); - if (isRadioScriptTool(fno.fname)) - addRadioScriptTool(index++, path); + if (isRadioScriptTool(fno.fname)) { + char toolName[RADIO_TOOL_NAME_MAXLEN + 1] = {0}; + const char *label; + char *ext = (char *)getFileExtension(path); + if (readToolName(toolName, path)) { + label = toolName; + } else { + *ext = '\0'; + label = getBasename(path); + } + + luaScripts.emplace_back(LuaScript{path, label}); + } } f_closedir(&dir); + + std::sort(luaScripts.begin(), luaScripts.end(), LuaScript_compare_nocase); + addRadioScriptTool(luaScripts); + index += luaScripts.size(); } #endif