utf-8: initial support
This commit is contained in:
@@ -6,6 +6,7 @@
|
|||||||
- [x] Create dynamic map of elements IDs and their pointers
|
- [x] Create dynamic map of elements IDs and their pointers
|
||||||
- [x] Adapt modals to new value-forwarding method
|
- [x] Adapt modals to new value-forwarding method
|
||||||
- [ ] More intuitive navigation between nested groups (by pressing left and right buttons)
|
- [ ] More intuitive navigation between nested groups (by pressing left and right buttons)
|
||||||
|
- [ ] UTF-8: correct sizing in modals
|
||||||
|
|
||||||
## Features:
|
## Features:
|
||||||
- [x] Search files
|
- [x] Search files
|
||||||
|
|||||||
@@ -1,4 +1,9 @@
|
|||||||
|
#include <ncursesw/ncurses.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <wchar.h>
|
||||||
|
|
||||||
size_t count_utf8_code_points(const char *s) {
|
size_t count_utf8_code_points(const char *s) {
|
||||||
size_t count = 0;
|
size_t count = 0;
|
||||||
@@ -7,3 +12,29 @@ size_t count_utf8_code_points(const char *s) {
|
|||||||
}
|
}
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int64_t convert_utf8_to_wide(const char *utf8_str, wchar_t **restrict wide_str,
|
||||||
|
size_t *restrict len) {
|
||||||
|
// Step 1: Convert UTF-8 (MBCS) to wchar_t (wide)
|
||||||
|
size_t wide_len =
|
||||||
|
count_utf8_code_points(utf8_str); // Get required length (-1 on error)
|
||||||
|
if (wide_len == (size_t)-1) {
|
||||||
|
*len = 0;
|
||||||
|
perror("mbstowcs failed");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
*wide_str =
|
||||||
|
malloc((wide_len + 1) * sizeof(wchar_t)); // +1 for null terminator
|
||||||
|
mbstowcs(*wide_str, utf8_str, wide_len + 1); // Perform conversion
|
||||||
|
*len = wide_len;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int64_t curs_printw(WINDOW *win, int64_t y, int64_t x, char *const utf8_str) {
|
||||||
|
wchar_t *wname = NULL;
|
||||||
|
size_t nsize = 0;
|
||||||
|
convert_utf8_to_wide(utf8_str, &wname, &nsize);
|
||||||
|
mvwaddwstr(win, y, x, wname);
|
||||||
|
free(wname);
|
||||||
|
return nsize;
|
||||||
|
}
|
||||||
|
|||||||
+5
-1
@@ -1,7 +1,11 @@
|
|||||||
#ifndef UTF8_H
|
#ifndef UTF8_H
|
||||||
#define UTF8_H
|
#define UTF8_H
|
||||||
|
|
||||||
|
#include <ncursesw/ncurses.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
size_t count_utf8_code_points(const char *s);
|
size_t count_utf8_code_points(const char *s);
|
||||||
|
int64_t convert_utf8_to_wide(const char *utf8_str, wchar_t **restrict wide_str,
|
||||||
|
size_t *restrict len);
|
||||||
|
int64_t curs_printw(WINDOW *win, int64_t y, int64_t x, char *const utf8_str);
|
||||||
#endif
|
#endif
|
||||||
@@ -169,8 +169,8 @@ void draw_file_list(ui_file_list_t *fui) {
|
|||||||
active_el = el;
|
active_el = el;
|
||||||
}
|
}
|
||||||
p_x = 1;
|
p_x = 1;
|
||||||
mvwprintw(parent_win, p_y, p_x, "%s%n", el->name, &p_x);
|
p_x += curs_printw(parent_win, p_y, p_x, el->name);
|
||||||
mvwprintw(parent_win, p_y, p_x + 1, "%*s", sz_x - p_x, "");
|
mvwprintw(parent_win, p_y, p_x, "%*s", sz_x - p_x, "");
|
||||||
if (cur_el_idx == fui->current_idx) {
|
if (cur_el_idx == fui->current_idx) {
|
||||||
wattroff(parent_win, A_BOLD | A_REVERSE);
|
wattroff(parent_win, A_BOLD | A_REVERSE);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <uchar.h>
|
#include <uchar.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <utils.h>
|
||||||
|
|
||||||
void reset_file_list(ui_fs_file_list_t *fl_ui);
|
void reset_file_list(ui_fs_file_list_t *fl_ui);
|
||||||
|
|
||||||
@@ -101,8 +102,7 @@ void select_item(ui_fs_file_list_t *fui, int32_t *resp_data) {
|
|||||||
} else {
|
} else {
|
||||||
dp_current = strlen(fui->d_path);
|
dp_current = strlen(fui->d_path);
|
||||||
selection = fui->current->name + 1;
|
selection = fui->current->name + 1;
|
||||||
fui->d_path =
|
fui->d_path = realloc(fui->d_path, dp_current + strlen(selection) + 2);
|
||||||
realloc(fui->d_path, dp_current + strlen(selection) + 2);
|
|
||||||
sprintf(fui->d_path, "%s/%s", fui->d_path, selection);
|
sprintf(fui->d_path, "%s/%s", fui->d_path, selection);
|
||||||
fl_clear(&(fui->start), &(fui->current));
|
fl_clear(&(fui->start), &(fui->current));
|
||||||
fui->start = get_files_from_fs(fui->d_path);
|
fui->start = get_files_from_fs(fui->d_path);
|
||||||
@@ -172,7 +172,6 @@ void draw_fs_file_list(ui_fs_file_list_t *fl_ui) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
wattrset(win, A_REVERSE); /* Match with modal background */
|
wattrset(win, A_REVERSE); /* Match with modal background */
|
||||||
|
|
||||||
do {
|
do {
|
||||||
if (p_y < 1) {
|
if (p_y < 1) {
|
||||||
p_y++;
|
p_y++;
|
||||||
@@ -182,14 +181,8 @@ void draw_fs_file_list(ui_fs_file_list_t *fl_ui) {
|
|||||||
wattrset(win, COLOR_PAIR(3) | A_BOLD | A_REVERSE);
|
wattrset(win, COLOR_PAIR(3) | A_BOLD | A_REVERSE);
|
||||||
}
|
}
|
||||||
p_x = 1;
|
p_x = 1;
|
||||||
/* TODO: */
|
p_x += curs_printw(win, p_y, p_x, el->name);
|
||||||
wchar_t wname[512];
|
mvwprintw(win, p_y, p_x, "%*s", sz_x_f - p_x, "");
|
||||||
mbstowcs(wname, el->name, 512);
|
|
||||||
// wchar_t text[] = L"ВUTF-8 работает! → ★ ℝ 日本語\n";
|
|
||||||
mvwaddwstr(win, p_y, p_x++, L"Ё");
|
|
||||||
mvwaddwstr(win, p_y, p_x++, L"Ж");
|
|
||||||
// mvwprintw(win, p_y, p_x, "%s%n", el->name, &p_x);
|
|
||||||
mvwprintw(win, p_y, p_x + 1, "%*s", sz_x_f - p_x, "");
|
|
||||||
if (el == fl_ui->current) {
|
if (el == fl_ui->current) {
|
||||||
wattroff(win, COLOR_PAIR(3) | A_BOLD);
|
wattroff(win, COLOR_PAIR(3) | A_BOLD);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,11 @@ add_library(widget_core STATIC)
|
|||||||
|
|
||||||
file(GLOB M_SOURCES "*.c")
|
file(GLOB M_SOURCES "*.c")
|
||||||
|
|
||||||
|
target_link_libraries(widget_core
|
||||||
|
PRIVATE
|
||||||
|
utils
|
||||||
|
)
|
||||||
|
|
||||||
target_sources(widget_core
|
target_sources(widget_core
|
||||||
PRIVATE
|
PRIVATE
|
||||||
${M_SOURCES}
|
${M_SOURCES}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <utils.h>
|
||||||
|
|
||||||
extern uint32_t m_id;
|
extern uint32_t m_id;
|
||||||
int32_t rsize_to_value(int32_t size, enum rsize rsize);
|
int32_t rsize_to_value(int32_t size, enum rsize rsize);
|
||||||
@@ -14,7 +15,8 @@ void init_widget(widget_t *w, widget_t *w_parent, WINDOW **win, char *title) {
|
|||||||
w->sz.y = 0;
|
w->sz.y = 0;
|
||||||
w->ps.x = 0;
|
w->ps.x = 0;
|
||||||
w->ps.y = 0;
|
w->ps.y = 0;
|
||||||
if (w_parent != NULL && w_parent->w_parent != NULL) { /* not root element of the window */
|
if (w_parent != NULL &&
|
||||||
|
w_parent->w_parent != NULL) { /* not root element of the window */
|
||||||
w->m.x = w_parent->m.x;
|
w->m.x = w_parent->m.x;
|
||||||
w->m.y = w_parent->m.y;
|
w->m.y = w_parent->m.y;
|
||||||
} else {
|
} else {
|
||||||
@@ -56,11 +58,11 @@ int32_t get_max_line_len(const char *text, uint32_t *line_count) {
|
|||||||
strncpy(l_buf, text + i - c_line_len, c_line_len); \
|
strncpy(l_buf, text + i - c_line_len, c_line_len); \
|
||||||
l_buf[c_line_len] = '\0'; \
|
l_buf[c_line_len] = '\0'; \
|
||||||
if ((attrs & PMT_ALIGN_CENTER) == PMT_ALIGN_CENTER) { \
|
if ((attrs & PMT_ALIGN_CENTER) == PMT_ALIGN_CENTER) { \
|
||||||
mvwprintw(win, line_v_pos, (win_width - c_line_len) / 2, "%s", l_buf); \
|
curs_printw(win, line_v_pos, (win_width - c_line_len) / 2, l_buf); \
|
||||||
} else if (attrs & PMT_POS_CENTER) { \
|
} else if (attrs & PMT_POS_CENTER) { \
|
||||||
mvwprintw(win, line_v_pos, (win_width - m_line_len) / 2, "%s", l_buf); \
|
curs_printw(win, line_v_pos, (win_width - m_line_len) / 2, l_buf); \
|
||||||
} else { \
|
} else { \
|
||||||
mvwprintw(win, line_v_pos, x, "%s", l_buf); \
|
curs_printw(win, line_v_pos, x, l_buf); \
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t print_multiline_text(WINDOW *win, const char *_text,
|
uint32_t print_multiline_text(WINDOW *win, const char *_text,
|
||||||
|
|||||||
Reference in New Issue
Block a user