Added dmenu-print_line_num
This commit is contained in:
		@@ -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
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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,29 +66,46 @@ void wofi_dmenu_init(struct mode* this, struct map* config) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	free(line);
 | 
						free(line);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	struct cache_line* node, *tmp;
 | 
						if(!print_line_num) {
 | 
				
			||||||
	wl_list_for_each_safe(node, tmp, cache, link) {
 | 
							struct wl_list* cache = wofi_read_cache(mode);
 | 
				
			||||||
		if(map_contains(entry_map, node->line)) {
 | 
					
 | 
				
			||||||
			map_put(cached, node->line, "true");
 | 
							struct cache_line* node, *tmp;
 | 
				
			||||||
			struct node* widget = malloc(sizeof(struct node));
 | 
							wl_list_for_each_safe(node, tmp, cache, link) {
 | 
				
			||||||
			widget->widget = wofi_create_widget(mode, &node->line, node->line, &node->line, 1);
 | 
								if(map_contains(entry_map, node->line)) {
 | 
				
			||||||
			wl_list_insert(&widgets, &widget->link);
 | 
									map_put(cached, node->line, "true");
 | 
				
			||||||
		} else {
 | 
									struct node* widget = malloc(sizeof(struct node));
 | 
				
			||||||
			wofi_remove_cache(mode, node->line);
 | 
									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);
 | 
							free(cache);
 | 
				
			||||||
		wl_list_remove(&node->link);
 | 
					 | 
				
			||||||
		free(node);
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	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;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	wofi_write_cache(mode, cmd);
 | 
						if(!print_line_num) {
 | 
				
			||||||
 | 
							wofi_write_cache(mode, cmd);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	printf("%s\n", action);
 | 
						printf("%s\n", action);
 | 
				
			||||||
	free(action);
 | 
						free(action);
 | 
				
			||||||
	exit(0);
 | 
						exit(0);
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user