r/lua • u/Cultural_Two_4964 • Jun 04 '26
Lua and yad (yet another dialogue) multiline form text.
If I have a string with a new line in it like "jim\njoe" I can split it into smaller strings, one for each line as follows:
textdata="jim\njoe"
for line in string.gmatch(textdata,"[^\n]+") do print(line) end
We get:
jim
joeNow, if I read multiline text from a yad form, I cannot split the string into lines.
options="yad --form --field=\"Multiline text.:TXT\" \"jim\njoe\""
yadform=io.popen(options)
for l in yadform:lines() do
for line in string.gmatch(l,"[^\n]+") do print(line) end
end
yadform:close()It gives me:
jim\njoe|The | is the yad separator, so that's normal, but the string gmatch thing is not splitting the text at the "\n".
I need to be able to split the string wherever there is a "\n" so any help much appreciated. Thank you.
7
Upvotes
7
u/Cootshk Jun 04 '26
Your script is escaping the \n into a newline in the match string. Try using "(.-)\\n" as your pattern instead
1
u/Cultural_Two_4964 Jun 05 '26 edited Jun 05 '26
Still a bit baffled because I can't get Fix 2 to work and whilst Fix 1 is almost OK with one of the search patterns, it splits the string wherever there is an "n", not just "\n". My regex is very poor, sorry. Anyway I made a home-brewed recursion to find "\n" and write the strings out which seems to work. I put my efforts here: https://pastecode.io/s/p8hsyw80
1
u/Cultural_Two_4964 Jun 06 '26
I have tried the lua pattern matching website: https://github.com/iamreiyn/lua-pattern-tester
and I can get a pattern that works well enough for me on the website but I can't get the multiline text from yad to obey the same rules in lua.
Is it something to do with encoding by yad. I guess the text goes from yad to the shell and then to lua, so what actually arrives is a bit unknown/system dependent, etc, although it looks OK if you print it to the screen. Of course, I may be doing something stupid. Anyway no worries.
1
u/NitinScripts Jun 04 '26
That’s happening because
yad --form --field="TXT"doesn’t return literal\nin the string. It returns an actual newline character, butio.popen():lines()already splits on newlines for you.So by the time you get
lfromfor l in yadform:lines(), eachlis already 1 line. Your innergmatch("[^\n]+")then sees no\nleft to split on.Why it works with "jim\njoe" but not with yad
"jim\njoe"= 1 string with byte0x0Ainside it.gmatch("[^\n]+")splits it.yadform:lines()reads until\n, returns"jim", then next loop gives"joe". The\nis gone.Fix 1: Don’t use lines(), read whole output then split
If you want the raw multiline text with
\ninside: yadform = io.popen(options) local raw = yadform:read("*all") -- read everything, keep \n yadform:close()for line in raw:gmatch("[\n]+") do print(line) end
*allgrabs the whole stdout including newlines, sogmatchworks like your first example.Fix 2: Just use lines() directly
Since
lines()already gives you line-by-line, skipgmatch: yadform = io.popen(options) for line in yadform:lines() do print(line) -- each line is already split end yadform:close() This is the cleanest way for yad forms.Extra tip for yad forms
yad --formoutputs fields separated by|, and multiline TXT fields use\ninside. If you ever need to split fields + lines: local raw = io.popen(options):read("*all") for field in raw:gmatch("([|]+)") do -- split by | for line in field:gmatch("[\n]+") do -- split field by \n print("Line:", line) end end So: useread("*all") + gmatchif you want the exact string with\n, or justlines()if you’re fine with line-by-line iteration.