OC-misc/wip/OpenOS/usr/lib/printwidth.lua

58 lines
902 B
Lua

local _, unicode = pcall(require, "unicode")
local widths = {
[32] = 3,
[33] = 1,
[34] = 3,
[39] = 1,
[40] = 3,
[41] = 3,
[42] = 3,
[44] = 1,
[46] = 1,
[58] = 1,
[59] = 1,
[60] = 4,
[62] = 4,
[64] = 6,
[73] = 3,
[91] = 3,
[93] = 3,
[96] = 2,
[102] = 4,
[105] = 1,
[107] = 4,
[108] = 2,
[116] = 3,
[123] = 3,
[124] = 1,
[125] = 3,
[126] = 6,
}
local sub = (unicode or {}).sub or string.sub
local function width(str)
local dispstr = str:gsub("§[%dabcdefklmnor]","")
local rlen = dispstr:len()
local cc, offset = false, 0
for i = 1, unicode.len(str) do
local c = unicode.sub(str,i,i)
if cc then
if c == "l" then
offset = 1
elseif c == "r" then
offset = 0
end
cc = false
else
if c == "§" then
cc = true
else
rlen = rlen + offset + (widths[string.byte(c)] or 5)
end
end
end
return rlen
end
return {width=width,widths=widths}