|
| 1 | +#include "modules.h" |
| 2 | +#include "history.h" |
| 3 | +#include "string_vec.h" |
| 4 | +#include "unicode.h" |
| 5 | +#include <stdio.h> |
| 6 | +#include <stdlib.h> |
| 7 | +#include <string.h> |
| 8 | + |
| 9 | +void module_math_suggestion(const char *restrict query, |
| 10 | + struct string_ref_vec *filt) { |
| 11 | + char command[128] = "qalc -t -m 1000 \""; |
| 12 | + command[sizeof(command) - 1] = '\0'; // Ensure null termination |
| 13 | + strncat(command, query, sizeof(command) - 1); |
| 14 | + strncat(command, "\"", sizeof(command) - 1); |
| 15 | + |
| 16 | + char result[128] = ""; |
| 17 | + |
| 18 | + // execute command |
| 19 | + FILE *fp = popen(command, "r"); |
| 20 | + // limit output to 50 characters |
| 21 | + fgets(result, 50, fp); |
| 22 | + pclose(fp); |
| 23 | + |
| 24 | + // add module prefix |
| 25 | + char finalresult[128] = "="; |
| 26 | + strncat(finalresult, result, sizeof(finalresult) - 1); |
| 27 | + finalresult[strlen(finalresult) - 1] = '\0'; // Ensure null termination |
| 28 | + |
| 29 | + string_ref_vec_add(filt, utf8_normalize(finalresult)); |
| 30 | +} |
| 31 | + |
| 32 | +bool module_math_selected(char *suggestion, char *query) { |
| 33 | + char notify[128] = "notify-send \"Calculation: "; |
| 34 | + // set notification title to be the calculation |
| 35 | + strncat(notify, query, sizeof(notify) - 1); |
| 36 | + strncat(notify, "\" \"", sizeof(notify) - 1); |
| 37 | + // set notification body to be result |
| 38 | + strncat(notify, suggestion, sizeof(notify) - 1); |
| 39 | + strncat(notify, "\"", sizeof(notify) - 1); |
| 40 | + |
| 41 | + char copy[128] = "wl-copy \""; |
| 42 | + strncat(copy, suggestion, sizeof(notify) - 1); |
| 43 | + strncat(copy, "\"", sizeof(notify) - 1); |
| 44 | + |
| 45 | + system(copy); |
| 46 | + system(notify); |
| 47 | + |
| 48 | + // return is here to avoid an ugly switch |
| 49 | + return true; |
| 50 | +} |
| 51 | + |
| 52 | +void module_search_suggestion(char *restrict query, |
| 53 | + struct string_ref_vec *restrict filt) { |
| 54 | + // always display this to avoid duplicate string |
| 55 | + string_ref_vec_add(filt, "?search"); |
| 56 | +} |
| 57 | + |
| 58 | +bool module_search_selected(char *restrict query) { |
| 59 | + char command[128] = "firefox --search \""; |
| 60 | + strncat(command, query, sizeof(command) - 1); |
| 61 | + strncat(command, "\"", sizeof(command) - 1); |
| 62 | + |
| 63 | + system(command); |
| 64 | + // return is here to avoid an ugly switch |
| 65 | + return true; |
| 66 | +} |
| 67 | + |
| 68 | +void modules_suggest(struct tofi *tofi, char *restrict query, |
| 69 | + struct string_ref_vec *filt) { |
| 70 | + if (tofi->module_math) |
| 71 | + module_math_suggestion(query, filt); |
| 72 | + if (tofi->module_search) |
| 73 | + module_search_suggestion(query, filt); |
| 74 | +} |
| 75 | + |
| 76 | +bool modules_try_execute(char *suggestion, char *restrict query) { |
| 77 | + char prefix = suggestion[0]; |
| 78 | + // remove first character (prefix) |
| 79 | + suggestion++; |
| 80 | + switch (prefix) { |
| 81 | + case '=': |
| 82 | + return module_math_selected(suggestion, query); |
| 83 | + case '?': |
| 84 | + return module_search_selected(query); |
| 85 | + } |
| 86 | +} |
0 commit comments