commit 0bde73583ce5420573d28b18b84525787bc4b7e6
parent 45a32ce1a28b9fbbe484308bf93a0c9968664508
Author: Vetle Haflan <vetle@haflan.dev>
Date: Sun, 27 Dec 2020 23:53:06 +0000
Add usage info and remove unnecessary global vars
Diffstat:
M | senc.c | | | 41 | ++++++++++++++++++++++++++--------------- |
1 file changed, 26 insertions(+), 15 deletions(-)
diff --git a/senc.c b/senc.c
@@ -14,9 +14,9 @@
/**
* Search match level
- * WEAK example: search 'his ring' will match on 'this string'
+ * FUZZY example: search 'his ring' will match on 'this string'
*/
-enum {NONE, WEAK, NOCASE, EXACT};
+enum {NONE, FUZZY, NOCASE, EXACT};
FILE * fp;
typedef struct {
@@ -24,9 +24,6 @@ typedef struct {
int matchlvl;
} match;
-int match_i = 0;
-match matches[MAX_RESULTS];
-
int
search(char * search_string, char * filename) {
int search_len = strlen(search_string);
@@ -65,7 +62,7 @@ search(char * search_string, char * filename) {
mchars++;
}
if (mchars == search_len) {
- return WEAK;
+ return FUZZY;
}
}
return NONE;
@@ -73,13 +70,14 @@ search(char * search_string, char * filename) {
/**
- * Searches favorite paths and stores matches in global var
+ * Searches favorite paths and stores matches in given matches variable
*/
int
-search_favs(char * search_string) {
+search_favs(match * matches, char * search_string) {
char buf[LINE_MAX];
int line_match;
int read_chars;
+ int match_i = 0;
// TODO: Find out if getline is the preferred way
while (fgets(buf, LINE_MAX, fp)) {
line_match = search(search_string, buf);
@@ -97,9 +95,10 @@ search_favs(char * search_string) {
matches[match_i].matchlvl = line_match;
match_i++;
if (match_i == MAX_RESULTS) {
- return 0;
+ break;
}
}
+ return match_i;
}
int
@@ -128,9 +127,13 @@ compare_matches(const void * e1, const void * e2) {
return m2.matchlvl - m1.matchlvl;
}
-void
-free_matches() {
-
+char *
+matchlvl_to_string(int match_level) {
+ switch (match_level) {
+ case FUZZY: return "fuzzy";
+ case NOCASE: return "case ignored";
+ case EXACT: return "exact";
+ }
}
int
@@ -138,7 +141,12 @@ main(int nargs, char ** args) {
char * favs_file;
char * term;
int i;
- if (nargs < 3) {
+ if (nargs < 2) {
+ printf(
+ "Usage: senc [favorites file] <search term>\n"
+ );
+ exit(EXIT_FAILURE);
+ } else if (nargs < 3) {
favs_file = strcat(getenv("HOME"), "/.sencs");
term = args[1];
} else {
@@ -149,13 +157,16 @@ main(int nargs, char ** args) {
perror("fopen");
exit(EXIT_FAILURE);
}
+
+ match matches[MAX_RESULTS];
for (i = 0; i < MAX_RESULTS; i++) {
matches[i].filename = malloc(LINE_MAX);
}
- search_favs(term);
+ int match_i = search_favs(matches, term);
qsort(matches, match_i, sizeof(match), compare_matches);
for (i = 0; i < match_i; i++) {
- printf("%s\t%d\n", matches[i].filename, matches[i].matchlvl);
+ printf("%s\t \e[1m%s\e[0m \n", matches[i].filename,
+ matchlvl_to_string(matches[i].matchlvl));
}
fclose(fp);
}