Requires libbemenu
Build with
gcc -o bm-input -lbemenu bm-input.c
Command
$./bm-input -h :(
./bm-input: invalid option -- 'h'
Usage: ./bm-input [-l inputlength] [-p prompt]
/* | |
Copyright (c) 2019 Andreas Bosch | |
Permission is hereby granted, free of charge, to any person obtaining a copy of | |
this software and associated documentation files (the "Software"), to deal in | |
the Software without restriction, including without limitation the rights to | |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | |
of the Software, and to permit persons to whom the Software is furnished to do | |
so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
*/ | |
#include <bemenu.h> | |
#include <stdbool.h> | |
#include <string.h> | |
#include <stdint.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <stdlib.h> | |
int main(int argc, char *argv[]) { | |
int length = 0; | |
char opt, *title = NULL; | |
while ((opt = getopt(argc, argv, "l:p:")) != -1) { | |
switch (opt) { | |
case 'p': | |
title = optarg; | |
break; | |
case 'l': | |
length = atoi(optarg); | |
if (length < 0) length = 0; | |
break; | |
default: /* '?' */ | |
fprintf(stderr, "Usage: %s [-l inputlength] [-p prompt]\n", | |
argv[0]); | |
return 1; | |
} | |
} | |
if (!bm_init()) { | |
return 1; | |
} | |
struct bm_menu *menu = bm_menu_new(NULL); | |
if (!menu) { | |
return 1; | |
} | |
if (title) { | |
bm_menu_set_title(menu, title); | |
} | |
bm_menu_set_bottom(menu, true); | |
bm_menu_grab_keyboard(menu, true); | |
const char *filter = NULL; | |
enum bm_key key; | |
uint32_t unicode; | |
bool run = true; | |
do { | |
bm_menu_render(menu); | |
key = bm_menu_poll_key(menu, &unicode); | |
switch (bm_menu_run_with_key(menu, key, unicode)) { | |
case BM_RUN_RESULT_RUNNING: | |
if (length == 0) { | |
break; | |
} | |
// fall through if length > 0 | |
case BM_RUN_RESULT_SELECTED: | |
filter = bm_menu_get_filter(menu); | |
if (length == 0 || (filter && strlen(filter) == length)) { | |
run = false; | |
} | |
break; | |
default: | |
run = false; | |
filter = NULL; | |
break; | |
} | |
} while (run); | |
if (filter) { | |
printf("%s", filter); | |
} | |
bm_menu_free(menu); | |
return 0; | |
} |