Added dmenu-print_line_num

This commit is contained in:
Scoopta 2020-02-11 10:44:50 -08:00
parent 78129b3404
commit d69e4b416f
2 changed files with 40 additions and 17 deletions

View File

@ -26,6 +26,9 @@ If true the result returned by dmenu will be stripped of image escape sequences
.TP .TP
.B separator=\fICHAR\fR .B separator=\fICHAR\fR
The character used to separate dmenu entries, default is \\n. The character used to separate dmenu entries, default is \\n.
.TP
.B print_line_num=\fIBOOL\fR
When an entry is selected the number of the line the entry was on is printed instead of the entry itself. This disables caching as it's fundamentally incompatible with it.
.SH RUN CONFIG OPTIONS .SH RUN CONFIG OPTIONS
.TP .TP

View File

@ -17,10 +17,11 @@
#include <wofi.h> #include <wofi.h>
static const char* arg_names[] = {"parse_action", "separator"}; static const char* arg_names[] = {"parse_action", "separator", "print_line_num"};
static bool parse_action; static bool parse_action;
static char* separator; static char* separator;
static bool print_line_num;
static struct mode* mode; static struct mode* mode;
struct node { struct node {
@ -34,6 +35,7 @@ void wofi_dmenu_init(struct mode* this, struct map* config) {
mode = this; mode = this;
parse_action = strcmp(config_get(config, "parse_action", "false"), "true") == 0; parse_action = strcmp(config_get(config, "parse_action", "false"), "true") == 0;
separator = config_get(config, "separator", "\n"); separator = config_get(config, "separator", "\n");
print_line_num = strcmp(config_get(config, "print_line_num", "false"), "true") == 0;
if(strcmp(separator, "\\n") == 0) { if(strcmp(separator, "\\n") == 0) {
separator = "\n"; separator = "\n";
@ -44,7 +46,6 @@ void wofi_dmenu_init(struct mode* this, struct map* config) {
wl_list_init(&widgets); wl_list_init(&widgets);
struct map* cached = map_init(); struct map* cached = map_init();
struct wl_list* cache = wofi_read_cache(mode);
struct wl_list entries; struct wl_list entries;
wl_list_init(&entries); wl_list_init(&entries);
@ -65,6 +66,9 @@ void wofi_dmenu_init(struct mode* this, struct map* config) {
} }
free(line); free(line);
if(!print_line_num) {
struct wl_list* cache = wofi_read_cache(mode);
struct cache_line* node, *tmp; struct cache_line* node, *tmp;
wl_list_for_each_safe(node, tmp, cache, link) { wl_list_for_each_safe(node, tmp, cache, link) {
if(map_contains(entry_map, node->line)) { if(map_contains(entry_map, node->line)) {
@ -79,15 +83,29 @@ void wofi_dmenu_init(struct mode* this, struct map* config) {
wl_list_remove(&node->link); wl_list_remove(&node->link);
free(node); free(node);
} }
free(cache); free(cache);
}
map_free(entry_map); map_free(entry_map);
uint16_t line_num = 0;
struct cache_line* node, *tmp;
wl_list_for_each_reverse_safe(node, tmp, &entries, link) { wl_list_for_each_reverse_safe(node, tmp, &entries, link) {
if(!map_contains(cached, node->line)) { if(!map_contains(cached, node->line)) {
char* action;
if(print_line_num) {
action = malloc(6);
snprintf(action, 6, "%u", line_num++);
} else {
action = strdup(node->line);
}
struct node* widget = malloc(sizeof(struct node)); struct node* widget = malloc(sizeof(struct node));
widget->widget = wofi_create_widget(mode, &node->line, node->line, &node->line, 1); widget->widget = wofi_create_widget(mode, &node->line, node->line, &action, 1);
wl_list_insert(&widgets, &widget->link); wl_list_insert(&widgets, &widget->link);
free(action);
} }
free(node->line); free(node->line);
wl_list_remove(&node->link); wl_list_remove(&node->link);
@ -121,7 +139,9 @@ void wofi_dmenu_exec(const gchar* cmd) {
action = out; action = out;
} }
} }
if(!print_line_num) {
wofi_write_cache(mode, cmd); wofi_write_cache(mode, cmd);
}
printf("%s\n", action); printf("%s\n", action);
free(action); free(action);
exit(0); exit(0);