summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJari Vetoniemi <mailroxas@gmail.com>2014-10-26 15:42:09 +0200
committerJari Vetoniemi <mailroxas@gmail.com>2014-10-26 15:42:09 +0200
commita44b001e8260883c0876fba967b1d72a5b7ab7c3 (patch)
treee7a2dd45c9ee5de7adfe72e2bbe2b4f28a0cb1b8
parentfb6a49e69011f911b670ac3bdb08fe7868db3070 (diff)
downloadbemenu-a44b001e8260883c0876fba967b1d72a5b7ab7c3.tar.gz
bemenu-a44b001e8260883c0876fba967b1d72a5b7ab7c3.tar.bz2
bemenu-a44b001e8260883c0876fba967b1d72a5b7ab7c3.zip
Mkae highlighted item prefix configurable.
-rw-r--r--client/bemenu-run.c1
-rw-r--r--client/bemenu.c1
-rw-r--r--client/common/common.c8
-rw-r--r--client/common/common.h1
-rw-r--r--lib/bemenu.h19
-rw-r--r--lib/internal.h6
-rw-r--r--lib/menu.c17
-rw-r--r--lib/renderers/cairo.h13
-rw-r--r--lib/renderers/curses/curses.c10
9 files changed, 70 insertions, 6 deletions
diff --git a/client/bemenu-run.c b/client/bemenu-run.c
index 4ac1175..9ef7b66 100644
--- a/client/bemenu-run.c
+++ b/client/bemenu-run.c
@@ -15,6 +15,7 @@ static struct client client = {
.lines = 0,
.colors = {0},
.title = "bemenu",
+ .prefix = NULL,
.renderer = NULL,
.font = NULL,
.font_size = 0,
diff --git a/client/bemenu.c b/client/bemenu.c
index 96ad72e..b344443 100644
--- a/client/bemenu.c
+++ b/client/bemenu.c
@@ -11,6 +11,7 @@ static struct client client = {
.lines = 0,
.colors = {0},
.title = "bemenu",
+ .prefix = NULL,
.renderer = NULL,
.font = NULL,
.font_size = 0,
diff --git a/client/common/common.c b/client/common/common.c
index e34a34f..8677d1f 100644
--- a/client/common/common.c
+++ b/client/common/common.c
@@ -67,6 +67,7 @@ usage(FILE *out, const char *name)
" -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"
+ " -P, --prefix text to shown before highlighted item.\n"
" -I, --index select item at index automatically.\n"
" --backend options: curses, wayland\n"
" --prioritory options: terminal, gui\n\n"
@@ -108,6 +109,7 @@ parse_args(struct client *client, int *argc, char **argv[])
{ "list", required_argument, 0, 'l' },
{ "prompt", required_argument, 0, 'p' },
{ "index", required_argument, 0, 'I' },
+ { "prefix", required_argument, 0, 'P' },
{ "backend", required_argument, 0, 0x100 },
{ "prioritory", required_argument, 0, 0x101 },
@@ -136,7 +138,7 @@ parse_args(struct client *client, int *argc, char **argv[])
* or parse them before running getopt.. */
for (;;) {
- int32_t opt = getopt_long(*argc, *argv, "hviwl:I:p:I:bfm:", opts, NULL);
+ int32_t opt = getopt_long(*argc, *argv, "hviwl:I:p:P:I:bfm:", opts, NULL);
if (opt < 0)
break;
@@ -160,6 +162,9 @@ parse_args(struct client *client, int *argc, char **argv[])
case 'p':
client->title = optarg;
break;
+ case 'P':
+ client->prefix = optarg;
+ break;
case 'I':
client->selected = strtol(optarg, NULL, 10);
break;
@@ -248,6 +253,7 @@ menu_with_options(struct client *client)
bm_menu_set_font(menu, client->font, client->font_size);
bm_menu_set_title(menu, client->title);
+ bm_menu_set_prefix(menu, client->prefix);
bm_menu_set_filter_mode(menu, client->filter_mode);
bm_menu_set_lines(menu, client->lines);
bm_menu_set_wrap(menu, client->wrap);
diff --git a/client/common/common.h b/client/common/common.h
index b87b974..e710236 100644
--- a/client/common/common.h
+++ b/client/common/common.h
@@ -10,6 +10,7 @@ struct client {
uint32_t lines;
const char *colors[BM_COLOR_LAST];
const char *title;
+ const char *prefix;
const char *renderer;
char *font;
uint32_t font_size;
diff --git a/lib/bemenu.h b/lib/bemenu.h
index c52135d..b4a6423 100644
--- a/lib/bemenu.h
+++ b/lib/bemenu.h
@@ -253,10 +253,27 @@ void bm_menu_set_userdata(struct bm_menu *menu, void *userdata);
void* bm_menu_get_userdata(struct bm_menu *menu);
/**
+ * Set highlight prefix.
+ * This is shown on vertical list mode only.
+ *
+ * @param menu bm_menu instance where to set highlight prefix.
+ * @param prefix Null terminated C "string" to act as prefix for highlighted item. May be set **NULL** for none.
+ */
+void bm_menu_set_prefix(struct bm_menu *menu, const char *prefix);
+
+/**
+ * Get highlight prefix.
+ *
+ * @param menu bm_menu instance where to get highlight prefix.
+ * @param Const pointer to current highlight prefix, may be **NULL** if empty.
+ */
+const char* bm_menu_get_prefix(struct bm_menu *menu);
+
+/**
* Set filter text to bm_menu instance.
*
* @param menu bm_menu instance where to set filter.
- * @param filter Null terminated C "string" to act as filter.
+ * @param filter Null terminated C "string" to act as filter. May be set **NULL** for none.
*/
void bm_menu_set_filter(struct bm_menu *menu, const char *filter);
diff --git a/lib/internal.h b/lib/internal.h
index b9b4a5e..e22c546 100644
--- a/lib/internal.h
+++ b/lib/internal.h
@@ -211,6 +211,12 @@ struct bm_menu {
struct bm_hex_color colors[BM_COLOR_LAST];
/**
+ * Prefix shown for highlighted item.
+ * Vertical mode only.
+ */
+ char *prefix;
+
+ /**
* Text used to filter matches.
*/
char *filter;
diff --git a/lib/menu.c b/lib/menu.c
index fe13276..bab915a 100644
--- a/lib/menu.c
+++ b/lib/menu.c
@@ -136,12 +136,27 @@ bm_menu_get_userdata(struct bm_menu *menu)
}
void
+bm_menu_set_prefix(struct bm_menu *menu, const char *prefix)
+{
+ assert(menu);
+ free(menu->prefix);
+ menu->prefix = (prefix && strlen(prefix) > 0 ? bm_strdup(prefix) : NULL);
+}
+
+const char*
+bm_menu_get_prefix(struct bm_menu *menu)
+{
+ assert(menu);
+ return menu->prefix;
+}
+
+void
bm_menu_set_filter(struct bm_menu *menu, const char *filter)
{
assert(menu);
free(menu->filter);
- menu->filter = (filter ? bm_strdup(filter) : NULL);
+ menu->filter = (filter && strlen(filter) > 0 ? bm_strdup(filter) : NULL);
menu->filter_size = (filter ? strlen(filter) : 0);
}
diff --git a/lib/renderers/cairo.h b/lib/renderers/cairo.h
index 3132c09..32f8131 100644
--- a/lib/renderers/cairo.h
+++ b/lib/renderers/cairo.h
@@ -129,6 +129,12 @@ bm_cairo_paint(struct cairo *cairo, uint32_t width, uint32_t height, const struc
uint32_t displayed = 1;
uint32_t lines = MAX(height / paint.fe.height, menu->lines);
if (lines > 1) {
+ uint32_t start_x = 0;
+ if (menu->prefix) {
+ bm_cairo_get_text_extents(cairo, &result, "%s ", menu->prefix);
+ start_x = result.te.x_advance;
+ }
+
uint32_t count, cl = 1;
struct bm_item **items = bm_menu_get_filtered_items(menu, &count);
for (uint32_t i = (menu->index / (lines - 1)) * (lines - 1); i < count && cl < lines; ++i) {
@@ -145,8 +151,11 @@ bm_cairo_paint(struct cairo *cairo, uint32_t width, uint32_t height, const struc
bm_cairo_color_from_menu_color(menu, BM_COLOR_ITEM_BG, &paint.bg);
}
- bm_cairo_draw_line(cairo, &paint, &result, 0, width, 0, 4 + paint.fe.height * cl++,
- "%s%s", (highlighted ? ">> " : " "), (items[i]->text ? items[i]->text : ""));
+ if (menu->prefix && highlighted) {
+ bm_cairo_draw_line(cairo, &paint, &result, 0, width, 0, 4 + paint.fe.height * cl++, "%s %s", menu->prefix, (items[i]->text ? items[i]->text : ""));
+ } else {
+ bm_cairo_draw_line(cairo, &paint, &result, start_x, width, start_x, 4 + paint.fe.height * cl++, "%s", (items[i]->text ? items[i]->text : ""));
+ }
++displayed;
}
diff --git a/lib/renderers/curses/curses.c b/lib/renderers/curses/curses.c
index 11dfa2d..7923c93 100644
--- a/lib/renderers/curses/curses.c
+++ b/lib/renderers/curses/curses.c
@@ -194,7 +194,15 @@ render(const struct bm_menu *menu)
for (uint32_t i = (menu->index / (lines - 1)) * (lines - 1); i < count && cl < lines; ++i) {
bool highlighted = (items[i] == bm_menu_get_highlighted_item(menu));
int32_t color = (highlighted ? 2 : (bm_menu_item_is_selected(menu, items[i]) ? 1 : 0));
- draw_line(color, cl++, "%s%s", (highlighted ? ">> " : " "), (items[i]->text ? items[i]->text : ""));
+
+ if (menu->prefix && highlighted) {
+ draw_line(color, cl++, "%s %s", menu->prefix, (items[i]->text ? items[i]->text : ""));
+ } else if (menu->prefix) {
+ int32_t offset = (menu->prefix ? bm_utf8_string_screen_width(menu->prefix) : 0);
+ draw_line(color, cl++, "%*s %s", offset, "", (items[i]->text ? items[i]->text : ""));
+ } else {
+ draw_line(color, cl++, "%s", (items[i]->text ? items[i]->text : ""));
+ }
}
}