mirror of
https://github.com/profanity-im/profanity.git
synced 2025-06-02 08:53:38 -04:00
Added utf8_display_len
This commit is contained in:
parent
4ac11ddcd6
commit
ba89297382
22
src/common.c
22
src/common.c
@ -202,6 +202,28 @@ str_contains(const char str[], int size, char ch)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
utf8_display_len(const char * const str)
|
||||||
|
{
|
||||||
|
if (!str) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int len = 0;
|
||||||
|
gchar *curr = g_utf8_offset_to_pointer(str, 0);
|
||||||
|
while (*curr != '\0') {
|
||||||
|
gunichar curru = g_utf8_get_char(curr);
|
||||||
|
if (g_unichar_iswide(curru)) {
|
||||||
|
len += 2;
|
||||||
|
} else {
|
||||||
|
len ++;
|
||||||
|
}
|
||||||
|
curr = g_utf8_next_char(curr);
|
||||||
|
}
|
||||||
|
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
char *
|
char *
|
||||||
prof_getline(FILE *stream)
|
prof_getline(FILE *stream)
|
||||||
{
|
{
|
||||||
|
@ -104,6 +104,7 @@ gboolean mkdir_recursive(const char *dir);
|
|||||||
char * str_replace(const char *string, const char *substr,
|
char * str_replace(const char *string, const char *substr,
|
||||||
const char *replacement);
|
const char *replacement);
|
||||||
int str_contains(const char str[], int size, char ch);
|
int str_contains(const char str[], int size, char ch);
|
||||||
|
int utf8_display_len(const char * const str);
|
||||||
char * prof_getline(FILE *stream);
|
char * prof_getline(FILE *stream);
|
||||||
char* release_get_latest(void);
|
char* release_get_latest(void);
|
||||||
gboolean release_is_new(char *found_version);
|
gboolean release_is_new(char *found_version);
|
||||||
|
@ -92,7 +92,6 @@ static int _printable(const wint_t ch);
|
|||||||
static void _clear_input(void);
|
static void _clear_input(void);
|
||||||
static void _go_to_end(void);
|
static void _go_to_end(void);
|
||||||
static void _delete_previous_word(void);
|
static void _delete_previous_word(void);
|
||||||
static int _get_display_length(void);
|
|
||||||
|
|
||||||
void
|
void
|
||||||
create_input_window(void)
|
create_input_window(void)
|
||||||
@ -145,7 +144,7 @@ inp_block(void)
|
|||||||
char *
|
char *
|
||||||
inp_read(int *key_type, wint_t *ch)
|
inp_read(int *key_type, wint_t *ch)
|
||||||
{
|
{
|
||||||
int display_size = _get_display_length();
|
int display_size = utf8_display_len(input);
|
||||||
|
|
||||||
// echo off, and get some more input
|
// echo off, and get some more input
|
||||||
noecho();
|
noecho();
|
||||||
@ -260,24 +259,6 @@ inp_put_back(void)
|
|||||||
_inp_win_update_virtual();
|
_inp_win_update_virtual();
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
|
||||||
_get_display_length(void)
|
|
||||||
{
|
|
||||||
int len = 0;
|
|
||||||
gchar *curr = g_utf8_offset_to_pointer(input, 0);
|
|
||||||
while (*curr != '\0') {
|
|
||||||
gunichar curru = g_utf8_get_char(curr);
|
|
||||||
if (g_unichar_iswide(curru)) {
|
|
||||||
len += 2;
|
|
||||||
} else {
|
|
||||||
len ++;
|
|
||||||
}
|
|
||||||
curr = g_utf8_next_char(curr);
|
|
||||||
}
|
|
||||||
|
|
||||||
return len;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
void
|
||||||
inp_replace_input(const char * const new_input)
|
inp_replace_input(const char * const new_input)
|
||||||
{
|
{
|
||||||
@ -322,7 +303,7 @@ _handle_edit(int key_type, const wint_t ch)
|
|||||||
char *next = NULL;
|
char *next = NULL;
|
||||||
int inp_x = getcurx(inp_win);
|
int inp_x = getcurx(inp_win);
|
||||||
int next_ch;
|
int next_ch;
|
||||||
int display_size = _get_display_length();
|
int display_size = utf8_display_len(input);
|
||||||
|
|
||||||
// CTRL-LEFT
|
// CTRL-LEFT
|
||||||
if ((key_type == KEY_CODE_YES) && (ch == 547 || ch == 545 || ch == 544 || ch == 540 || ch == 539) && (inp_x > 0)) {
|
if ((key_type == KEY_CODE_YES) && (ch == 547 || ch == 545 || ch == 544 || ch == 540 || ch == 539) && (inp_x > 0)) {
|
||||||
@ -608,7 +589,7 @@ static void
|
|||||||
_handle_backspace(void)
|
_handle_backspace(void)
|
||||||
{
|
{
|
||||||
int inp_x = getcurx(inp_win);
|
int inp_x = getcurx(inp_win);
|
||||||
int display_size = _get_display_length();
|
int display_size = utf8_display_len(input);
|
||||||
roster_reset_search_attempts();
|
roster_reset_search_attempts();
|
||||||
if (display_size > 0) {
|
if (display_size > 0) {
|
||||||
|
|
||||||
@ -784,7 +765,7 @@ _delete_previous_word(void)
|
|||||||
static void
|
static void
|
||||||
_go_to_end(void)
|
_go_to_end(void)
|
||||||
{
|
{
|
||||||
int display_size = _get_display_length();
|
int display_size = utf8_display_len(input);
|
||||||
wmove(inp_win, 0, display_size);
|
wmove(inp_win, 0, display_size);
|
||||||
if (display_size > cols-2) {
|
if (display_size > cols-2) {
|
||||||
pad_start = display_size - cols + 1;
|
pad_start = display_size - cols + 1;
|
||||||
|
@ -544,3 +544,46 @@ void test_p_sha1_hash7(void **state)
|
|||||||
|
|
||||||
assert_string_equal(result, "bNfKVfqEOGmzlH8M+e8FYTB46SU=");
|
assert_string_equal(result, "bNfKVfqEOGmzlH8M+e8FYTB46SU=");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void utf8_display_len_null_str(void **state)
|
||||||
|
{
|
||||||
|
int result = utf8_display_len(NULL);
|
||||||
|
|
||||||
|
assert_int_equal(0, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
void utf8_display_len_1_non_wide(void **state)
|
||||||
|
{
|
||||||
|
int result = utf8_display_len("1");
|
||||||
|
|
||||||
|
assert_int_equal(1, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
void utf8_display_len_1_wide(void **state)
|
||||||
|
{
|
||||||
|
int result = utf8_display_len("四");
|
||||||
|
|
||||||
|
assert_int_equal(2, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
void utf8_display_len_non_wide(void **state)
|
||||||
|
{
|
||||||
|
int result = utf8_display_len("123456789abcdef");
|
||||||
|
|
||||||
|
assert_int_equal(15, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
void utf8_display_len_wide(void **state)
|
||||||
|
{
|
||||||
|
int result = utf8_display_len("12三四56");
|
||||||
|
|
||||||
|
assert_int_equal(8, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
void utf8_display_len_all_wide(void **state)
|
||||||
|
{
|
||||||
|
int result = utf8_display_len("ひらがな");
|
||||||
|
|
||||||
|
assert_int_equal(8, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -46,3 +46,9 @@ void test_p_sha1_hash5(void **state);
|
|||||||
void test_p_sha1_hash6(void **state);
|
void test_p_sha1_hash6(void **state);
|
||||||
void test_p_sha1_hash6(void **state);
|
void test_p_sha1_hash6(void **state);
|
||||||
void test_p_sha1_hash7(void **state);
|
void test_p_sha1_hash7(void **state);
|
||||||
|
void utf8_display_len_null_str(void **state);
|
||||||
|
void utf8_display_len_1_non_wide(void **state);
|
||||||
|
void utf8_display_len_1_wide(void **state);
|
||||||
|
void utf8_display_len_non_wide(void **state);
|
||||||
|
void utf8_display_len_wide(void **state);
|
||||||
|
void utf8_display_len_all_wide(void **state);
|
@ -85,6 +85,12 @@ int main(int argc, char* argv[]) {
|
|||||||
unit_test(test_p_sha1_hash5),
|
unit_test(test_p_sha1_hash5),
|
||||||
unit_test(test_p_sha1_hash6),
|
unit_test(test_p_sha1_hash6),
|
||||||
unit_test(test_p_sha1_hash7),
|
unit_test(test_p_sha1_hash7),
|
||||||
|
unit_test(utf8_display_len_null_str),
|
||||||
|
unit_test(utf8_display_len_1_non_wide),
|
||||||
|
unit_test(utf8_display_len_1_wide),
|
||||||
|
unit_test(utf8_display_len_non_wide),
|
||||||
|
unit_test(utf8_display_len_wide),
|
||||||
|
unit_test(utf8_display_len_all_wide),
|
||||||
|
|
||||||
unit_test(clear_empty),
|
unit_test(clear_empty),
|
||||||
unit_test(reset_after_create),
|
unit_test(reset_after_create),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user