Add possibility to use alternative path

This commit is contained in:
Isak Ellmer 2025-04-15 15:46:12 +02:00
parent fc69aa6e2b
commit 5fa012f9ad
4 changed files with 13 additions and 3 deletions

View file

@ -39,6 +39,9 @@ $PATH and runs the result.
wmenu will not directly display the keyboard input, but instead replace it wmenu will not directly display the keyboard input, but instead replace it
with asterisks. with asterisks.
*-d* _path_
wmenu will use path instead of $PATH.
*-v* *-v*
prints version information to stdout, then exits. prints version information to stdout, then exits.

9
menu.c
View file

@ -26,6 +26,7 @@
struct menu *menu_create(menu_callback callback) { struct menu *menu_create(menu_callback callback) {
struct menu *menu = calloc(1, sizeof(struct menu)); struct menu *menu = calloc(1, sizeof(struct menu));
menu->strncmp = strncmp; menu->strncmp = strncmp;
menu->path = NULL;
menu->font = "monospace 10"; menu->font = "monospace 10";
menu->normalbg = 0x222222ff; menu->normalbg = 0x222222ff;
menu->normalfg = 0xbbbbbbff; menu->normalfg = 0xbbbbbbff;
@ -86,10 +87,11 @@ static bool parse_color(const char *color, uint32_t *result) {
void menu_getopts(struct menu *menu, int argc, char *argv[]) { void menu_getopts(struct menu *menu, int argc, char *argv[]) {
const char *usage = const char *usage =
"Usage: wmenu [-biPv] [-f font] [-l lines] [-o output] [-p prompt]\n" "Usage: wmenu [-biPv] [-f font] [-l lines] [-o output] [-p prompt]\n"
"\t[-N color] [-n color] [-M color] [-m color] [-S color] [-s color]\n"; "\t[-N color] [-n color] [-M color] [-m color] [-S color] [-s color]\n"
"\t[-d path]\n";
int opt; int opt;
while ((opt = getopt(argc, argv, "bhiPvf:l:o:p:N:n:M:m:S:s:")) != -1) { while ((opt = getopt(argc, argv, "bhiPvf:l:o:p:N:n:M:m:S:s:d:")) != -1) {
switch (opt) { switch (opt) {
case 'b': case 'b':
menu->bottom = true; menu->bottom = true;
@ -145,6 +147,9 @@ void menu_getopts(struct menu *menu, int argc, char *argv[]) {
fprintf(stderr, "Invalid selection foreground color: %s", optarg); fprintf(stderr, "Invalid selection foreground color: %s", optarg);
} }
break; break;
case 'd':
menu->path = optarg;
break;
default: default:
fprintf(stderr, "%s", usage); fprintf(stderr, "%s", usage);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);

2
menu.h
View file

@ -37,6 +37,8 @@ struct menu {
bool passwd; bool passwd;
// The font used to display the menu // The font used to display the menu
char *font; char *font;
// Alternative path
const char *path;
// The number of lines to list items vertically // The number of lines to list items vertically
int lines; int lines;
// The name of the output to display on // The name of the output to display on

View file

@ -10,7 +10,7 @@
#include "xdg-activation-v1-client-protocol.h" #include "xdg-activation-v1-client-protocol.h"
static void read_items(struct menu *menu) { static void read_items(struct menu *menu) {
char *path = strdup(getenv("PATH")); char *path = menu->path ? strdup(menu->path) : strdup(getenv("PATH"));
for (char *p = strtok(path, ":"); p != NULL; p = strtok(NULL, ":")) { for (char *p = strtok(path, ":"); p != NULL; p = strtok(NULL, ":")) {
DIR *dir = opendir(p); DIR *dir = opendir(p);
if (dir == NULL) { if (dir == NULL) {