diff options
Diffstat (limited to 'src/term.c')
| -rw-r--r-- | src/term.c | 15 |
1 files changed, 11 insertions, 4 deletions
| @@ -1085,6 +1085,7 @@ int utf8_to_wchar(const char *utf8, size_t len, mk_wchar_t *codepoint) { | |||
| 1085 | Get the width of a utf8 character for terminal display. | 1085 | Get the width of a utf8 character for terminal display. |
| 1086 | @function utf8cwidth | 1086 | @function utf8cwidth |
| 1087 | @tparam string|int utf8_char the utf8 character, or unicode codepoint, to check, only the width of the first character will be returned | 1087 | @tparam string|int utf8_char the utf8 character, or unicode codepoint, to check, only the width of the first character will be returned |
| 1088 | @tparam[opt=1] int ambiguous_width the width to return for ambiguous width characters (usually 1 or 2) | ||
| 1088 | @treturn[1] int the display width in columns of the first character in the string (0 for an empty string) | 1089 | @treturn[1] int the display width in columns of the first character in the string (0 for an empty string) |
| 1089 | @treturn[2] nil | 1090 | @treturn[2] nil |
| 1090 | @treturn[2] string error message | 1091 | @treturn[2] string error message |
| @@ -1093,6 +1094,7 @@ Get the width of a utf8 character for terminal display. | |||
| 1093 | int lst_utf8cwidth(lua_State *L) { | 1094 | int lst_utf8cwidth(lua_State *L) { |
| 1094 | int width = 0; | 1095 | int width = 0; |
| 1095 | mk_wchar_t wc; | 1096 | mk_wchar_t wc; |
| 1097 | int ambiguous_width = luaL_optinteger(L, 2, 1); | ||
| 1096 | 1098 | ||
| 1097 | if (lua_type(L, 1) == LUA_TSTRING) { | 1099 | if (lua_type(L, 1) == LUA_TSTRING) { |
| 1098 | // Handle UTF8 as string input | 1100 | // Handle UTF8 as string input |
| @@ -1129,10 +1131,10 @@ int lst_utf8cwidth(lua_State *L) { | |||
| 1129 | } | 1131 | } |
| 1130 | 1132 | ||
| 1131 | // Get the width of the wide character | 1133 | // Get the width of the wide character |
| 1132 | width = mk_wcwidth(wc); | 1134 | width = mk_wcwidth(wc, ambiguous_width); |
| 1133 | if (width == -1) { | 1135 | if (width == -1) { |
| 1134 | lua_pushnil(L); | 1136 | lua_pushnil(L); |
| 1135 | lua_pushstring(L, "Character width determination failed"); | 1137 | lua_pushstring(L, "Control characters have no width"); |
| 1136 | return 2; | 1138 | return 2; |
| 1137 | } | 1139 | } |
| 1138 | 1140 | ||
| @@ -1147,6 +1149,7 @@ int lst_utf8cwidth(lua_State *L) { | |||
| 1147 | Get the width of a utf8 string for terminal display. | 1149 | Get the width of a utf8 string for terminal display. |
| 1148 | @function utf8swidth | 1150 | @function utf8swidth |
| 1149 | @tparam string utf8_string the utf8 string to check | 1151 | @tparam string utf8_string the utf8 string to check |
| 1152 | @tparam[opt=1] int ambiguous_width the width to return for ambiguous width characters (1 or 2) | ||
| 1150 | @treturn[1] int the display width of the string in columns (0 for an empty string) | 1153 | @treturn[1] int the display width of the string in columns (0 for an empty string) |
| 1151 | @treturn[2] nil | 1154 | @treturn[2] nil |
| 1152 | @treturn[2] string error message | 1155 | @treturn[2] string error message |
| @@ -1156,6 +1159,10 @@ int lst_utf8swidth(lua_State *L) { | |||
| 1156 | const char *utf8_str; | 1159 | const char *utf8_str; |
| 1157 | size_t utf8_len; | 1160 | size_t utf8_len; |
| 1158 | utf8_str = luaL_checklstring(L, 1, &utf8_len); | 1161 | utf8_str = luaL_checklstring(L, 1, &utf8_len); |
| 1162 | int ambiguous_width = luaL_optinteger(L, 2, 1); | ||
| 1163 | if (ambiguous_width != 1 && ambiguous_width != 2) { | ||
| 1164 | return luaL_argerror(L, 2, "Ambiguous width must be 1 or 2"); | ||
| 1165 | } | ||
| 1159 | int total_width = 0; | 1166 | int total_width = 0; |
| 1160 | 1167 | ||
| 1161 | if (utf8_len == 0) { | 1168 | if (utf8_len == 0) { |
| @@ -1175,10 +1182,10 @@ int lst_utf8swidth(lua_State *L) { | |||
| 1175 | return 2; | 1182 | return 2; |
| 1176 | } | 1183 | } |
| 1177 | 1184 | ||
| 1178 | int width = mk_wcwidth(wc); | 1185 | int width = mk_wcwidth(wc, ambiguous_width); |
| 1179 | if (width == -1) { | 1186 | if (width == -1) { |
| 1180 | lua_pushnil(L); | 1187 | lua_pushnil(L); |
| 1181 | lua_pushstring(L, "Character width determination failed"); | 1188 | lua_pushstring(L, "Control characters have no width"); |
| 1182 | return 2; | 1189 | return 2; |
| 1183 | } | 1190 | } |
| 1184 | 1191 | ||
