Squeeze String

Run Settings
LanguageLua
Language Version
Run Command
local config = { max_columns = 80; } local text = require "text" local function split(s, delimiter) delimiter = delimiter or "" local t = {} (s..delimiter):gsub("(.-)"..delimiter, function(c) table.insert(t, c) end) return t end print("Word count: "..#split(text, " ")) local lines = split(text, "\n") local new_lines = {} for _, line in ipairs (lines) do if line:len() <= config.max_columns then table.insert(new_lines, line) else local words = split(line, " ") local word_line = "" for _, word in ipairs(words) do if (word_line..word.." "):len() <= config.max_columns then word_line = word_line..word.." " else table.insert(new_lines, word_line) word_line = word.." " end end table.insert(new_lines, word_line) end end local charlimited_text = table.concat(new_lines, "\n") local final_text = charlimited_text:gsub("\n","\\n") print(final_text) print("------") print(charlimited_text)
return [[ When Narcissus died the pool of his pleasure changed from a cup of sweet waters into a cup of salt tears, and the Oreads came weeping through the woodland that they might sing to the pool and give it comfort. And when they saw that the pool had changed from a cup of sweet waters into a cup of salt tears, they loosened the green tresses of their hair and cried to the pool and said, `We do not wonder that you should mourn in this manner for Narcissus, so beautiful was he.' `But was Narcissus beautiful?' said the pool. `Who should know that better than you?' answered the Oreads. `Us did he ever pass by, but you he sought for, and would lie on your banks and look down at you, and in the mirror of your waters he would mirror his own beauty.' And the pool answered, `But I loved Narcissus because, as he lay on my banks and looked down at me, in the mirror of his eyes I saw ever my own beauty mirrored.' ]] -- https://americanliterature.com/author/oscar-wilde/poem/the-disciple
Editor Settings
Theme
Key bindings
Full width
Lines