Skip to content

Instantly share code, notes, and snippets.

@progandy
Last active April 22, 2024 15:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save progandy/2c05fc5f0b4e76d92002263776549bd1 to your computer and use it in GitHub Desktop.
Save progandy/2c05fc5f0b4e76d92002263776549bd1 to your computer and use it in GitHub Desktop.
minimal i3-input replacement built with bemenu

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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment