|
19 | 19 | #include <cstdio> |
20 | 20 | #include <cstring> |
21 | 21 | #include <cstdlib> |
| 22 | +#include <cctype> |
22 | 23 | #include <ctime> |
23 | 24 | #include <thread> |
24 | 25 | #include <atomic> |
|
28 | 29 | #include "imgui.h" |
29 | 30 | #include "IconsFontAwesome6.h" |
30 | 31 |
|
| 32 | +/* case-insensitive alphabetical comparison for sorting source lists */ |
| 33 | +static int CiStrCmp(const char *a, const char *b) |
| 34 | +{ |
| 35 | + while (*a && *b) |
| 36 | + { |
| 37 | + unsigned char ca = (unsigned char)tolower((unsigned char)*a); |
| 38 | + unsigned char cb = (unsigned char)tolower((unsigned char)*b); |
| 39 | + if (ca != cb) return (int)ca - (int)cb; |
| 40 | + a++; |
| 41 | + b++; |
| 42 | + } |
| 43 | + return (*a) ? 1 : ((*b) ? -1 : 0); |
| 44 | +} |
| 45 | + |
| 46 | +/* insertion sort the (mutable) Retlector groups alphabetically by name */ |
| 47 | +static void SortRetlectorGroups(RetlectorGroup *groups, int count) |
| 48 | +{ |
| 49 | + for (int i = 1; i < count; i++) |
| 50 | + { |
| 51 | + RetlectorGroup key = groups[i]; |
| 52 | + int j = i - 1; |
| 53 | + while (j >= 0 && CiStrCmp(groups[j].name, key.name) > 0) |
| 54 | + { |
| 55 | + groups[j + 1] = groups[j]; |
| 56 | + j--; |
| 57 | + } |
| 58 | + groups[j + 1] = key; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +static int *GetCelestrakSortedIndex(void) |
| 63 | +{ |
| 64 | + static int *s_idx = NULL; |
| 65 | + static bool s_built = false; |
| 66 | + if (!s_built) |
| 67 | + { |
| 68 | + int n = NUM_CELESTRAK_SOURCES; |
| 69 | + s_idx = (int *)malloc((size_t)n * sizeof(int)); |
| 70 | + for (int i = 0; i < n; i++) s_idx[i] = i; |
| 71 | + for (int i = 1; i < n; i++) |
| 72 | + { |
| 73 | + int key = s_idx[i]; |
| 74 | + int j = i - 1; |
| 75 | + while (j >= 0 && CiStrCmp(CELESTRAK_SOURCES[s_idx[j]].name, |
| 76 | + CELESTRAK_SOURCES[key].name) > 0) |
| 77 | + { |
| 78 | + s_idx[j + 1] = s_idx[j]; |
| 79 | + j--; |
| 80 | + } |
| 81 | + s_idx[j + 1] = key; |
| 82 | + } |
| 83 | + s_built = true; |
| 84 | + } |
| 85 | + return s_idx; |
| 86 | +} |
| 87 | + |
31 | 88 | static bool PasteTleHasNameLine(const char *data) |
32 | 89 | { |
33 | 90 | const char *p = data; |
@@ -95,6 +152,7 @@ void DrawPanelDataSources(UIContext *ctx, AppConfig *cfg) |
95 | 152 | for (int i = 0; i < s_pending_count && i < MAX_RETLECTOR_GROUPS; i++) |
96 | 153 | cfg->retlector_groups[i] = s_pending[i]; |
97 | 154 | cfg->retlector_group_count = s_pending_count; |
| 155 | + SortRetlectorGroups(cfg->retlector_groups, cfg->retlector_group_count); |
98 | 156 | cfg->retlector_groups_fetched = true; |
99 | 157 | LOG_INFO("Loaded %d retlector groups", s_pending_count); |
100 | 158 | NotifyPush(NOTIFY_INFO, ICON_FA_SATELLITE_DISH, |
@@ -127,8 +185,18 @@ void DrawPanelDataSources(UIContext *ctx, AppConfig *cfg) |
127 | 185 | ImGui::SetNextItemWidth(avail_w * 0.65f); |
128 | 186 | if (ImGui::BeginCombo("##retlector_group", combo_preview)) |
129 | 187 | { |
| 188 | + static char search_buf[64] = ""; |
| 189 | + bool search_active = (search_buf[0] != '\0'); |
| 190 | + ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x); |
| 191 | + ImGui::InputTextWithHint("##retlector_search", ICON_FA_MAGNIFYING_GLASS " Search groups...", |
| 192 | + search_buf, sizeof(search_buf)); |
| 193 | + ImGui::Separator(); |
| 194 | + |
130 | 195 | for (int i = 0; i < cfg->retlector_group_count; i++) |
131 | 196 | { |
| 197 | + if (search_active && |
| 198 | + !str_contains_ic(cfg->retlector_groups[i].name, search_buf)) |
| 199 | + continue; |
132 | 200 | bool is_selected = (i == s_retlector_combo_idx); |
133 | 201 | if (ImGui::Selectable(cfg->retlector_groups[i].name, is_selected)) |
134 | 202 | s_retlector_combo_idx = i; |
@@ -171,12 +239,23 @@ void DrawPanelDataSources(UIContext *ctx, AppConfig *cfg) |
171 | 239 | if (s_celestrak_combo_idx >= NUM_CELESTRAK_SOURCES) |
172 | 240 | s_celestrak_combo_idx = 0; |
173 | 241 |
|
| 242 | + int *celestrak_idx = GetCelestrakSortedIndex(); |
174 | 243 | const char *combo_preview = CELESTRAK_SOURCES[s_celestrak_combo_idx].name; |
175 | 244 | ImGui::SetNextItemWidth(avail_w * 0.65f); |
176 | 245 | if (ImGui::BeginCombo("##celestrak_group", combo_preview)) |
177 | 246 | { |
178 | | - for (int i = 0; i < NUM_CELESTRAK_SOURCES; i++) |
| 247 | + static char search_buf[64] = ""; |
| 248 | + bool search_active = (search_buf[0] != '\0'); |
| 249 | + ImGui::SetNextItemWidth(ImGui::GetContentRegionAvail().x); |
| 250 | + ImGui::InputTextWithHint("##celestrak_search", ICON_FA_MAGNIFYING_GLASS " Search sources...", |
| 251 | + search_buf, sizeof(search_buf)); |
| 252 | + ImGui::Separator(); |
| 253 | + |
| 254 | + for (int k = 0; k < NUM_CELESTRAK_SOURCES; k++) |
179 | 255 | { |
| 256 | + int i = celestrak_idx[k]; |
| 257 | + if (search_active && !str_contains_ic(CELESTRAK_SOURCES[i].name, search_buf)) |
| 258 | + continue; |
180 | 259 | bool is_selected = (i == s_celestrak_combo_idx); |
181 | 260 | if (ImGui::Selectable(CELESTRAK_SOURCES[i].name, is_selected)) |
182 | 261 | s_celestrak_combo_idx = i; |
|
0 commit comments