Skip to content

Instantly share code, notes, and snippets.

@mazunki
Created October 22, 2021 22:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mazunki/7a166fb0e0be616eaf2cd638f309c296 to your computer and use it in GitHub Desktop.
Save mazunki/7a166fb0e0be616eaf2cd638f309c296 to your computer and use it in GitHub Desktop.
Adding support for --pre-display-cmd so one can run something like `ls /some/path | wofi --show dmenu --pre-display-cmd 'cat /some/path/%s', and output the original filename, while displaying the contents of the file in the entries
diff -r bae1c99a465f src/main.c
--- a/src/main.c Mon Feb 22 23:53:57 2021 -0800
+++ b/src/main.c Fri Oct 22 23:56:28 2021 +0200
@@ -96,6 +96,7 @@
printf("--gtk-dark\t-G\tUses the dark variant of the current GTK theme\n");
printf("--search\t-Q\tSearch for something immediately on open\n");
printf("--monitor\t-o\tSets the monitor to open on\n");
+ printf("--pre-display-cmd\t-r\tRuns command for the displayed entries, without changing the output. %%s for the real string\n");
exit(0);
}
@@ -106,7 +107,10 @@
ssize_t size = ftell(file);
fseek(file, 0, SEEK_SET);
char* data = malloc(size + 1);
- fread(data, 1, size, file);
+ if (fread(data, 1, size, file) != 0) {
+ printf("failed to read stylesheet data from file");
+ exit(EXIT_FAILURE);
+ }
fclose(file);
data[size] = 0;
@@ -422,6 +426,12 @@
.val = 'o'
},
{
+ .name = "pre-display-cmd",
+ .has_arg = required_argument,
+ .flag = NULL,
+ .val = 'r'
+ },
+ {
.name = NULL,
.has_arg = 0,
.flag = NULL,
@@ -457,13 +467,14 @@
char* gtk_dark = NULL;
char* search = NULL;
char* monitor = NULL;
+ char* pre_display_cmd = NULL;
struct wl_list options;
wl_list_init(&options);
struct option_node* node;
int opt;
- while((opt = getopt_long(argc, argv, "hfc:s:C:dS:W:H:p:x:y:nImk:t:P::ebM:iqvl:aD:L:w:O:GQ:o:", opts, NULL)) != -1) {
+ while((opt = getopt_long(argc, argv, "hfc:s:C:dS:W:H:p:x:y:nImk:t:P::ebM:iqvl:aD:L:w:O:GQ:o:r:", opts, NULL)) != -1) {
switch(opt) {
case 'h':
print_usage(argv);
@@ -572,6 +583,9 @@
case 'o':
monitor = optarg;
break;
+ case 'r':
+ pre_display_cmd = optarg;
+ break;
}
}
@@ -761,6 +775,9 @@
if(monitor != NULL) {
map_put(config, "monitor", monitor);
}
+ if(pre_display_cmd != NULL) {
+ map_put(config, "pre_display_cmd", pre_display_cmd);
+ }
struct sigaction sigact = {0};
sigact.sa_handler = sig;
diff -r bae1c99a465f src/wofi.c
--- a/src/wofi.c Mon Feb 22 23:53:57 2021 -0800
+++ b/src/wofi.c Fri Oct 22 23:56:28 2021 +0200
@@ -106,6 +106,7 @@
static pthread_t mode_thread;
static bool has_joined_mode = false;
static char* copy_exec = NULL;
+static char* pre_display_cmd = NULL;
static struct map* keys;
@@ -396,10 +397,32 @@
setup_label(mode, WOFI_PROPERTY_BOX(box));
+ char labeltext[1000];
+ if (pre_display_cmd == NULL) {
+ *labeltext = *text;
+ } else {
+ FILE *fp_labeltext;
+ char *cmd_labeltext;
+ if ((asprintf(&cmd_labeltext, pre_display_cmd, text)) == -1) {
+ printf("error parsing pre_display_cmd to run\n");
+ exit(EXIT_FAILURE);
+ }
+ fp_labeltext = popen(cmd_labeltext, "r");
+ if (fp_labeltext == NULL) {
+ printf("error executing '%s'", cmd_labeltext);
+ exit(EXIT_FAILURE);
+ }
+ if (fgets(labeltext, sizeof(labeltext), fp_labeltext) == NULL) {
+ printf("failed to read output of command");
+ exit(EXIT_FAILURE);
+ }
+ pclose(fp_labeltext);
+ }
+
if(allow_images) {
- parse_images(WOFI_PROPERTY_BOX(box), text, true);
+ parse_images(WOFI_PROPERTY_BOX(box), labeltext, true);
} else {
- GtkWidget* label = gtk_label_new(text);
+ GtkWidget* label = gtk_label_new(labeltext);
gtk_widget_set_name(label, "text");
gtk_label_set_use_markup(GTK_LABEL(label), allow_markup);
gtk_label_set_xalign(GTK_LABEL(label), 0);
@@ -536,6 +559,7 @@
if(node == NULL) {
return FALSE;
}
+ char* nodetext = node->text[0];
GtkWidget* parent;
if(node->action_count > 1 && !no_actions) {
@@ -543,7 +567,7 @@
g_signal_connect(parent, "activate", G_CALLBACK(expand), NULL);
GtkWidget* box;
if(node->builder == NULL) {
- box = create_label(node->mode, node->text[0], node->search_text, node->actions[0]);
+ box = create_label(node->mode, nodetext, node->search_text, node->actions[0]);
} else {
box = GTK_WIDGET(node->builder->box);
setup_label(node->builder->mode->name, WOFI_PROPERTY_BOX(box));
@@ -570,7 +594,7 @@
}
} else {
if(node->builder == NULL) {
- parent = create_label(node->mode, node->text[0], node->search_text, node->actions[0]);
+ parent = create_label(node->mode, nodetext, node->search_text, node->actions[0]);
} else {
parent = GTK_WIDGET(node->builder->box);
setup_label(node->builder->mode->name, WOFI_PROPERTY_BOX(parent));
@@ -1234,7 +1258,10 @@
}
int fds[2];
- pipe(fds);
+ if (pipe(fds) == -1) {
+ perror("pipe broken");
+ exit(EXIT_FAILURE);
+ }
if(fork() == 0) {
close(fds[1]);
dup2(fds[0], STDIN_FILENO);
@@ -1244,7 +1271,9 @@
}
close(fds[0]);
- write(fds[1], action, strlen(action));
+ if (!(write(fds[1], action, strlen(action)) == 0)) {
+ printf("fd pipe failed to write");
+ }
close(fds[1]);
@@ -1674,6 +1703,8 @@
char* monitor = map_get(config, "monitor");
char* layer = config_get(config, "layer", "top");
copy_exec = config_get(config, "copy_exec", "wl-copy");
+
+ pre_display_cmd = map_get(config, "pre_display_cmd");
keys = map_init_void();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment