aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/term.c47
1 files changed, 32 insertions, 15 deletions
diff --git a/src/term.c b/src/term.c
index 8c2b87a..2375080 100644
--- a/src/term.c
+++ b/src/term.c
@@ -953,30 +953,47 @@ int utf8_to_wchar(const char *utf8, size_t len, mk_wchar_t *codepoint) {
953/*** 953/***
954Get the width of a utf8 character for terminal display. 954Get the width of a utf8 character for terminal display.
955@function utf8cwidth 955@function utf8cwidth
956@tparam string utf8_char the utf8 character to check, only the width of the first character will be returned 956@tparam string|int utf8_char the utf8 character, or unicode codepoint, to check, only the width of the first character will be returned
957@treturn[1] int the display width in columns of the first character in the string (0 for an empty string) 957@treturn[1] int the display width in columns of the first character in the string (0 for an empty string)
958@treturn[2] nil 958@treturn[2] nil
959@treturn[2] string error message 959@treturn[2] string error message
960*/ 960*/
961int lst_utf8cwidth(lua_State *L) { 961int lst_utf8cwidth(lua_State *L) {
962 const char *utf8_char;
963 size_t utf8_len;
964 utf8_char = luaL_checklstring(L, 1, &utf8_len);
965 int width = 0; 962 int width = 0;
966
967 mk_wchar_t wc; 963 mk_wchar_t wc;
968 964
969 if (utf8_len == 0) { 965 if (lua_type(L, 1) == LUA_TSTRING) {
970 lua_pushinteger(L, 0); 966 // Handle UTF8 as string input
971 return 1; 967 const char *utf8_char;
972 } 968 size_t utf8_len;
969 utf8_char = luaL_checklstring(L, 1, &utf8_len);
973 970
974 // Convert the UTF-8 string to a wide character 971 if (utf8_len == 0) {
975 int bytes_processed = utf8_to_wchar(utf8_char, utf8_len, &wc); 972 lua_pushinteger(L, 0);
976 if (bytes_processed == -1) { 973 return 1;
977 lua_pushnil(L); 974 }
978 lua_pushstring(L, "Invalid UTF-8 character"); 975
979 return 2; 976 // Convert the UTF-8 string to a wide character
977 int bytes_processed = utf8_to_wchar(utf8_char, utf8_len, &wc);
978 if (bytes_processed == -1) {
979 lua_pushnil(L);
980 lua_pushstring(L, "Invalid UTF-8 character");
981 return 2;
982 }
983
984 } else if (lua_type(L, 1) == LUA_TNUMBER) {
985 // Handle codepoint input
986 int codepoint = luaL_checkinteger(L, 1);
987
988 if (codepoint < 0 || codepoint > 0x10FFFF) {
989 lua_pushnil(L);
990 lua_pushstring(L, "Invalid Unicode codepoint");
991 return 2;
992 }
993 wc = (mk_wchar_t)codepoint;
994
995 } else {
996 return luaL_argerror(L, 1, "Expected UTF-8-string or codepoint-integer as first argument");
980 } 997 }
981 998
982 // Get the width of the wide character 999 // Get the width of the wide character