bemenu: Reuse code more

This commit is contained in:
Jari Vetoniemi 2017-02-14 21:51:02 +02:00
parent 0ff5f20178
commit 8cfa530b89
4 changed files with 32 additions and 34 deletions

View File

@ -251,6 +251,13 @@ launch(const char *bin)
} }
} }
static void
item_cb(struct bm_item *item, const char *text)
{
(void)item; // may be null
launch(text);
}
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
@ -272,22 +279,7 @@ main(int argc, char **argv)
return EXIT_FAILURE; return EXIT_FAILURE;
read_items_to_menu_from_path(menu); read_items_to_menu_from_path(menu);
bm_menu_set_highlighted_index(menu, client.selected); const enum bm_run_result status = run_menu(&client, menu, item_cb);
enum bm_run_result status = run_menu(menu);
if (status == BM_RUN_RESULT_SELECTED) {
uint32_t i, count;
struct bm_item **items = bm_menu_get_selected_items(menu, &count);
for (i = 0; i < count; ++i) {
const char *text = bm_item_get_text(items[i]);
launch(text);
}
if (!count && bm_menu_get_filter(menu))
launch(bm_menu_get_filter(menu));
}
bm_menu_free(menu); bm_menu_free(menu);
return (status == BM_RUN_RESULT_SELECTED ? EXIT_SUCCESS : EXIT_FAILURE); return (status == BM_RUN_RESULT_SELECTED ? EXIT_SUCCESS : EXIT_FAILURE);
} }

View File

@ -57,6 +57,13 @@ read_items_to_menu_from_stdin(struct bm_menu *menu)
free(buffer); free(buffer);
} }
static void
item_cb(struct bm_item *item, const char *text)
{
(void)item; // may be null
printf("%s\n", (text ? text : ""));
}
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
@ -70,22 +77,7 @@ main(int argc, char **argv)
return EXIT_FAILURE; return EXIT_FAILURE;
read_items_to_menu_from_stdin(menu); read_items_to_menu_from_stdin(menu);
bm_menu_set_highlighted_index(menu, client.selected); const enum bm_run_result status = run_menu(&client, menu, item_cb);
enum bm_run_result status = run_menu(menu);
if (status == BM_RUN_RESULT_SELECTED) {
uint32_t i, count;
struct bm_item **items = bm_menu_get_selected_items(menu, &count);
for (i = 0; i < count; ++i) {
const char *text = bm_item_get_text(items[i]);
printf("%s\n", (text ? text : ""));
}
if (!count && bm_menu_get_filter(menu))
printf("%s\n", bm_menu_get_filter(menu));
}
bm_menu_free(menu); bm_menu_free(menu);
return (status == BM_RUN_RESULT_SELECTED ? EXIT_SUCCESS : EXIT_FAILURE); return (status == BM_RUN_RESULT_SELECTED ? EXIT_SUCCESS : EXIT_FAILURE);
} }

View File

@ -275,8 +275,9 @@ menu_with_options(const struct client *client)
} }
enum bm_run_result enum bm_run_result
run_menu(struct bm_menu *menu) run_menu(const struct client *client, struct bm_menu *menu, void (*item_cb)(struct bm_item *item, const char *text))
{ {
bm_menu_set_highlighted_index(menu, client->selected);
bm_menu_grab_keyboard(menu, true); bm_menu_grab_keyboard(menu, true);
uint32_t unicode; uint32_t unicode;
@ -286,6 +287,19 @@ run_menu(struct bm_menu *menu)
bm_menu_render(menu); bm_menu_render(menu);
key = bm_menu_poll_key(menu, &unicode); key = bm_menu_poll_key(menu, &unicode);
} while ((status = bm_menu_run_with_key(menu, key, unicode)) == BM_RUN_RESULT_RUNNING); } while ((status = bm_menu_run_with_key(menu, key, unicode)) == BM_RUN_RESULT_RUNNING);
if (status == BM_RUN_RESULT_SELECTED) {
uint32_t i, count;
struct bm_item **items = bm_menu_get_selected_items(menu, &count);
for (i = 0; i < count; ++i) {
const char *text = bm_item_get_text(items[i]);
item_cb(items[i], text);
}
if (!count && bm_menu_get_filter(menu))
item_cb(NULL, bm_menu_get_filter(menu));
}
return status; return status;
} }

View File

@ -20,7 +20,7 @@ struct client {
void parse_args(struct client *client, int *argc, char **argv[]); void parse_args(struct client *client, int *argc, char **argv[]);
struct bm_menu* menu_with_options(const struct client *client); struct bm_menu* menu_with_options(const struct client *client);
enum bm_run_result run_menu(struct bm_menu *menu); enum bm_run_result run_menu(const struct client *client, struct bm_menu *menu, void (*item_cb)(struct bm_item *item, const char *text));
#endif /* _BM_COMMON_H_ */ #endif /* _BM_COMMON_H_ */