Skip to content

Commit 1a6ac67

Browse files
committed
i was stupid i only needed to copy the strings that have a score > 0
also len is taken into account now
1 parent ef2256e commit 1a6ac67

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/cli.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
typedef struct {
1010
char *str;
1111
int32_t score;
12+
size_t len;
1213
} fzf_tuple_t;
1314

1415
typedef struct {
@@ -50,12 +51,14 @@ void swap(fzf_tuple_t *x, fzf_tuple_t *y) {
5051

5152
// take strlen in consideration if lhs == rhs
5253
static bool smaller(fzf_tuple_t *lhs, fzf_tuple_t *rhs) {
53-
return lhs->score < rhs->score;
54+
return (lhs->score == rhs->score && lhs->len > rhs->len) ||
55+
lhs->score < rhs->score;
5456
}
5557

5658
// take strlen in consideration if lhs == rhs
5759
bool greater(fzf_tuple_t *lhs, fzf_tuple_t *rhs) {
58-
return lhs->score > rhs->score;
60+
return (lhs->score == rhs->score && lhs->len < rhs->len) ||
61+
lhs->score > rhs->score;
5962
}
6063

6164
static void insert(heap_t *heap, fzf_tuple_t data) {
@@ -111,19 +114,19 @@ int main(int argc, char **argv) {
111114
size_t len = 0;
112115
ssize_t read;
113116
while ((read = getline(&line, &len, stdin)) != -1) {
114-
char *copy = (char *)malloc(sizeof(char) * read);
115-
strncpy(copy, line, read - 1);
116-
copy[read - 1] = '\0';
117-
118-
int32_t score = fzf_get_score(copy, pattern, slab);
117+
line[read - 1] = '\0';
118+
int32_t score = fzf_get_score(line, pattern, slab);
119119
if (score > 0) {
120-
insert(heap, (fzf_tuple_t){.str = copy, .score = score});
120+
char *copy = (char *)malloc(sizeof(char) * read);
121+
strncpy(copy, line, read - 1);
122+
copy[read - 1] = '\0';
123+
insert(heap, (fzf_tuple_t){.str = copy, .score = score, .len = read - 1});
121124
}
122125
}
123126

124127
while (heap->len > 0) {
125128
fzf_tuple_t el = extract_max(heap);
126-
printf("%s (%d)\n", el.str, el.score);
129+
printf("%s\n", el.str);
127130
free(el.str);
128131
}
129132
free_heap(heap);

0 commit comments

Comments
 (0)