Skip to content

Commit

Permalink
FEAT: App list sorting tool (asc/desc A-Z) (#1300)
Browse files Browse the repository at this point in the history
## Needs to be merged after Mainui sig handling #1193 as it uses -2, and
#1299 unless rebased after this merges (as the NUM_TOOLS needs to be
updated)

- Tested on MM+ FW 0628, MM 1027, MM 0611
- Tested with Sandisk Ultra cards

## Adds a tool to sort the Apps list A-Z or Z-A based on their
config.json LABEL.

- Will add 2 tools to the Tweaks -> Tools menu
  • Loading branch information
XK9274 committed Dec 11, 2023
1 parent c769451 commit e31cb84
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/tweaks/menus.h
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,18 @@ void menu_tools(void *_)
"This generates a 'miyoogamelist.xml' file\n"
"which comes with some limitations, such\n"
"as no subfolder support.");
list_addItemWithInfoNote(&_menu_tools,
(ListItem){
.label = "Sort applist A-Z",
.action = tool_sortAppsAZ},
"Use this tool to sort your App list\n"
"ascending from A to Z.\n");
list_addItemWithInfoNote(&_menu_tools,
(ListItem){
.label = "Sort applist Z-A",
.action = tool_sortAppsZA},
"Use this tool to sort your App list\n"
"descending from Z to A.\n");
}
menu_stack[++menu_level] = &_menu_tools;
header_changed = true;
Expand Down
14 changes: 13 additions & 1 deletion src/tweaks/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,23 @@ void tool_generateMiyoogamelists(void *pt)
_runCommandPopup(tools_short_names[4], "/mnt/SDCARD/.tmp_update/script/miyoogamelist_gen.sh");
}

void tool_sortAppsAZ(void *pt)
{
_runCommandPopup(tools_short_names[3], "/mnt/SDCARD/.tmp_update/script/app_sorter.sh");
}

void tool_sortAppsZA(void *pt)
{
_runCommandPopup(tools_short_names[4], "/mnt/SDCARD/.tmp_update/script/app_sorter.sh desc");
}

static void (*tools_pt[NUM_TOOLS])(void *) = {
tool_generateCueFiles,
tool_generateM3uFiles_sd,
tool_generateM3uFiles_md,
tool_buildShortRomGameList,
tool_generateMiyoogamelists};
tool_generateMiyoogamelists,
tool_sortAppsAZ,
tool_sortAppsZA};

#endif // TWEAKS_TOOLS_H__
4 changes: 3 additions & 1 deletion src/tweaks/tools_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ static char tools_short_names[NUM_TOOLS][STR_MAX] = {
"m3u_gen_sd",
"m3u_gen_md",
"build_short_rom_game_list",
"miyoogamelist_gen"};
"miyoogamelist_gen",
"sort_apps_az",
"sort_apps_za"};

#endif // TWEAKS_TOOLS_DEFS_H__
53 changes: 53 additions & 0 deletions static/build/.tmp_update/script/app_sorter.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
create_temp_dir() {
mkdir -p /mnt/SDCARD/App.temp
for dir in /mnt/SDCARD/App/*/; do
if [ -f "$dir/config.json" ]; then
mv "$dir" /mnt/SDCARD/App.temp/
fi
done
}

read_and_store_labels() {
temp_file="/mnt/SDCARD/temp_labels.txt"
: > "$temp_file"

for dir in /mnt/SDCARD/App.temp/*/; do
if [ -f "$dir/config.json" ]; then
label=$(awk -F'"' '/"label":/ {print $4}' "$dir/config.json")
echo "$label:$dir" >> "$temp_file"
fi
done
}

sort_and_copy_back() {
sort_order="$1"
if [ "$sort_order" = "desc" ]; then
sort_flag="-rf"
else
sort_flag="-f"
fi

sort $sort_flag "$temp_file" | while IFS=: read -r label dir; do
mv "$dir" /mnt/SDCARD/App/
done
}

cleanup() {
#restart mainui to refresh app list (if called from a button/script while main is running)
if pgrep "MainUI" > /dev/null; then
pkill -2 "MainUI"
fi

rm -rf /mnt/SDCARD/App.temp
rm "$temp_file"
}

main() {
sort_order="${1:-asc}"
create_temp_dir
read_and_store_labels
sort_and_copy_back "$sort_order"
cleanup
}

main "$@"

0 comments on commit e31cb84

Please sign in to comment.