2014-03-18 17:35:10 +00:00
|
|
|
#include <stdlib.h>
|
2014-04-09 22:09:35 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
2014-03-28 19:33:20 +00:00
|
|
|
#include <bemenu.h>
|
2014-03-18 17:35:10 +00:00
|
|
|
|
2014-04-12 16:21:40 +00:00
|
|
|
static void readItemsToMenuFromStdin(bmMenu *menu)
|
2014-04-09 22:09:35 +00:00
|
|
|
{
|
2014-04-12 16:21:40 +00:00
|
|
|
assert(menu);
|
2014-04-09 22:09:35 +00:00
|
|
|
|
2014-04-12 16:21:40 +00:00
|
|
|
size_t step = 1024, allocated;
|
|
|
|
char *buffer;
|
2014-04-09 22:09:35 +00:00
|
|
|
|
2014-04-12 16:21:40 +00:00
|
|
|
if (!(buffer = malloc((allocated = step))))
|
|
|
|
return;
|
2014-04-09 22:09:35 +00:00
|
|
|
|
2014-04-12 16:21:40 +00:00
|
|
|
size_t read;
|
|
|
|
while ((read = fread(buffer + (allocated - step), 1, step, stdin)) == step) {
|
|
|
|
void *tmp;
|
|
|
|
if (!(tmp = realloc(buffer, (allocated += step)))) {
|
|
|
|
free(buffer);
|
|
|
|
return;
|
2014-04-09 22:09:35 +00:00
|
|
|
}
|
2014-04-12 16:21:40 +00:00
|
|
|
buffer = tmp;
|
2014-04-09 22:09:35 +00:00
|
|
|
}
|
2014-04-12 16:21:40 +00:00
|
|
|
buffer[allocated - step + read - 1] = 0;
|
2014-04-09 22:09:35 +00:00
|
|
|
|
2014-04-12 16:59:21 +00:00
|
|
|
size_t pos;
|
|
|
|
char *s = buffer;
|
|
|
|
while ((pos = strcspn(s, "\n")) != 0) {
|
2014-04-12 17:16:33 +00:00
|
|
|
size_t next = pos + (s[pos] != 0);
|
2014-04-12 16:59:21 +00:00
|
|
|
s[pos] = 0;
|
2014-04-12 17:16:33 +00:00
|
|
|
|
2014-04-12 16:21:40 +00:00
|
|
|
bmItem *item = bmItemNew(s);
|
2014-04-09 22:09:35 +00:00
|
|
|
if (!item)
|
|
|
|
break;
|
|
|
|
|
|
|
|
bmMenuAddItem(menu, item);
|
2014-04-12 17:16:33 +00:00
|
|
|
s += next;
|
2014-04-09 22:09:35 +00:00
|
|
|
}
|
|
|
|
|
2014-04-12 16:21:40 +00:00
|
|
|
free(buffer);
|
2014-04-09 22:09:35 +00:00
|
|
|
}
|
|
|
|
|
2014-03-18 17:35:10 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
(void)argc, (void)argv;
|
|
|
|
|
2014-04-09 22:09:35 +00:00
|
|
|
bmMenu *menu = bmMenuNew(BM_DRAW_MODE_CURSES);
|
2014-03-28 19:33:20 +00:00
|
|
|
if (!menu)
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
2014-04-09 22:09:35 +00:00
|
|
|
bmMenuSetTitle(menu, "bemenu");
|
|
|
|
readItemsToMenuFromStdin(menu);
|
|
|
|
|
|
|
|
bmKey key;
|
|
|
|
unsigned int unicode;
|
|
|
|
int status = 0;
|
|
|
|
do {
|
|
|
|
bmMenuRender(menu);
|
|
|
|
key = bmMenuGetKey(menu, &unicode);
|
|
|
|
} while ((status = bmMenuRunWithKey(menu, key, unicode)) == BM_RUN_RESULT_RUNNING);
|
|
|
|
|
|
|
|
if (status == BM_RUN_RESULT_SELECTED) {
|
2014-04-10 17:04:06 +00:00
|
|
|
unsigned int i, count;
|
|
|
|
bmItem **items = bmMenuGetSelectedItems(menu, &count);
|
2014-04-12 11:15:37 +00:00
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
const char *text = bmItemGetText(items[i]);
|
|
|
|
printf("%s\n", (text ? text : ""));
|
|
|
|
}
|
2014-04-12 11:38:42 +00:00
|
|
|
|
|
|
|
if (!count && bmMenuGetFilter(menu))
|
|
|
|
printf("%s\n", bmMenuGetFilter(menu));
|
2014-04-09 22:09:35 +00:00
|
|
|
}
|
2014-03-28 19:33:20 +00:00
|
|
|
|
|
|
|
bmMenuFree(menu);
|
2014-04-09 22:09:35 +00:00
|
|
|
return (status == BM_RUN_RESULT_SELECTED ? EXIT_SUCCESS : EXIT_FAILURE);
|
2014-03-18 17:35:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vim: set ts=8 sw=4 tw=0 :*/
|