Skip to content

Commit 425758e

Browse files
committed
added modules to tofi (currently math(documented) and search(undocumented) with firefox only)
1 parent 087896e commit 425758e

File tree

10 files changed

+129
-3
lines changed

10 files changed

+129
-3
lines changed

doc/tofi.5.md

+4
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,10 @@ options.
630630
>
631631
> Default: true
632632
633+
## MODULES
634+
**module-math**=true|false
635+
> Calculate Math equations inside the Input field. The Calculation result will appear inside the result-suggestions, when Selecting this result it will be copied to clipboard and a notification will be sent
636+
633637
## COLORS
634638

635639
Colors can be specified in the form *RGB*, *RGBA*, *RRGGBB* or

meson.build

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ common_sources = files(
116116
'src/surface.c',
117117
'src/unicode.c',
118118
'src/xmalloc.c',
119+
'src/modules.c',
119120
)
120121

121122
compgen_sources = files(

src/config.c

+10
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,16 @@ bool parse_option(struct tofi *tofi, const char *filename, size_t lineno, const
766766
if (!err) {
767767
tofi->use_scale = val;
768768
}
769+
} else if (strcasecmp(option, "module-math") == 0) {
770+
bool val = parse_bool(filename, lineno, value, &err);
771+
if (!err) {
772+
tofi->module_math = val;
773+
}
774+
} else if (strcasecmp(option, "module-search") == 0) {
775+
bool val = parse_bool(filename, lineno, value, &err);
776+
if (!err) {
777+
tofi->module_search = val;
778+
}
769779
} else {
770780
PARSE_ERROR(filename, lineno, "Unknown option \"%s\"\n", option);
771781
err = true;

src/desktop_vec.c

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "string_vec.h"
77
#include "unicode.h"
88
#include "xmalloc.h"
9+
#include "modules.h"
910

1011
static bool match_current_desktop(char * const *desktop_list, gsize length);
1112

@@ -148,6 +149,7 @@ struct desktop_entry *desktop_vec_find_sorted(struct desktop_vec *restrict vec,
148149
}
149150

150151
struct string_ref_vec desktop_vec_filter(
152+
struct tofi *tofi,
151153
const struct desktop_vec *restrict vec,
152154
const char *restrict substr,
153155
enum matching_algorithm algorithm)
@@ -175,6 +177,10 @@ struct string_ref_vec desktop_vec_filter(
175177
}
176178
}
177179
}
180+
181+
// add suggestions form enabled modules
182+
modules_suggest(tofi, substr, &filt);
183+
178184
/*
179185
* Sort the results by this search_score. This moves matches at the beginnings
180186
* of words to the front of the result list.

src/desktop_vec.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ void desktop_vec_add_file(struct desktop_vec *desktop, const char *id, const cha
3535

3636
void desktop_vec_sort(struct desktop_vec *restrict vec);
3737
struct desktop_entry *desktop_vec_find_sorted(struct desktop_vec *restrict vec, const char *name);
38+
struct tofi;
3839
struct string_ref_vec desktop_vec_filter(
39-
const struct desktop_vec *restrict vec,
40+
struct tofi *tofi,
41+
const struct desktop_vec *restrict vec,
4042
const char *restrict substr,
4143
enum matching_algorithm algorithm);
4244

src/input.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ void add_character(struct tofi *tofi, xkb_keycode_t keycode)
203203
entry->input_utf8_length += len;
204204

205205
if (entry->mode == TOFI_MODE_DRUN) {
206-
struct string_ref_vec results = desktop_vec_filter(&entry->apps, entry->input_utf8, tofi->matching_algorithm);
206+
struct string_ref_vec results = desktop_vec_filter(tofi, &entry->apps, entry->input_utf8, tofi->matching_algorithm);
207207
string_ref_vec_destroy(&entry->results);
208208
entry->results = results;
209209
} else {
@@ -241,7 +241,7 @@ void input_refresh_results(struct tofi *tofi)
241241
entry->input_utf8_length = bytes_written;
242242
string_ref_vec_destroy(&entry->results);
243243
if (entry->mode == TOFI_MODE_DRUN) {
244-
entry->results = desktop_vec_filter(&entry->apps, entry->input_utf8, tofi->matching_algorithm);
244+
entry->results = desktop_vec_filter(tofi, &entry->apps, entry->input_utf8, tofi->matching_algorithm);
245245
} else {
246246
entry->results = string_ref_vec_filter(&entry->commands, entry->input_utf8, tofi->matching_algorithm);
247247
}

src/main.c

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <wayland-client.h>
1414
#include <wayland-util.h>
1515
#include <xkbcommon/xkbcommon.h>
16+
#include "src/modules.h"
1617
#include "tofi.h"
1718
#include "compgen.h"
1819
#include "drun.h"
@@ -1030,6 +1031,8 @@ static bool do_submit(struct tofi *tofi)
10301031
}
10311032
}
10321033
if (app == NULL) {
1034+
if (modules_try_execute(res, entry->input_utf8))
1035+
return true;
10331036
log_error("Couldn't find application file! This shouldn't happen.\n");
10341037
return false;
10351038
}
@@ -1200,6 +1203,8 @@ int main(int argc, char *argv[])
12001203
.require_match = true,
12011204
.use_scale = true,
12021205
.physical_keybindings = true,
1206+
.module_math = true,
1207+
.module_search = false
12031208
};
12041209
wl_list_init(&tofi.output_list);
12051210
if (getenv("TERMINAL") != NULL) {

src/modules.c

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
}

src/modules.h

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef MODULES_H
2+
#define MODULES_H
3+
4+
#include "tofi.h"
5+
6+
void modules_suggest(struct tofi *tofi, char *restrict query,
7+
struct string_ref_vec *filt);
8+
bool modules_try_execute(char *suggestion, char *restrict query);
9+
10+
#endif /* MKDIRP_H */

src/tofi.h

+2
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ struct tofi {
104104
bool print_index;
105105
bool multiple_instance;
106106
bool physical_keybindings;
107+
bool module_math;
108+
bool module_search;
107109
char target_output_name[MAX_OUTPUT_NAME_LEN];
108110
char default_terminal[MAX_TERMINAL_NAME_LEN];
109111
char history_file[MAX_HISTORY_FILE_NAME_LEN];

0 commit comments

Comments
 (0)