p-gen/smenu

A column context but limiting how many columns are produced

Earnestly opened this issue · 4 comments

I have inputs which display livestreams, the view count and their title; e.g.

foobar 1234 title of the stream
baz 1337 another title for another stream

When using -c -C1 this more or less does what I want, making only the first column selectable and producing alignment over the view count. The problem is that the rest of the message is also aligned, which I'd like to avoid, e.g.

foobar | 1234 | title   | of    | the | stream
baz    | 1337 | another | title | for | another stream

Instead I would like something that behaves as follows:

foobar | 1234 | title of the stream
baz    | 1337 | another title for another stream

(Using | as a visual indicator of the columns, not much unlike -g '|| ')

I'm not sure if what I'm after can be done with existing features, I tried my best to look through the manual but didn't find anything particularly obvious.

p-gen commented

Hi, this is not possible at this time. This feature could be added in a future version.
However, as smenu was designed as a filter you can easily apply the unix philosophy by combining it with other commands as in the example below:

cat your_data_file \
| while read -r L ;do
    read A B C <<<$L
    echo $A $B \"$C\"
  done \
| smenu -c -C1 -g '|'
p-gen commented

By the way, -g displays a vertical bar separator by default, so its argument is unnecessary here.

I didn't realise that double quoted regions would be considered as a single word in this context, despite having read about -Q.

I'll see about escaping double quotes in the input and wrapping the title in double quotes.

Edit: A decent solution:

< livestreams awk 'NF {
    # Reconstruct the field separators according to OFS - my input is already
    # table aligned so this will remove the extraneous spaces.
    $1 = $1

    # Escape any escapes and double quotes.
    gsub(/\\/, "\\\\")
    gsub(/\"/, "\\\"")

    # Ranged prints in awk are a sore point, substr is fair approach.
    printf "%s %d %s \"%s\"\n", $1, $2, $3, substr($0, length($1 $2 $3) + 4)
}' | smenu -qcC1

Even if smenu implemented flags for this I still couldn't use it generally as inputs might have different column arrangements.

In general my input to these sorts of menus (dmenu, bemenu, etc.) is already table aligned and all I need is the row, but smenu breaks that alignment, which is understandable given what smenu is trying to do.

Feel free to close this issue if you wish, either approach works well enough from my point of view.

Thanks for the example of quoting, I should have thought of that.

PS. Hopefully only double quotes and not single quotes are allowed to do this.

p-gen commented

Adding such functionality may be done later but with low priority as your problem can be easily solved by preprocessing the standard input before passing it to smenu.

Thank you for considering my small utility to solve your problem.