diff options
author | Jari Vetoniemi <mailroxas@gmail.com> | 2014-04-10 01:09:35 +0300 |
---|---|---|
committer | Jari Vetoniemi <mailroxas@gmail.com> | 2014-04-10 01:10:05 +0300 |
commit | 67be25fbe43274340de89049fec7098cdf998b47 (patch) | |
tree | f8d6be283944fc90e1c224c461f6e92b3ae78d6f /client/client.c | |
parent | a3498b25f414a888aa01ffd1cf9b2e44f6c54764 (diff) | |
download | bemenu-67be25fbe43274340de89049fec7098cdf998b47.tar.gz bemenu-67be25fbe43274340de89049fec7098cdf998b47.tar.bz2 bemenu-67be25fbe43274340de89049fec7098cdf998b47.zip |
Basic working bemenu with curses backend
Diffstat (limited to 'client/client.c')
-rw-r--r-- | client/client.c | 95 |
1 files changed, 92 insertions, 3 deletions
diff --git a/client/client.c b/client/client.c index 559651f..1c30498 100644 --- a/client/client.c +++ b/client/client.c @@ -6,8 +6,83 @@ */ #include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <stddef.h> +#include <assert.h> #include <bemenu.h> +static ptrdiff_t getLine(char **outLine, size_t *outAllocated, FILE *stream) +{ + size_t len = 0, allocated; + char *s, *buffer; + + assert(outLine != NULL); + assert(outAllocated != NULL); + + if (stream == NULL || feof(stream) || ferror(stream)) + return -1; + + allocated = *outAllocated; + buffer = *outLine; + + if (buffer == NULL || allocated == 0) { + if (!(buffer = calloc(1, (allocated = 1024) + 1))) + return -1; + } + + for (s = buffer;;) { + if (fgets(s, allocated - (s - buffer), stream) == NULL) + return -1; + + len = strlen(s); + if (feof(stream)) + break; + + if (len > 0 && s[len - 1] == '\n') + break; + + if (len + 1 >= allocated - (s - buffer)) { + void *tmp = realloc(buffer, 2 * allocated); + if (!tmp) + break; + + buffer = tmp; + s = buffer + allocated - 1; + memset(s, 0, allocated - (s - buffer)); + allocated *= 2; + } else { + s += len; + } + } + + *outAllocated = allocated; + *outLine = buffer; + + if (s[len - 1] == '\n') + s[len - 1] = 0; + + return s - buffer + len; +} + +static void readItemsToMenuFromStdin(bmMenu *menu) +{ + ptrdiff_t len; + size_t size; + char *line = NULL; + + while ((len = getLine(&line, &size, stdin)) != -1) { + bmItem *item = bmItemNew((len > 0 ? line : NULL)); + if (!item) + break; + + bmMenuAddItem(menu, item); + } + + if (line) + free(line); +} + /** * Main method * @@ -21,16 +96,30 @@ int main(int argc, char **argv) { (void)argc, (void)argv; - bmMenu *menu = bmMenuNew(BM_DRAW_MODE_NONE); + bmMenu *menu = bmMenuNew(BM_DRAW_MODE_CURSES); if (!menu) return EXIT_FAILURE; - bmMenuRender(menu); + 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) { + bmItem *item = bmMenuGetSelectedItem(menu); + printf("%s\n", bmItemGetText(item)); + } bmMenuFree(menu); - return EXIT_SUCCESS; + return (status == BM_RUN_RESULT_SELECTED ? EXIT_SUCCESS : EXIT_FAILURE); } /* vim: set ts=8 sw=4 tw=0 :*/ |