From dbc89bb7dfef89ed0e4188ba10459914078f5804 Mon Sep 17 00:00:00 2001
From: pommicket <pommicket@gmail.com>
Date: Sun, 10 Sep 2023 13:55:03 -0400
Subject: put exact matches first in selector

---
 main.c |  2 --
 ui.c   | 28 ++++++++++++++++++++++------
 2 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/main.c b/main.c
index b1f7e08..930d6da 100644
--- a/main.c
+++ b/main.c
@@ -1,6 +1,4 @@
 /*
-TODO:
-- put exact match first in selector
 FUTURE FEATURES:
 - autodetect indentation (tabs vs spaces)
 - custom file/build command associations
diff --git a/ui.c b/ui.c
index 21ba7ab..06c0240 100644
--- a/ui.c
+++ b/ui.c
@@ -184,13 +184,21 @@ void selector_end(Ted *ted, Selector *s) {
 	selector_scroll_to_cursor(ted, s);
 }
 
-static int selectory_entry_cmp_name(const void *av, const void *bv) {
-	SelectorEntry const *a = av, *b = bv;
-	return strcoll(a->name, b->name);
+static int selectory_entry_cmp_name(void *context, const void *av, const void *bv) {
+	const Selector *s = context;
+	const SelectorEntry *a = av, *b = bv;
+	
+	// put exact match of search term first
+	if (s->search_term && streq(a->name, s->search_term))
+		return -1;
+	if (s->search_term && streq(b->name, s->search_term))
+		return 1;
+
+	return strcmp_case_insensitive(a->name, b->name);
 }
 
 void selector_sort_entries_by_name(Selector *s) {
-	qsort(s->entries, arr_len(s->entries), sizeof *s->entries, selectory_entry_cmp_name);
+	qsort_with_context(s->entries, arr_len(s->entries), sizeof *s->entries, selectory_entry_cmp_name, s);
 }
 
 static Rect selector_entry_rect_unclipped(Ted *ted, Selector *s, u32 i_display) {
@@ -354,8 +362,16 @@ void file_selector_clear(FileSelector *fs) {
 }
 
 static int file_selector_entry_cmp(void *context, const SelectorEntry *a, const SelectorEntry *b) {
-	(void)context;
+	const Selector *s = context;
 	FsType a_type = (FsType)a->userdata, b_type = (FsType)b->userdata;
+
+
+	// put exact match of search term first
+	if (s->search_term && streq(a->name, s->search_term))
+		return -1;
+	if (s->search_term && streq(b->name, s->search_term))
+		return 1;
+
 	// put directories first
 	if (a_type == FS_DIRECTORY && b_type != FS_DIRECTORY) {
 		return -1;
@@ -602,7 +618,7 @@ char *file_selector_update(Ted *ted, FileSelector *fs) {
 			};
 			selector_add_entry(&fs->sel, &entry);
 		}
-		selector_sort_entries(&fs->sel, file_selector_entry_cmp, NULL);
+		selector_sort_entries(&fs->sel, file_selector_entry_cmp, &fs->sel);
 
 		for (u32 i = 0; files[i]; ++i)
 			free(files[i]);
-- 
cgit v1.2.3