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

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

Merged
merged 1 commit into from
Sep 2, 2022
Merged
Changes from all commits
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
53 changes: 48 additions & 5 deletions radio/src/gui/common/stdlcd/radio_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -88,27 +115,43 @@ 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 */
if (fno.fattrib & AM_HID) continue; /* Skip hidden files */
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

Expand Down