Add `--accept-nth` command line option to select column to output
Before adoption: this commit came in with the history the project adopted, so no landing records it.
For example, my niri window picker oneliner from #470: ``` niri msg action focus-window --id $(niri msg -j windows | jq -r 'map("\(.title // .app_id)\t\(.id)\u0000icon\u001f\(.app_id)") | .[]' | fuzzel -d --with-nth 1 --accept-nth 2) ``` This was recently also added to fzf: https://github.com/junegunn/fzf/commit/2b58458
authored by bbb651 · committed by Daniel Eklöf · parent 4b2cc7a · Browse files · 14 files·+118 added−39 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
14 files+118 added−39 removed
- other:+118 added−39 removed
Provenance
- Commit
33c9b8e- Tree
e4c863e- Author
bbb651 <bar.ye651@gmail.com>- Committer
Daniel Eklöf <daniel@ekloef.se>- Parent
4b2cc7a
14 files changed+118 added−39 removed
modified:completions/fish/fuzzel.fish+1 added
69 unmodified lines70 complete -c fuzzel -s d -l dmenu -d "dmenu compatibility mode; entries are read from stdin, newline separated"71 complete -c fuzzel -l dmenu0 -d "dmenu compatibility mode; entries are read from stdin, NUL separated"72 complete -c fuzzel -l index -d "print selected entry's index instead of its text (dmenu mode only)"73 complete -c fuzzel -x -l with-nth -d "display the N:th column (tab separated) of each input line (dmenu only)"74+complete -c fuzzel -x -l accept-nth -d "output the N:th column (tab separated) of each input line (dmenu only)"75 complete -c fuzzel -s R -l no-run-if-empty -d "exit immediately without showing the UI if stdin is empty (dmenu mode only)"76 complete -c fuzzel -x -s d -l log-level -a "info warning error none" -d "log-level (warning)"77 complete -c fuzzel -x -s l -l log-colorize -a "always never auto" -d "enable or disable colorization of log output on stderr"78 complete -c fuzzel -s S -l log-no-syslog -d "disable syslog logging"2 unmodified linesmodified:completions/zsh/_fuzzel+1 added
61 unmodified lines62 '(-d --dmenu)'{-d,--dmenu}'[dmenu compatibility mode; entries are read from stdin, newline separated]' \63 '--dmenu0[dmenu compatibility mode; entries are read from stdin, NUL separated]' \64 '--index[print selected entry's index instead of its text (dmenu mode only)]' \65 '--with-nth[display the N:th column (tab separated) of each input line (dmenu mode only)]:with-nth:()' \66+ '--accept-nth[output the N:th column (tab separated) of each input line (dmenu mode only)]:with-nth:()' \67 '(-R --no-run-if-empty)'{-R,--no-run-if-empty}'[exit immediately without showing the UI if stdin is empty (dmenu mode only)]' \68 '--log-level[log level (warning)]:loglevel:(info warning error none)' \69 '--log-colorize[enable or disable colorization of log output on stderr]:logcolor:(never always auto)' \70 '--log-no-syslog[disable syslog logging]'20 unmodified linesmodified:doc/fuzzel.1.scd+17 added-4 removed
378 unmodified lines379 only.380 381 Example:382 ```383- printf "1\tFirst\n2\tSecond" | fuzzel --with-nth=2383+ printf "1\tFirst\n2\tSecond" | fuzzel -d --with-nth=2384 ```385 386 This will display the entries *First* and *Second*. When one of387- them has selected, the full input line (tabs included) are printed387+ them is selected, the full input line (tabs included) are printed388 on stdout.389 390- Setting it to *0* is the same as not specifying *--with-nth* at391- all.390+ Setting it to *0* is the same as not specifying *--with-nth* at all.391 392+*--accept-nth*=_N_393+ Output the N:th column (tab separated) of each input line to stdout.394+ dmenu mode only.395+ 396+ Example:397+ ```398+ printf "1\tFirst\n2\tSecond" | fuzzel -d --accept-nth=2399+ ```400+ 401+ This will display the entries *1 First* and *2 Second*. Depending402+ on which one is selected, *First* or *Second* will be printed to stdout.403+ 404+ Setting it to *0* is the same as not specifying *--accept-nth* at all.405+406 *-R*,*--no-run-if-empty*407 Exit immediately, without showing the UI, if stdin is408 empty. dmenu mode only.409 71 unmodified linesmodified:CHANGELOG.md+2 added
31 unmodified lines32 ### Added33 34 * Nanosvg updated to ea6a6aca009422bba0dbad4c80df6e6ba0c8218335 * `--with-nth=N` command line option ([#438][438]).36+* `--accept-nth=N` command line option ([#493][493]).37 38 [438]: https://codeberg.org/dnkl/fuzzel/issues/43839+[493]: https://codeberg.org/dnkl/fuzzel/pulls/49340 41 42 ### Changed43 873 unmodified linesadded:column.c+50 added
1+#include "column.h"2+3+#include <assert.h>4+#include <limits.h>5+#include <stdlib.h>6+#include <string.h>7+#include <uchar.h>8+#include <unistd.h>9+10+#define LOG_MODULE "column"11+#define LOG_ENABLE_DBG 012+#include "log.h"13+#include "xmalloc.h"14+15+char32_t *16+nth_column(const char32_t *string, unsigned int nth) {17+ assert(nth > 0);18+19+ const char32_t* column = string;20+21+ for (unsigned int i = 0;22+ i < nth - 1;23+ i++)24+ {25+ column = c32chr(column, U'\t');26+27+ 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+ }33+34+ column++;35+ }36+37+ char32_t *result = NULL;38+ const char32_t *next_tab = c32chr(column, U'\t');39+40+ 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';47+ }48+49+ return result;50+}added:column.h+4 added
1+#pragma once2+3+#include <uchar.h>4+char32_t *nth_column(const char32_t *column, unsigned int nth);modified:config.c+1 added
1609 unmodified lines1610 .mode = DMENU_MODE_TEXT,1611 .exit_immediately_if_empty = false,1612 .delim = '\n',1613 .render_column = 0,1614+ .output_column = 0,1615 },1616 .anchor = ANCHOR_CENTER,1617 .margin = {1618 .x = 0,114 unmodified linesmodified:config.h+1 added
149 unmodified lines150 enum dmenu_mode mode;151 bool exit_immediately_if_empty;152 char delim;153 unsigned int render_column;154+ unsigned int output_column;155 } dmenu;156 157 enum anchors anchor;158 54 unmodified linesmodified:dmenu.c+15 added-3 removed
1 #include "dmenu.h"2+#include "column.h"3 4 #include <stdlib.h>5 #include <stdbool.h>6 #include <string.h>1 unmodified line8 #include <errno.h>9 #include <sys/types.h>10 #include <fcntl.h>11 #include <poll.h>12+#include <uchar.h>13 #include <unistd.h>14 15 #include <tllist.h>16 209 unmodified lines226 }227 228 bool229 dmenu_execute(const struct application *app, ssize_t index,228- const struct prompt *prompt, enum dmenu_mode format)230+ const struct prompt *prompt, enum dmenu_mode format, unsigned int column)231 {232 switch (format) {233 case DMENU_MODE_TEXT: {234+ const char32_t *output = app != NULL ? app->title : prompt_text(prompt);235+ 236+ char32_t *column_output = NULL;237+ if (column > 0) {238+ column_output = nth_column(output, column);239+ output = column_output;240+ }241+232- char *text = ac32tombs(app != NULL ? app->title : prompt_text(prompt));233- if (text != NULL)242+ char *text = ac32tombs(output);243+ if (text != NULL) {244 printf("%s\n", text);245+ }246+ free(column_output);247 free(text);248 break;249 }250 7 unmodified linesmodified:dmenu.h+1 added-1 removed
7 unmodified lines8 9 void dmenu_load_entries(10 struct application_list *applications, char delim, int event_fd, int abort_fd);11 bool dmenu_execute(const struct application *app, ssize_t index,12- const struct prompt *prompt, enum dmenu_mode format);12+ const struct prompt *prompt, enum dmenu_mode format, unsigned int column);modified:main.c+19 added-1 removed
405 unmodified lines406 " --index print selected entry's index instead of of the \n"407 " entry's text (dmenu mode only)\n"408 " --with-nth=N display the N:th column (tab separated) of each\n"409 " input line (dmenu only)\n"410+ " --accept-nth=N output the N:th column (tab separated) of each\n"411+ " input line (dmenu only)\n"412 " -R,--no-run-if-empty exit immediately without showing UI if stdin\n"413 " is empty (dmenu mode only)\n"414 " --log-level={info|warning|error|none}\n"415 " log level (warning)\n"325 unmodified lines741 #define OPT_PLACEHOLDER_COLOR 293742 #define OPT_SEARCH_TEXT 294743 #define OPT_COUNTER 295744 #define OPT_HIDE_WHEN_PROMPT_EMPTY 296743- # define OPT_DMENU_WITH_NTH 297745+ #define OPT_DMENU_WITH_NTH 297746+ #define OPT_DMENU_ACCEPT_NTH 298747 748 static const struct option longopts[] = {749 {"config", required_argument, 0, OPT_CONFIG},750 {"check-config", no_argument, 0, OPT_CHECK_CONFIG},60 unmodified lines811 {"dmenu0", no_argument, 0, OPT_DMENU_NULL},812 {"no-run-if-empty", no_argument, 0, 'R'},813 {"index", no_argument, 0, OPT_DMENU_INDEX},814 {"with-nth", required_argument, 0, OPT_DMENU_WITH_NTH},815+ {"accept-nth", required_argument, 0, OPT_DMENU_ACCEPT_NTH},816 817 /* Misc */818 {"log-level", required_argument, 0, OPT_LOG_LEVEL},819 {"log-colorize", optional_argument, 0, OPT_LOG_COLORIZE},51 unmodified lines871 bool dmenu_mode_set:1;872 bool dmenu_exit_immediately_if_empty_set:1;873 bool dmenu_delim_set:1;874 bool dmenu_with_nth_set:1;875+ bool dmenu_accept_nth_set:1;876 bool layer_set:1;877 bool no_exit_on_keyboard_focus_loss_set:1;878 bool render_workers_set:1;879 bool match_workers_set:1;676 unmodified lines1556 }1557 cmdline_overrides.dmenu_with_nth_set = true;1558 break;1559 1560+ case OPT_DMENU_ACCEPT_NTH:1561+ if (sscanf(optarg, "%u", &cmdline_overrides.conf.dmenu.output_column) != 1) {1562+ fprintf(1563+ stderr,1564+ "%s: invalid accept-nth value (must be an integer)\n",1565+ optarg);1566+ return EXIT_FAILURE;1567+ }1568+ cmdline_overrides.dmenu_accept_nth_set = true;1569+ break;1570+1571 case OPT_LOG_LEVEL: {1572 int lvl = log_level_from_string(optarg);1573 if (lvl < 0) {1574 fprintf(242 unmodified lines1817 if (cmdline_overrides.dmenu_exit_immediately_if_empty_set)1818 conf.dmenu.exit_immediately_if_empty = cmdline_overrides.conf.dmenu.exit_immediately_if_empty;1819 if (cmdline_overrides.dmenu_with_nth_set)1820 conf.dmenu.render_column = cmdline_overrides.conf.dmenu.render_column;1821+ if (cmdline_overrides.dmenu_accept_nth_set)1822+ conf.dmenu.output_column = cmdline_overrides.conf.dmenu.output_column;1823 if (cmdline_overrides.conf.list_executables_in_path)1824 conf.list_executables_in_path = cmdline_overrides.conf.list_executables_in_path;1825 if (cmdline_overrides.render_workers_set)1826 conf.render_worker_count = cmdline_overrides.conf.render_worker_count;223 unmodified linesmodified:meson.build+1 added
153 unmodified lines154 'fuzzel',155 'application.c', 'application.h',156 'char32.c', 'char32.h',157 'clipboard.c', 'clipboard.h',158+ 'column.c', 'column.h',159 'config.c', 'config.h',160 'debug.c', 'debug.h',161 'dmenu.c', 'dmenu.h',162 'event.c', 'event.h',53 unmodified linesmodified:render.c+4 added-29 removed
6 unmodified lines7 #include <stdio.h>8 #include <stdlib.h>9 #include <string.h>10 #include <threads.h>11+#include <uchar.h>12 13+#include "column.h"14 #include "macros.h"15 #if HAS_INCLUDE(<pthread_np.h>)16 #include <pthread_np.h>17 #define pthread_setname_np(thread, name) (pthread_set_name_np(thread, name), 0)1048 unmodified lines1066 1067 if (render->conf->dmenu.enabled &&1068 render->conf->dmenu.render_column > 0)1069 {1068- const char32_t *column = match->application->render_title;1070+ char32_t *new_render_title =1071+ nth_column(match->application->render_title, render->conf->dmenu.render_column);1072 1070- for (unsigned int i = 0;1071- i < render->conf->dmenu.render_column - 1;1072- i++)1073- {1074- column = c32chr(column, U'\t');1075-1076- if (column == NULL) {1077- LOG_WARN("%ls: only %u column(s) available",1078- (const wchar_t *)match->application->title, i + 1);1079- column = U"";1080- break;1081- }1082-1083- column++;1084- }1085-1086- char32_t *new_render_title = NULL;1087- const char32_t *next_tab = c32chr(column, U'\t');1088-1089- if (next_tab == NULL)1090- new_render_title = xc32dup(column);1091- else {1092- const size_t len = next_tab - column;1093- new_render_title = xmalloc((len + 1) * sizeof(char32_t));1094- memcpy(new_render_title, column, len * sizeof(char32_t));1095- new_render_title[len] = U'\0';1096- }1097-1073 if (match->application->render_title != match->application->title)1074 free(match->application->render_title);1075 match->application->render_title = new_render_title;1076 }390 unmodified linesmodified:wayland.c+1 added-1 removed
534 unmodified lines535 struct application *app = match != NULL ? match->application : NULL;536 ssize_t index = app != NULL ? app->index : -1;537 538 if (wayl->conf->dmenu.enabled) {539- dmenu_execute(app, index, wayl->prompt, wayl->conf->dmenu.mode);539+ dmenu_execute(app, index, wayl->prompt, wayl->conf->dmenu.mode, wayl->conf->dmenu.output_column);540 wayl->exit_code = custom_success_exit_code >= 0541 ? custom_success_exit_code542 : EXIT_SUCCESS;543 wayl->status = EXIT_UPDATE_CACHE;1961 unmodified lines