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

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