[A] Add function ft_set_u8strwid_func
to set custom function to compute width of utf8 strings
This commit is contained in:
@@ -5,6 +5,24 @@
|
||||
|
||||
#include "fort.h"
|
||||
|
||||
#if defined(FT_HAVE_UTF8)
|
||||
/* Custom function to compute visible width of utf8 strings */
|
||||
int u8strwid(const void *beg, const void *end, size_t *width)
|
||||
{
|
||||
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
|
||||
const char *emojis[] = {"😃", "😍"};
|
||||
const size_t sz = sizeof(emojis) / sizeof(emojis[0]);
|
||||
const size_t raw_len = (const char *)end - (const char *)beg;
|
||||
|
||||
for (size_t i = 0; i < sz; ++i) {
|
||||
if (memcmp(beg, emojis[i], MIN(strlen(emojis[i]), raw_len)) == 0) {
|
||||
*width = 2; /* On my terminal emojis have width of 2 chars */
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
int main(void)
|
||||
{
|
||||
@@ -28,6 +46,23 @@ int main(void)
|
||||
printf("%s\n", table_str);
|
||||
ft_destroy_table(table);
|
||||
}
|
||||
|
||||
/* Example of providing custom function to compute utf8 string width */
|
||||
{
|
||||
ft_set_u8strwid_func(&u8strwid);
|
||||
|
||||
ft_table_t *table = ft_create_table();
|
||||
ft_set_border_style(table, FT_NICE_STYLE);
|
||||
|
||||
ft_set_cell_prop(table, 0, FT_ANY_COLUMN, FT_CPROP_ROW_TYPE, FT_ROW_HEADER);
|
||||
ft_u8write_ln(table, "SMILING", "Native");
|
||||
ft_u8write_ln(table, "SMILING FACE WITH OPEN MOUTH", "😃");
|
||||
ft_u8write_ln(table, "SMILING FACE WITH HEART-SHAPED EYES", "😍");
|
||||
|
||||
const char *table_str = (const char *)ft_to_u8string(table);
|
||||
printf("%s\n", table_str);
|
||||
ft_destroy_table(table);
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Example of wchar table */
|
||||
|
@@ -1,8 +1,27 @@
|
||||
#include <iostream>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "fort.hpp"
|
||||
|
||||
#if defined(FT_HAVE_UTF8)
|
||||
/* Custom function to compute visible width of utf8 strings */
|
||||
int u8strwid(const void *beg, const void *end, size_t *width)
|
||||
{
|
||||
const char *emojis[] = {"😃", "😍"};
|
||||
const size_t sz = sizeof(emojis) / sizeof(emojis[0]);
|
||||
const size_t raw_len = (const char *)end - (const char *)beg;
|
||||
|
||||
for (size_t i = 0; i < sz; ++i) {
|
||||
if (memcmp(beg, emojis[i], std::min(strlen(emojis[i]), raw_len)) == 0) {
|
||||
*width = 2; /* On my terminal emojis have width of 2 chars */
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
@@ -24,6 +43,22 @@ int main(void)
|
||||
|
||||
std::cout << table.to_string() << std::endl;
|
||||
}
|
||||
|
||||
|
||||
/* Example of providing custom function to compute utf8 string width */
|
||||
{
|
||||
ft_set_u8strwid_func(&u8strwid);
|
||||
|
||||
fort::utf8_table table;
|
||||
table.set_border_style(FT_NICE_STYLE);
|
||||
|
||||
table << fort::header
|
||||
<< "Description" << "Native" << fort::endr
|
||||
<< "SMILING FACE WITH OPEN MOUTH" << "😃" << fort::endr
|
||||
<< "SMILING FACE WITH HEART-SHAPED EYES" << "😍" << fort::endr;
|
||||
|
||||
std::cout << table.to_string() << std::endl;
|
||||
}
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
|
Reference in New Issue
Block a user