Quick actions

cmd+k|ctrl+k

Navigation

Languages

Squeeze String

Snippet info

Language

Lua

Visibility

public

Author

captain.g

Created

2021-02-23T11:24:19Z

Updated

2021-02-23T11:27:27Z

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)
INFO