[colorlcd] Sort Lua scripts before building UI in radio tools (#1150)

* sort lua scripts before building ui in radio tools

* Formatting

* Case insensitive sorting

Co-authored-by: kkoenig1@optonline.com <kkoenig1@optonline.com>
Co-authored-by: Peter Feerick <peter.feerick@gmail.com>
This commit is contained in:
Kevin Koenig
2021-12-05 23:50:52 +11:00
committed by GitHub
co-authored by kkoenig1@optonline.com <kkoenig1@optonline.com> Peter Feerick
parent 71dd6bca89
commit 9694689067
2 changed files with 66 additions and 41 deletions
+67 -43
View File
@@ -72,61 +72,34 @@ void RadioToolsPage::checkEvents()
PageTab::checkEvents(); PageTab::checkEvents();
} }
void RadioToolsPage::rebuild(FormWindow * window) struct LuaScript
{ {
FormGridLayout grid; std::string path;
grid.spacer(PAGE_PADDING); std::string label;
grid.setLabelWidth(100); bool operator < (const LuaScript &a) { return label < a.label; }
};
window->clear(); inline bool LuaScript_compare_nocase(LuaScript first, LuaScript second)
Window::clearFocus(); {
return strcasecmp(first.label.c_str(), second.label.c_str()) < 0;
// LUA scripts in TOOLS
#if defined(LUA)
FILINFO fno;
DIR dir;
#if defined(CROSSFIRE)
// if (isFileAvailable(SCRIPTS_TOOLS_PATH "/CROSSFIRE/crossfire.lua"))
// addRadioScriptTool(index++, SCRIPTS_TOOLS_PATH "/CROSSFIRE/crossfire.lua");
#endif
FRESULT res = f_opendir(&dir, SCRIPTS_TOOLS_PATH);
if (res == FR_OK) {
for (;;) {
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)) {
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);
} }
void buildLuaUi(std::vector<LuaScript> luaScripts, FormWindow *window, FormGridLayout &grid)
{
for (auto luaScript : luaScripts) {
auto txt = new StaticText(window, grid.getLabelSlot(), "lua", auto txt = new StaticText(window, grid.getLabelSlot(), "lua",
BUTTON_BACKGROUND, COLOR_THEME_PRIMARY1 | CENTERED); BUTTON_BACKGROUND, COLOR_THEME_PRIMARY1 | CENTERED);
std::string path_str(path);
auto b = new TextButton( auto b = new TextButton(
window, grid.getFieldSlot(1), label, window, grid.getFieldSlot(1), luaScript.label,
[window, path_str]() -> uint8_t { [window, luaScript]() -> uint8_t {
char toolPath[FF_MAX_LFN + 1]; char toolPath[FF_MAX_LFN + 1];
strncpy(toolPath, path_str.c_str(), sizeof(toolPath)-1); strncpy(toolPath, luaScript.path.c_str(), sizeof(toolPath)-1);
*((char *)getBasename(toolPath)-1) = '\0'; *((char *)getBasename(toolPath)-1) = '\0';
f_chdir(toolPath); f_chdir(toolPath);
luaExec(path_str.c_str()); luaExec(luaScript.path.c_str());
StandaloneLuaWindow::instance()->attach(window); StandaloneLuaWindow::instance()->attach(window);
return 0; return 0;
@@ -148,6 +121,57 @@ void RadioToolsPage::rebuild(FormWindow * window)
grid.nextLine(); grid.nextLine();
} }
} }
void RadioToolsPage::rebuild(FormWindow * window)
{
FormGridLayout grid;
grid.spacer(PAGE_PADDING);
grid.setLabelWidth(100);
window->clear();
Window::clearFocus();
// LUA scripts in TOOLS
#if defined(LUA)
FILINFO fno;
DIR dir;
#if defined(CROSSFIRE)
// if (isFileAvailable(SCRIPTS_TOOLS_PATH "/CROSSFIRE/crossfire.lua"))
// addRadioScriptTool(index++, SCRIPTS_TOOLS_PATH "/CROSSFIRE/crossfire.lua");
#endif
FRESULT res = f_opendir(&dir, SCRIPTS_TOOLS_PATH);
if (res == FR_OK) {
std::vector<LuaScript> luaScripts; // gather them all and then create UI after sort
for (;;) {
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)) {
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 });
}
}
std::sort(luaScripts.begin(), luaScripts.end(), LuaScript_compare_nocase);
buildLuaUi(luaScripts, window, grid);
} }
#endif #endif
+2 -1
View File
@@ -21,7 +21,8 @@
#include "tabsgroup.h" #include "tabsgroup.h"
class RadioToolsPage: public PageTab { class RadioToolsPage : public PageTab
{
public: public:
RadioToolsPage(); RadioToolsPage();