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

feat(terminal): check ambiguous unicode width #44

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions luasystem-scm-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ local function make_platform(plat)
'src/term.c',
'src/bitflags.c',
'src/wcwidth.c',
'src/wcwidtha.c',
},
defines = defines[plat],
libraries = libraries[plat],
Expand Down
6 changes: 6 additions & 0 deletions spec/04-term_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,12 @@ describe("Terminal:", function()
assert.same({2}, {system.utf8cwidth(ch2 .. ch3 .. ch4)})
end)

it("returns 2nd ambigious boolean value only if requested", function()
assert.same({1}, {system.utf8cwidth("¡", false)})
assert.same({1, true}, {system.utf8cwidth("¡", true)})
assert.same({1, false}, {system.utf8cwidth("a", true)})
end)

end)


Expand Down
11 changes: 10 additions & 1 deletion src/term.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
// Windows does not have a wcwidth function, so we use compatibilty code from
// http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c by Markus Kuhn
#include "wcwidth.h"
#include "wcwidtha.h" // ambiguous width checks for East Asian characters


#ifdef _WIN32
Expand Down Expand Up @@ -950,14 +951,16 @@ int utf8_to_wchar(const char *utf8, size_t len, mk_wchar_t *codepoint) {
Get the width of a utf8 character for terminal display.
@function utf8cwidth
@tparam string utf8_char the utf8 character to check, only the width of the first character will be returned
@tparam bool ambiguous if `true` a second return value will be returned; boolean indicating if the character is ambiguous
@treturn[1] int the display width in columns of the first character in the string (0 for an empty string)
@treturn[2] nil
@treturn[2] nil|bool if `ambiguous` is `true`, a boolean indicating if the character is ambiguous
@treturn[2] string error message
*/
int lst_utf8cwidth(lua_State *L) {
const char *utf8_char;
size_t utf8_len;
utf8_char = luaL_checklstring(L, 1, &utf8_len);
int ambiguous = lua_toboolean(L, 2);
int width = 0;

mk_wchar_t wc;
Expand All @@ -984,6 +987,12 @@ int lst_utf8cwidth(lua_State *L) {
}

lua_pushinteger(L, width);

if (ambiguous) {
// also check if the width is ambiguous
lua_pushboolean(L, mk_wcwidth_a(wc));
return 2;
}
return 1;
}

Expand Down
Loading
Loading