Skip to content
Myr

Rework --accept-nth and --with-nth to accept a format string

Before adoption: this commit came in with the history the project adopted, so no landing records it.

With this, --accept-nth and --with-nth can either be specified as a single integer (--with-nth=2), or as a template string (--with-nth='{1}'). The simplified case, --with-nth=1, is converted to '{1}' internally - expansion always assumes a template string. If the parameter is in any way invalid, it is copied verbatim instead of being expanded. This includes: * Non-closed '{' * '{x}', where x is not an integer * '{x}', where x is an integer, but larger than the number of available column Not planned: * fzf-styled: simplified variants: --with-nth=1,2, --with-nth=1..2 (you'll need to use a template string instead) Closes #598

authored by Daniel Eklöf · committed by Daniel Eklöf · parent 117dc1f · Browse files · 9 files+220 added−67 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

9 files+220 added−67 removed

  • other:+220 added−67 removed

9 files+220 added−67 removed

modified:doc/fuzzel.1.scd+10 added-2 removed
481 unmodified lines
482 Print selected entry's index instead of its text.483 Index values start with zero for the first entry.484 dmenu mode only.485486*--with-nth*=_N_486*--with-nth*=_N_|_FMT_487 Display the N:th column (tab separated) of each input line. When488 selected, the full input line is printed on stdout. dmenu mode489 only.490 491 The argument can also be a format string (i.e free-format text),492 where *{N}* expands to the N:th column. Ranges on the form493 *{N..M}* and *{N..}* (open-ended) are also supported.494 495 Example:496 ```497 printf "1\\tFirst\\n2\\tSecond" | fuzzel -d --with-nth=2498 ```
3 unmodified lines
502 on stdout.503 504 Setting it to *0* is the same as not specifying *--with-nth* at all.505502*--accept-nth*=_N_506*--accept-nth*=_N_|_FMT_507 Output the N:th column (tab separated) of each input line to stdout.508 dmenu mode only.509 510 The argument can also be a format string (i.e free-format text),511 where *{N}* expands to the N:th column. Ranges on the form512 *{N..M}* and *{N..}* (open-ended) are also supported.513 514 Example:515 ```516 printf "1\\tFirst\\n2\\tSecond" | fuzzel -d --accept-nth=2517 ```
101 unmodified lines
modified:column.c+159 added-24 removed
6 unmodified lines
7#include <uchar.h>8#include <unistd.h>910#define LOG_MODULE "column"11#define LOG_ENABLE_DBG 011#define LOG_ENABLE_DBG 112#include "log.h"13#include "debug.h"14#include "xmalloc.h"1516char32_t *16nth_column(const char32_t *string, unsigned int nth) {17 assert(nth > 0);17nth_column(const char32_t *string, const char *nth_format)18{19 const char32_t delim = U'\t';20 const char32_t *string_end = string + c32len(string);21 xassert(*string_end == U'\0');2219 const char32_t* column = string;23 /* Count number of columns */24 size_t col_count = 1;25 for (const char32_t *next = c32chr(string, delim); next != NULL; next = c32chr(next + 1, delim))26 col_count++;2721 for (unsigned int i = 0;22 i < nth - 1;23 i++)24 {25 column = c32chr(column, U'\t');28 /* Find the beginning, and length, of each column */29 const char32_t *col_starts[col_count];30 size_t col_lens[col_count];3127 if (column == NULL) {28 LOG_WARN("%ls: only %u column(s) available",29 (const wchar_t *)string, i + 1);30 column = U"";31 break;32 }32 col_starts[0] = string;33 for (size_t i = 1; i < col_count; i++) {34 const char32_t *next = c32chr(col_starts[i - 1] + 1, delim);3534 column++;36 xassert(next != NULL);37 col_starts[i] = next + 1;38 col_lens[i - 1] = next - col_starts[i - 1];39 }4041 col_lens[col_count - 1] = string_end - col_starts[col_count - 1];4243 for (size_t i = 0; i < col_count; i++) {44 LOG_DBG("col #%zu: \"%.*ls\"", i,45 (int)col_lens[i],46 (const wchar_t *)col_starts[i]);47 }4849 /* TODO: allocate dynamically */50 char32_t *result = NULL;38 const char32_t *next_tab = c32chr(column, U'\t');51 size_t res_chars = 0;52 size_t res_idx = 0;5340 if (next_tab == NULL)41 result = xc32dup(column);42 else {43 const size_t len = next_tab - column;44 result = xmalloc((len + 1) * sizeof(char32_t));45 memcpy(result, column, len * sizeof(char32_t));46 result[len] = U'\0';54 /* Expand the 'nth' template, replacing '{1}' with column[0] etc */55 const char *copy_start = nth_format;56 const char *p = nth_format;57 while (true) {58 const char *arg_start = strchr(p, U'{');59 if (arg_start == NULL) {60 const size_t copy_len = mbstoc32(NULL, copy_start, 0);61 if (copy_len == (size_t)-1) {62 result[res_idx] = U'\0';63 break;64 }6566 const size_t req_res_len = res_idx + copy_len;6768 if (req_res_len > res_chars) {69 res_chars = req_res_len + 1;70 result = xrealloc(result, res_chars * sizeof(result[0]));71 }7273 mbstoc32(&result[res_idx], copy_start, res_chars - res_idx);74 res_idx += copy_len;7576 xassert(res_idx <= res_chars);7778 result[res_idx] = U'\0';79 break;80 }8182 const char *arg_end = strchr(arg_start, U'}');83 if (arg_end == NULL) {84 /* Unclosed '{' */85 p++;86 continue;87 }8889 unsigned int idx_start = 0;90 unsigned int idx_end = 0;91 int dots = 0;9293 for (const char *d = arg_start + 1; d != arg_end; d++) {94 if (*d == '.') {95 if (idx_start == 0 || dots >= 2) {96 idx_start = 0;97 break;98 }99100 dots++;101 xassert(dots >= 1 && dots <= 3);102 } else if (*d >= '0' && *d <= '9') {103 if (dots == 0) {104 idx_start *= 10;105 idx_start += *d - '0';106 } else if (dots == 2) {107 idx_end *= 10;108 idx_end += *d - '0';109 } else {110 idx_start = 0;111 break;112 }113 } else {114 /* Invalid index */115 idx_start = 0;116 break;117 }118 }119120 if (idx_end == 0) {121 if (dots == 0)122 idx_end = idx_start;123 else if (dots == 2) {124 /* Open ended - append all remaining columns */125 idx_end = col_count;126 } else127 idx_start = 0;128 }129130 xassert(idx_end >= idx_start);131132 if (idx_start == 0 || idx_start > col_count || idx_end > col_count) {133 /* Invalid index */134 p++;135 continue;136 }137138 /* Non-template prefix length */139 const size_t copy_len = arg_start - copy_start;140 const size_t copy_len_wide_chars = mbsntoc32(NULL, copy_start, copy_len, 0);141142 if (copy_len_wide_chars == (size_t)-1) {143 result[res_idx] = U'\0';144 break;145 }146147 /* Column(s) length */148 size_t total_col_len = 0;149 for (unsigned int i = idx_start; i <= idx_end; i++)150 total_col_len += col_lens[i - 1];151152 if (idx_end > idx_start) {153 /* Space-separated columns */154 total_col_len += idx_end - idx_start;155 }156157 const size_t req_res_len =158 res_idx + copy_len_wide_chars + total_col_len + 1;159160 if (req_res_len > res_chars) {161 res_chars = req_res_len * 2;162 result = xrealloc(result, res_chars * sizeof(result[0]));163 }164165 mbsntoc32(&result[res_idx], copy_start, copy_len, res_chars - res_idx);166 res_idx += copy_len_wide_chars;167 xassert(res_idx <= res_chars);168169 for (unsigned int i = idx_start; i <= idx_end; i++) {170 c32memcpy(&result[res_idx], col_starts[i - 1], col_lens[i - 1]);171 res_idx += col_lens[i - 1];172 xassert(res_idx <= res_chars);173174 if (i != idx_end) {175 result[res_idx++] = U' ';176 xassert(res_idx <= res_chars);177 }178 }179180 result[res_idx] = U'\0';181 copy_start = p = arg_end + 1;182 }183184 return result;185}
modified:column.h+1 added-1 removed
1#pragma once23#include <uchar.h>4char32_t *nth_column(const char32_t *column, unsigned int nth);4char32_t *nth_column(const char32_t *string, const char *nth_format);
modified:config.c+4 added-2 removed
1659 unmodified lines
1660 .enabled = false,1661 .mode = DMENU_MODE_TEXT,1662 .exit_immediately_if_empty = false,1663 .delim = '\n',1664 .render_column = 0,1665 .output_column = 0,1664 .with_nth_format = NULL,1665 .accept_nth_format = NULL,1666 },1667 .anchor = ANCHOR_CENTER,1668 .margin = {1669 .x = 0,
108 unmodified lines
1778 free(conf->launch_prefix);1779 free(conf->font);1780 free(conf->icon_theme);1781 free(conf->cache_path);1782 free(conf->dmenu.with_nth_format);1783 free(conf->dmenu.accept_nth_format);1784 free_key_binding_list(&conf->key_bindings);1785}17861787struct rgba
9 unmodified lines
modified:config.h+2 added-2 removed
164 unmodified lines
165 bool enabled;166 enum dmenu_mode mode;167 bool exit_immediately_if_empty;168 char delim;169 unsigned int render_column;170 unsigned int output_column;169 char *with_nth_format;170 char *accept_nth_format;171 } dmenu;172173 enum anchors anchor;174
63 unmodified lines
modified:dmenu.c+8 added-6 removed
23 unmodified lines
24#include "xmalloc.h"2526void27dmenu_load_entries(struct application_list *applications, char delim,28 const char *with_nth_format,28 int with_nth, int event_fd, int abort_fd)29 int event_fd, int abort_fd)30{31 tll(struct application *) entries = tll_init();3233 size_t size = 0;
133 unmodified lines
167 free(icon_name);168 continue;169 }170170 char32_t *title = with_nth == 0171 char32_t *title = with_nth_format == 0172 ? xc32dup(wline)172 : nth_column(wline, with_nth);173 : nth_column(wline, with_nth_format);174175 char32_t *lowercase = xc32dup(title);176 for (size_t i = 0; i < c32len(lowercase); i++)177 lowercase[i] = toc32lower(lowercase[i]);
61 unmodified lines
239}240241bool242dmenu_execute(const struct application *app, ssize_t index,242 const struct prompt *prompt, enum dmenu_mode format, unsigned int column)243 const struct prompt *prompt, enum dmenu_mode format,244 const char *columns)245{246 switch (format) {247 case DMENU_MODE_TEXT: {248 const char32_t *output = app != NULL249 ? app->dmenu_input250 : prompt_text(prompt);251252 char32_t *column_output = NULL;251 if (column > 0) {252 column_output = nth_column(output, column);253 if (columns != NULL) {254 column_output = nth_column(output, columns);255 output = column_output;256 }257258 char *text = ac32tombs(output);
64 unmodified lines
modified:dmenu.h+10 added-6 removed
6 unmodified lines
7#include "icon.h"8#include "prompt.h"910void dmenu_load_entries(11 struct application_list *applications, char delim, int with_nth,12 int event_fd, int abort_fd);13bool dmenu_execute(const struct application *app, ssize_t index,14 const struct prompt *prompt, enum dmenu_mode format, unsigned int column);15void dmenu_try_icon_list(struct application_list *applications,16 icon_theme_list_t themes, int icon_size);11 struct application_list *applications, char delim,12 const char *with_nth_format, int event_fd, int abort_fd);1314bool dmenu_execute(15 const struct application *app, ssize_t index,16 const struct prompt *prompt, enum dmenu_mode format, const char *columns);1718void dmenu_try_icon_list(19 struct application_list *applications,20 icon_theme_list_t themes, int icon_size);
modified:main.c+25 added-23 removed
583 unmodified lines
584 }585586 if (dmenu_enabled) {587 if (!conf->prompt_only) {588 dmenu_load_entries(apps, dmenu_delim, conf->dmenu.render_column,588 dmenu_load_entries(apps, dmenu_delim, conf->dmenu.with_nth_format,589 ctx->event_fd, ctx->dmenu_abort_fd);590 read_cache(cache_path, apps, true);591 }592 } else {
1119 unmodified lines
1712 cmdline_overrides.conf.dmenu.mode = DMENU_MODE_INDEX;1713 cmdline_overrides.dmenu_mode_set = true;1714 break;17151716 case OPT_DMENU_WITH_NTH:1717 if (sscanf(optarg, "%u", &cmdline_overrides.conf.dmenu.render_column) != 1) {1718 fprintf(1719 stderr,1720 "%s: invalid with-nth value (must be an integer)\n",1721 optarg);1722 return EXIT_FAILURE;1723 }1716 case OPT_DMENU_WITH_NTH: {1717 unsigned int with_nth_idx;1718 if (sscanf(optarg, "%u", &with_nth_idx) == 1)1719 cmdline_overrides.conf.dmenu.with_nth_format = xasprintf("{%u}", with_nth_idx);1720 else1721 cmdline_overrides.conf.dmenu.with_nth_format = xstrdup(optarg);1722 cmdline_overrides.dmenu_with_nth_set = true;1723 break;1724 }17251727 case OPT_DMENU_ACCEPT_NTH:1728 if (sscanf(optarg, "%u", &cmdline_overrides.conf.dmenu.output_column) != 1) {1729 fprintf(1730 stderr,1731 "%s: invalid accept-nth value (must be an integer)\n",1732 optarg);1733 return EXIT_FAILURE;1734 }1735 cmdline_overrides.dmenu_accept_nth_set = true;1736 break;1726 case OPT_DMENU_ACCEPT_NTH: {1727 unsigned int accept_nth_idx;1728 if (sscanf(optarg, "%u", &accept_nth_idx) == 1)1729 cmdline_overrides.conf.dmenu.accept_nth_format = xasprintf("{%u}", accept_nth_idx);1730 else1731 cmdline_overrides.conf.dmenu.accept_nth_format = xstrdup(optarg);1732 cmdline_overrides.dmenu_accept_nth_set = true;1733 break;1734 }17351736 case OPT_LOG_LEVEL: {1737 int lvl = log_level_from_string(optarg);1738 if (lvl < 0) {
303 unmodified lines
2042 if (cmdline_overrides.dmenu_mode_set)2043 conf.dmenu.mode = cmdline_overrides.conf.dmenu.mode;2044 if (cmdline_overrides.dmenu_exit_immediately_if_empty_set)2045 conf.dmenu.exit_immediately_if_empty = cmdline_overrides.conf.dmenu.exit_immediately_if_empty;2048 if (cmdline_overrides.dmenu_with_nth_set)2049 conf.dmenu.render_column = cmdline_overrides.conf.dmenu.render_column;2050 if (cmdline_overrides.dmenu_accept_nth_set)2051 conf.dmenu.output_column = cmdline_overrides.conf.dmenu.output_column;2046 if (cmdline_overrides.dmenu_with_nth_set) {2047 free(conf.dmenu.with_nth_format);2048 conf.dmenu.with_nth_format = cmdline_overrides.conf.dmenu.with_nth_format;2049 }2050 if (cmdline_overrides.dmenu_accept_nth_set) {2051 free(conf.dmenu.accept_nth_format);2052 conf.dmenu.accept_nth_format = cmdline_overrides.conf.dmenu.accept_nth_format;2053 }2054 if (cmdline_overrides.conf.list_executables_in_path)2055 conf.list_executables_in_path = cmdline_overrides.conf.list_executables_in_path;2056 if (cmdline_overrides.render_workers_set)2057 conf.render_worker_count = cmdline_overrides.conf.render_worker_count;
239 unmodified lines
modified:wayland.c+1 added-1 removed
632 unmodified lines
633 struct application *app = match != NULL ? match->application : NULL;634 ssize_t index = app != NULL ? app->index : -1;635636 if (wayl->conf->dmenu.enabled) {637 dmenu_execute(app, index, wayl->prompt, wayl->conf->dmenu.mode, wayl->conf->dmenu.output_column);637 dmenu_execute(app, index, wayl->prompt, wayl->conf->dmenu.mode, wayl->conf->dmenu.accept_nth_format);638 wayl->exit_code = custom_success_exit_code >= 0639 ? custom_success_exit_code640 : EXIT_SUCCESS;641 wayl->status = EXIT_UPDATE_CACHE;
2425 unmodified lines