Skip to content

Instantly share code, notes, and snippets.

@diazvictor
Created October 16, 2023 17:59
Show Gist options
  • Save diazvictor/e47d44194f22c5b37d19bd83d1c768fc to your computer and use it in GitHub Desktop.
Save diazvictor/e47d44194f22c5b37d19bd83d1c768fc to your computer and use it in GitHub Desktop.
A truncate string function
--[[--
@package Solus Frontend
@filename truncate.lua
@version 1.0
@author Díaz Urbaneja Víctor Eduardo Diex <victor.vector008@gmail.com>
@date 04.01.2021 19:53:44 -04
@see https://github.com/diazvictor/solus-organizer/blob/solus-new/test/truncate.lua
]]
local str = "Donkey Kong Country 3 - Dixie Kong's Double Trouble.sfc"
-- make in 34min
function string.truncate(str, max, min)
local t, s, max, min = {}, str, max or 20, min or 0
if #str > min then
for w in string.gmatch(str, ".") do
table.insert(t, w)
if #t > max then
break
end
end
s = ('%s...'):format(table.concat(t))
return s
else
return s
end
end
-- Donkey Kong Country 3...
print(str:truncate())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment