summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJari Vetoniemi <mailroxas@gmail.com>2014-04-12 22:12:44 +0300
committerJari Vetoniemi <mailroxas@gmail.com>2014-04-12 22:12:44 +0300
commit06972a357983a4686dc37a2aa5a8b0bfd2dd4e7e (patch)
treef7bf0ed9b43250b5560e684cde5efd82ac0841e9
parent01ff5f48edde73364d8f4b11f4f5480c8cc86789 (diff)
downloadbemenu-06972a357983a4686dc37a2aa5a8b0bfd2dd4e7e.tar.gz
bemenu-06972a357983a4686dc37a2aa5a8b0bfd2dd4e7e.tar.bz2
bemenu-06972a357983a4686dc37a2aa5a8b0bfd2dd4e7e.zip
CLI interface, aka option parsing.
-rw-r--r--client/client.c142
-rw-r--r--lib/CMakeLists.txt7
-rw-r--r--lib/bemenu.h21
-rw-r--r--lib/library.c8
-rw-r--r--lib/version.h.in3
5 files changed, 177 insertions, 4 deletions
diff --git a/client/client.c b/client/client.c
index 2f95d52..fb94ea5 100644
--- a/client/client.c
+++ b/client/client.c
@@ -3,6 +3,140 @@
#include <string.h>
#include <assert.h>
#include <bemenu.h>
+#include <getopt.h>
+
+static struct {
+ bmFilterMode filterMode;
+ int wrap;
+ unsigned int lines;
+ const char *title;
+ int selected;
+ int bottom;
+ int grab;
+ int monitor;
+} client = {
+ BM_FILTER_MODE_DMENU, /* filterMode */
+ 0, /* wrap */
+ 0, /* lines */
+ "bemenu", /* title */
+ 0, /* selected */
+ 0, /* bottom */
+ 0, /* grab */
+ 0 /* monitor */
+};
+
+static void printVersion(const char *name)
+{
+ char *base = strrchr(name, '/');
+ printf("%s v%s\n", (base ? base + 1 : name), bmVersion());
+ puts("<o/ \e[5mDISCO\e[m");
+}
+
+static void usage(FILE *out, const char *name)
+{
+ char *base = strrchr(name, '/');
+ fprintf(out, "usage: %s [options]\n", (base ? base + 1 : name));
+ fputs("Options\n"
+ " -h, --help display this help and exit.\n"
+ " -v, --version display version.\n"
+ " -i, --ignorecase match items case insensitively.\n"
+ " -w, --wrap wraps cursor selection.\n"
+ " -l, --list list items vertically with the given number of lines.\n"
+ " -p, --prompt defines the prompt text to be displayed.\n"
+ " -I, --index select item at index automatically.\n\n"
+
+ "Backend specific options\n"
+ " c = ncurses\n" // x == x11
+ " (...) At end of help indicates the backend support for option.\n\n"
+
+ " -b, --bottom appears at the bottom of the screen. ()\n"
+ " -f, --grab grabs the keyboard before reading stdin. ()\n"
+ " -m, --monitor index of monitor where menu will appear. ()\n"
+ " -fn, --fn defines the font to be used. ()\n"
+ " -nb, --nb defines the normal background color. ()\n"
+ " -nf, --nf defines the normal foreground color. ()\n"
+ " -sb, --sb defines the selected background color. ()\n"
+ " -sf, --sf defines the selected foreground color. ()\n"
+
+
+ , out);
+ exit((out == stderr ? EXIT_FAILURE : EXIT_SUCCESS));
+}
+
+static void parseArgs(int *argc, char **argv[])
+{
+ static const struct option opts[] = {
+ { "help", no_argument, 0, 'h' },
+ { "version", no_argument, 0, 'v' },
+
+ { "ignorecase", no_argument, 0, 'i' },
+ { "wrap", no_argument, 0, 'w' },
+ { "list", required_argument, 0, 'l' },
+ { "prompt", required_argument, 0, 'p' },
+ { "index", required_argument, 0, 'I' },
+
+ { "bottom", no_argument, 0, 'b' },
+ { "grab", no_argument, 0, 'f' },
+ { "monitor", required_argument, 0, 'm' },
+ { "fn", required_argument, 0, 0x100 },
+ { "nb", required_argument, 0, 0x101 },
+ { "nf", required_argument, 0, 0x102 },
+ { "sb", required_argument, 0, 0x103 },
+ { "sf", required_argument, 0, 0x104 },
+ { 0, 0, 0, 0 }
+ };
+
+ for (;;) {
+ int opt = getopt_long(*argc, *argv, "hviw:I:p:Ibf:m", opts, NULL);
+ if (opt < 0)
+ break;
+
+ switch (opt) {
+ case 'h':
+ usage(stdout, *argv[0]);
+ break;
+ case 'v':
+ printVersion(*argv[0]);
+ exit(EXIT_SUCCESS);
+
+ case 'i':
+ client.filterMode = BM_FILTER_MODE_DMENU_CASE_INSENSITIVE;
+ break;
+ case 'w':
+ client.wrap = 1;
+ break;
+ case 'l':
+ client.lines = strtol(optarg, NULL, 10);
+ break;
+ case 'p':
+ client.title = optarg;
+ break;
+ case 'I':
+ client.selected = strtol(optarg, NULL, 10);
+ break;
+
+ case 'b':
+ client.bottom = 1;
+ break;
+ case 'f':
+ client.grab = 1;
+ break;
+ case 'm':
+ client.monitor = strtol(optarg, NULL, 10);
+ break;
+
+ case 0x100:
+ case 0x101:
+ case 0x102:
+ case 0x103:
+ case 0x104:
+ break;
+ }
+ }
+
+ *argc -= optind;
+ *argv += optind;
+}
static void readItemsToMenuFromStdin(bmMenu *menu)
{
@@ -44,15 +178,19 @@ static void readItemsToMenuFromStdin(bmMenu *menu)
int main(int argc, char **argv)
{
- (void)argc, (void)argv;
+ parseArgs(&argc, &argv);
bmMenu *menu = bmMenuNew(BM_DRAW_MODE_CURSES);
if (!menu)
return EXIT_FAILURE;
- bmMenuSetTitle(menu, "bemenu");
+ bmMenuSetTitle(menu, client.title);
+ bmMenuSetFilterMode(menu, client.filterMode);
+
readItemsToMenuFromStdin(menu);
+ bmMenuSetHighlightedIndex(menu, client.selected);
+
bmKey key;
unsigned int unicode;
int status = 0;
diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt
index f53dfc6..bfdb843 100644
--- a/lib/CMakeLists.txt
+++ b/lib/CMakeLists.txt
@@ -2,14 +2,17 @@
SET(BEMENU_SOURCE
menu.c
item.c
- filter.c
list.c
util.c
+ filter.c
+ library.c
draw/curses.c
)
SET(BEMENU_INCLUDE)
SET(BEMENU_LIBRARIES)
+CONFIGURE_FILE(version.h.in version.h @ONLY)
+
# Warnings
IF (CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wno-variadic-macros -Wno-long-long")
@@ -24,7 +27,7 @@ IF (UNIX AND CMAKE_COMPILER_IS_GNUCC)
ENDIF ()
# Compile
-INCLUDE_DIRECTORIES(${BEMENU_INCLUDE})
+INCLUDE_DIRECTORIES(${BEMENU_INCLUDE} ${CMAKE_CURRENT_BINARY_DIR})
ADD_LIBRARY(bemenu ${BEMENU_SOURCE})
TARGET_LINK_LIBRARIES(bemenu ${BEMENU_LIBRARIES} dl)
diff --git a/lib/bemenu.h b/lib/bemenu.h
index 37c7d1e..ebafffa 100644
--- a/lib/bemenu.h
+++ b/lib/bemenu.h
@@ -22,6 +22,27 @@ typedef struct _bmItem bmItem;
*/
/**
+ * @addtogroub Library
+ * @{ */
+
+/**
+ * @name Library Version
+ * @{ */
+
+/**
+ * Get version of the library in 'major.minor.patch' format.
+ *
+ * @see @link http://semver.org/ Semantic Versioning @endlink
+ *
+ * @return Null terminated C "string" to version string.
+ */
+const char* bmVersion(void);
+
+/** @} Library Version */
+
+/** @} Library */
+
+/**
* @addtogroup Menu
* @{ */
diff --git a/lib/library.c b/lib/library.c
new file mode 100644
index 0000000..69e898b
--- /dev/null
+++ b/lib/library.c
@@ -0,0 +1,8 @@
+#include "version.h"
+
+const char *bmVersion(void)
+{
+ return BM_VERSION;
+}
+
+/* vim: set ts=8 sw=4 tw=0 :*/
diff --git a/lib/version.h.in b/lib/version.h.in
new file mode 100644
index 0000000..6be8b5f
--- /dev/null
+++ b/lib/version.h.in
@@ -0,0 +1,3 @@
+static const char *BM_VERSION = "@BEMENU_VERSION@";
+
+/* vim: set ts=8 sw=4 tw=0 :*/