p-gen/smenu

Error: Insufficient memory (attempt to calloc 2305843009213693952 bytes)

mjf opened this issue · 8 comments

mjf commented

I do not consider this serious bug but look: this is weird!

$ smenu -V
Version: 1.0.0
$ echo '[A]' '[B]' '[C]' |smenu -ES '/([^[]]+)/\2/'
Error: Insufficient memory (attempt to calloc 2305843009213693952 bytes)

The issue is caused by the typo \2 (as well as \3, \4, etc.) in the subtitution part.

Even much weirder issue occures if you try \0 (of course a non-sense, but \0 we get a menu but what a menu it is)!

$ smenu -ES '/([^[]]+)/\0/' <<< '[1] [2] [3]'
[0 [0 [0 

😄

(So a small typo and misuse of a -ES parameter caused by my initial misread of the manual page smenu(1) revealed such weird things in the end.)

p-gen commented

Hi, thanks for the report. I'll see how to fix that.

p-gen commented

BTW, what was the expected result?

mjf commented

@p-gen I would like to achieve this:

to have input like

[FOO] [BAR] [BAZ]

but display them like

 FOO  BAR  BAZ 

(the [] substituted with a space) and to display selected items eg. like

 FOO [BAR] BAZ 

(the "BAR" is under the cursor, selected)... When entered, I will obtain the "BAR" (if that's not possible, I can live with obtaining the original input "[BAR]" and post-process...) Also, the logic can be inverted, if I could somehow mangle clean input (without the extra brackets) and just add the spaces around...

The simplest case is to add single space around each word/item because I use inverse background for selector/cursor and with the additional space around it would be much readable and look better (I hope).

p-gen commented

The fix and some improvements are almost ready, but you should be able to get what you want using the following syntax:

echo '[FOO] [BAR] [BAZ]' | smenu -S '/[][]+//g' (note the inversion of the parentheses in the expression to conform to the regex standard).

Your approach is also wrong, because it will replace the word without parentheses with the same word without parentheses (\1) without removing the parentheses. This is an identity transformation.

Example with sed : echo '[A]' | sed -E 's/([^][]+)/x/' and echo '[A]' | sed -E 's/([^][]+)/\1/'

If you want the selected word to be the original word, you must add a "v" at the end and use -S, as in :

A=$(echo '[FOO] [BAR] [BAZ]' | smenu -S '/[][]+//gv')
echo "$A"
[BAR]

(if BAR has been selected).

p-gen commented

I pushed the commit 637fc27 which should fix the reported issues.

mjf commented

@p-gen Hello.

echo '[FOO] [BAR] [BAZ]' | smenu -S '/[][]+/ /g'

This does not produce the desired

 FOO [BAR] BAZ 

meaning that [BAR] is selected (pointed to) and shown with the [] included for the selected item (as seen above)... That's my fault to explain it better for the first time...

There are these options to the smenu(1):

         [-S|-subst... /regex/repl/opts]
         [-I|-si|-subst_included... /regex/repl/opts]
         [-E|-se|-subst_excluded... /regex/repl/opts]
         [-ES|-early_subst... /regex/repl/opts]

I guess (not sure) that what I need is some more substitution options for selected and unselected items separated, such as some

         [-SU|-subst-unselected... /regex/repl/opts]
         [-SS|-subst-selected... /regex/repl/opts]

With that the above example command would read:

echo '[FOO] [BAR] [BAZ]' | smenu -SU '/[][]+/ /g'

Or are we able to workaround with the current set of options somehow? And, if not, would you accept this as a feature request (I guess it could be quite easy to implement)? Thanks.

p-gen commented

Hi,

substitutions are made before elements are displayed for alignment and performance reasons.

If I understand correctly, you want the word under the cursor (or marked/pinned using the -T/-P option) to be dynamically modified using a regular expression. Is this correct?

mjf commented

@p-gen Hello, sorry for the delay...

substitutions are made before elements are displayed for alignment and performance reasons.

If I understand correctly, you want the word under the cursor (or marked/pinned using the -T/-P option) to be dynamically modified using a regular expression. Is this correct?

Yes, that's what the new options, if implemented, would basically do

         [-SU|-subst-unselected... /regex/repl/opts]
         [-SS|-subst-selected... /regex/repl/opts]

... just modify what you see on screen for selected and unselected items. These are not supposed to modify anything else (ie. output value). You're right it will influence the alignment algorithm (I did not realized that because I do not use smenu(1) with alignment but rather in "inline" mode).