add ability to print item number rather than the item itself

This commit is contained in:
speedie 2023-05-06 23:29:41 +02:00
parent c58542c2b9
commit 5ff1abc849
12 changed files with 58 additions and 3 deletions

View file

@ -54,6 +54,12 @@ You may use long, descriptive arguments or the shorter arguments.
`-nrw, --no-relative-width`
: Disable relative input width
`-ix, --print-index`
: Print index instead of actual text
`-nix, --no-print-index`
: Don't print index instead of actual text
`-f, --fast`
: Grabs keyboard before reading stdin

View file

@ -142,6 +142,7 @@ spmenu.dockproperty: 1
spmenu.fast: 0
spmenu.managed: 0
spmenu.mon: -1
spmenu.printindex: 0
spmenu.coloritems: 1
spmenu.sgr: 1

View file

@ -199,6 +199,10 @@ spmenu = {
password = 0; // Don't read standard input and replace all characters with the password character (0/1)
} );
/* Output options */
output = ( { printindex = 0; // Print index instead of actual text (0/1)
} );
/* Mode options */
mode = ( { default = 0; // Mode to start spmenu in (0: Normal mode, 1: Insert mode)
normal_text = "Normal"; // Normal mode text (text)

View file

@ -252,6 +252,14 @@ void backspace(Arg *arg) {
void selectitem(Arg *arg) {
char *selection;
// print index
if (printindex && sel && arg->i) {
fprintf(stdout, "%d\n", sel->index);
cleanup();
exit(0);
}
// selected item or input?
if (sel && arg->i && !hideitem) {
selection = sel->text;
} else {

View file

@ -94,6 +94,10 @@ void readargs(int argc, char *argv[]) {
fuzzy = 1;
} else if (!strcmp(argv[i], "-NF") || (!strcmp(argv[i], "--no-fuzzy"))) { // no fuzzy matching
fuzzy = 0;
} else if (!strcmp(argv[i], "-ix") || (!strcmp(argv[i], "--print-index"))) { // print index
printindex = 1;
} else if (!strcmp(argv[i], "-nix") || (!strcmp(argv[i], "--no-print-index"))) { // no print index
printindex = 0;
} else if (!strcmp(argv[i], "-s") || (!strcmp(argv[i], "--case-sensitive")) || (!strcmp(argv[i], "--sensitive"))) { // case-sensitive item matching
casesensitive = 1;
} else if (!strcmp(argv[i], "-ns") || (!strcmp(argv[i], "--case-insensitive")) || (!strcmp(argv[i], "--insensitive"))) { // case-insensitive item matching
@ -448,6 +452,8 @@ void usage(void) {
"spmenu -mc --max-cache <size> Set max image cache size to <size>\n"
"spmenu -rw, --relative-width Enable relative input width\n"
"spmenu -nrw, --no-relative-width Disable relative input width\n"
"spmenu -ix, --print-index Print index instead of actual text\n"
"spmenu -nix, --no-print-index Don't print index instead of actual text\n"
"spmenu -f, --fast Grabs keyboard before reading stdin\n"
"spmenu -F, --fuzzy Enable fuzzy matching\n"
"spmenu -NF, --no-fuzzy Disable fuzzy matching\n"

View file

@ -400,6 +400,17 @@ void conf_init(void) {
}
}
// load options spmenu.output
config_setting_t *output_setting = config_lookup(&cfg, "spmenu.output");
if (output_setting != NULL) {
for (unsigned int i = 0; i < config_setting_length(output_setting); ++i) {
config_setting_t *conf = config_setting_get_elem(output_setting, i);
// look up
config_setting_lookup_int(conf, "printindex", &printindex); // spmenu.output.printindex
}
}
// load options spmenu.mode
config_setting_t *mode_setting = config_lookup(&cfg, "spmenu.mode");
if (mode_setting != NULL) {

View file

@ -30,6 +30,8 @@ void readstdin(void) {
imax = i;
}
items[i].index = i;
// parse image markup
#if USEIMAGE
if(!strncmp("IMG:", items[i].text, strlen("IMG:"))) {

View file

@ -146,6 +146,7 @@ ResourcePref resources[] = {
{ "managed", INTEGER, &managed },
{ "mon", INTEGER, &mon },
{ "sortmatches", INTEGER, &sortmatches },
{ "printindex", INTEGER, &printindex },
{ "fuzzy", INTEGER, &fuzzy },
{ "pango_item", INTEGER, &pango_item },
{ "pango_prompt", INTEGER, &pango_prompt },

View file

@ -225,6 +225,7 @@ static int pango_mode = 0; /* Enable support for pango markup f
static int pango_password = 0; /* Enable support for pango markup for the password text */
/* Misc */
static int coloritems = 1; /* Color items or not */
static int sgr = 1; /* Support SGR sequences or not */
static char *worddelimiters = " "; /* Word delimiters used for keybinds that change words, " " is default. */
static int printindex = 0; /* Print index instead of the text itself (0/1) */
static int coloritems = 1; /* Color items (0/1) */
static int sgr = 1; /* Support SGR sequences (0/1) */
static char *worddelimiters = " "; /* Word delimiters used for keybinds that change words, Space is default. */

View file

@ -74,6 +74,12 @@ Enable relative input width
\f[V]-nrw, --no-relative-width\f[R]
Disable relative input width
.TP
\f[V]-ix, --print-index\f[R]
Print index instead of actual text
.TP
\f[V]-nix, --no-print-index\f[R]
Don\[cq]t print index instead of actual text
.TP
\f[V]-f, --fast\f[R]
Grabs keyboard before reading stdin
.TP

View file

@ -164,6 +164,7 @@ struct item {
char *ex;
struct item *left, *right;
int hp;
int index;
double distance;
};

View file

@ -223,6 +223,14 @@ Enable relative input width
<dd>
Disable relative input width
</dd>
<dt><code>-ix, --print-index</code></dt>
<dd>
Print index instead of actual text
</dd>
<dt><code>-nix, --no-print-index</code></dt>
<dd>
Dont print index instead of actual text
</dd>
<dt><code>-f, --fast</code></dt>
<dd>
Grabs keyboard before reading stdin