Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utf8 printing support #27

Merged
merged 5 commits into from
Dec 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/ltui/label.lua
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,20 @@ function label:splitext(text, width)
for idx = 1, #lines do
local line = lines[idx]
while #line > width do
table.insert(result, line:sub(1, width))
line = line:sub(width + 1)
local size = 0
for i = 1, #line do
if (line:byte(i) & 0xc0) ~= 0x80 then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, It does not work on luajit. We need bit module. bit.band(a, b)

size = size + 1
if size > width then
table.insert(result, line:sub(1, i - 1))
line = line:sub(i)
break
end
end
end
if size <= width then
break
end
end
table.insert(result, line)
end
Expand Down
15 changes: 10 additions & 5 deletions src/ltui/textedit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,18 @@ function textedit:on_event(e)

-- update text
if e.type == event.ev_keyboard then
if e.key_code > 0x1f and e.key_code < 0x7f then
self:text_set(self:text() .. e.key_name)
return true
elseif e.key_name == "Enter" and self:option("multiline") then
if e.key_name == "Enter" and self:option("multiline") then
self:text_set(self:text() .. '\n')
return true
elseif e.key_name == "Backspace" then
local text = self:text()
if #text > 0 then
self:text_set(text:sub(1, #text - 1))
local size = 1
-- while continuation byte
while (text:byte(#text - size + 1) & 0xc0) == 0x80 do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

size = size + 1
end
self:text_set(text:sub(1, #text - size))
end
return true
elseif e.key_name == "CtrlV" then
Expand All @@ -95,6 +97,9 @@ function textedit:on_event(e)
self:text_set(self:text() .. pastetext)
end
return true
elseif e.key_code > 0x1f and e.key_code < 0xf8 then
self:text_set(self:text() .. string.char(e.key_code))
return true
end
end

Expand Down