utf-8: initial support
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
- [x] Create dynamic map of elements IDs and their pointers
|
||||
- [x] Adapt modals to new value-forwarding method
|
||||
- [ ] More intuitive navigation between nested groups (by pressing left and right buttons)
|
||||
- [ ] UTF-8: correct sizing in modals
|
||||
|
||||
## Features:
|
||||
- [x] Search files
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
#include <ncursesw/ncurses.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 = 0;
|
||||
@@ -7,3 +12,29 @@ size_t count_utf8_code_points(const char *s) {
|
||||
}
|
||||
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
|
||||
#define UTF8_H
|
||||
|
||||
#include <ncursesw/ncurses.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
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
|
||||
@@ -169,8 +169,8 @@ void draw_file_list(ui_file_list_t *fui) {
|
||||
active_el = el;
|
||||
}
|
||||
p_x = 1;
|
||||
mvwprintw(parent_win, p_y, p_x, "%s%n", el->name, &p_x);
|
||||
mvwprintw(parent_win, p_y, p_x + 1, "%*s", sz_x - p_x, "");
|
||||
p_x += curs_printw(parent_win, p_y, p_x, el->name);
|
||||
mvwprintw(parent_win, p_y, p_x, "%*s", sz_x - p_x, "");
|
||||
if (cur_el_idx == fui->current_idx) {
|
||||
wattroff(parent_win, A_BOLD | A_REVERSE);
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include <sys/types.h>
|
||||
#include <uchar.h>
|
||||
#include <unistd.h>
|
||||
#include <utils.h>
|
||||
|
||||
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 {
|
||||
dp_current = strlen(fui->d_path);
|
||||
selection = fui->current->name + 1;
|
||||
fui->d_path =
|
||||
realloc(fui->d_path, dp_current + strlen(selection) + 2);
|
||||
fui->d_path = realloc(fui->d_path, dp_current + strlen(selection) + 2);
|
||||
sprintf(fui->d_path, "%s/%s", fui->d_path, selection);
|
||||
fl_clear(&(fui->start), &(fui->current));
|
||||
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 */
|
||||
|
||||
do {
|
||||
if (p_y < 1) {
|
||||
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);
|
||||
}
|
||||
p_x = 1;
|
||||
/* TODO: */
|
||||
wchar_t wname[512];
|
||||
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, "");
|
||||
p_x += curs_printw(win, p_y, p_x, el->name);
|
||||
mvwprintw(win, p_y, p_x, "%*s", sz_x_f - p_x, "");
|
||||
if (el == fl_ui->current) {
|
||||
wattroff(win, COLOR_PAIR(3) | A_BOLD);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,11 @@ add_library(widget_core STATIC)
|
||||
|
||||
file(GLOB M_SOURCES "*.c")
|
||||
|
||||
target_link_libraries(widget_core
|
||||
PRIVATE
|
||||
utils
|
||||
)
|
||||
|
||||
target_sources(widget_core
|
||||
PRIVATE
|
||||
${M_SOURCES}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <utils.h>
|
||||
|
||||
extern uint32_t m_id;
|
||||
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->ps.x = 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.y = w_parent->m.y;
|
||||
} 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); \
|
||||
l_buf[c_line_len] = '\0'; \
|
||||
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) { \
|
||||
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 { \
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user