feat(B&W): Lua tools list sorted alphabetically (#2258)

Resolves #1886
This commit is contained in:
Peter Feerick
2022-09-03 09:01:46 +10:00
committed by GitHub
parent c8693569b0
commit bce73a9398
+48 -5
View File
@@ -20,10 +20,22 @@
*/
#include <algorithm>
#include <vector>
#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<LuaScript> 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<LuaScript> 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