Skip to content

Commit

Permalink
Style Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
oshaboy committed Nov 10, 2023
1 parent a1d8041 commit 2d4f18a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 67 deletions.
80 changes: 22 additions & 58 deletions tools/chafa/chafa.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,39 +189,17 @@ static gboolean
write_to_stdout (gconstpointer buf, gsize len)
{
gboolean result = TRUE;
char * converted_buf = NULL;

if (len == 0)
return TRUE;

#ifdef G_OS_WIN32
if (options.output_utf_16_on_windows){
gsize tmp;
buf = converted_buf = g_convert (
buf,
len,
"UTF-16LE",
"UTF-8",
NULL,
&tmp,
NULL
);
len = tmp;
}

{
HANDLE chd = GetStdHandle (STD_OUTPUT_HANDLE);
gsize total_written = 0;
gboolean (*safe_WriteConsole) (HANDLE, const void *, gsize);
gint stride;
const void * newline;
/* We need to select whether to use the UTF-8 (Codepage 65001) or UTF-16 semantics */
if (options.output_utf_16_on_windows){
stride = 2;
safe_WriteConsole = safe_WriteConsoleW;
newline = L"\r\n";
} else {
stride = 1;
safe_WriteConsole = safe_WriteConsoleA;
newline = "\r\n";
}
const void *const newline = "\r\n";

{

/* on MS Windows. We convert line feeds to DOS-style CRLF as we go. */
Expand All @@ -230,25 +208,22 @@ write_to_stdout (gconstpointer buf, gsize len)
chd != INVALID_HANDLE_VALUE && total_written < len;
p0 = p1)
{
if (options.output_utf_16_on_windows)
p1 = (const gchar *) wmemchr ((const gunichar2 *) p0, L'\n', (end - p0)/2);
else
p1 = memchr (p0, '\n', end - p0);
p1 = memchr (p0, '\n', end - p0);
if (!p1)
p1 = end;

if (!safe_WriteConsole (chd, p0, (p1 - p0)/stride))
if (!safe_WriteConsoleA (chd, p0, p1 - p0))
break;

total_written += p1 - p0;

if (p1 != end)
{
if (!safe_WriteConsole (chd, newline, 2))
if (!safe_WriteConsoleA (chd, newline, 2))
break;

p1 += stride;
total_written += stride;
p1 += 1;
total_written += 1;
}
}
}
Expand All @@ -271,7 +246,6 @@ write_to_stdout (gconstpointer buf, gsize len)


#endif
if (converted_buf) g_free (converted_buf);
return result;
}

Expand Down Expand Up @@ -442,8 +416,8 @@ print_summary (void)
" --polite=BOOL Polite mode [on, off]. Inhibits escape sequences that may\n"
" confuse other programs. Defaults to off.\n"

" --utf16 Windows only, output using UTF16 functions,\n"
" required for compatibility with older versions of Windows\n"
/*" --utf16 Windows only, output using UTF16 functions,\n"
" required for compatibility with older versions of Windows\n"*/
"\nSize and layout:\n"

" -C, --center=BOOL Center images horizontally in view [on, off]. Default off.\n"
Expand Down Expand Up @@ -1538,19 +1512,6 @@ tty_options_init (void)
}

}
if (!options.output_utf_16_on_windows){
/* Set UTF-8 code page output */
SetConsoleOutputCP (65001);

/* Set UTF-8 code page input, for good measure */
SetConsoleCP (65001);
}

if (win32_stdout_is_file &&
options.output_utf_16_on_windows &&
(options.pixel_mode == CHAFA_PIXEL_MODE_SYMBOLS)

) safe_WriteConsoleW (chd, u"\xfeff", 1);

}
#endif
Expand Down Expand Up @@ -1834,7 +1795,7 @@ parse_options (int *argc, char **argv [])
{ "symbols", '\0', 0, G_OPTION_ARG_CALLBACK, parse_symbols_arg, "Output symbols", NULL },
{ "threads", '\0', 0, G_OPTION_ARG_INT, &options.n_threads, "Number of threads", NULL },
{ "threshold", 't', 0, G_OPTION_ARG_DOUBLE, &options.transparency_threshold, "Transparency threshold", NULL },
{ "utf16", '\0', 0, G_OPTION_ARG_NONE, &options.output_utf_16_on_windows, "Use WriteConsoleW instead of WriteConsoleA", NULL},
/*{ "utf16", '\0', 0, G_OPTION_ARG_NONE, &options.output_utf_16_on_windows, "Use WriteConsoleW instead of WriteConsoleA", NULL},*/
{ "view-size", '\0', 0, G_OPTION_ARG_CALLBACK, parse_view_size_arg, "View size", NULL },
{ "watch", '\0', 0, G_OPTION_ARG_NONE, &options.watch, "Watch a file's contents", NULL },
/* Deprecated: Equivalent to --scale max */
Expand Down Expand Up @@ -2547,7 +2508,6 @@ build_canvas (ChafaPixelType pixel_type, const guint8 *pixels,
chafa_image_unref (image);
chafa_frame_unref (frame);
return canvas;

}

static void
Expand Down Expand Up @@ -2702,20 +2662,24 @@ run_generic (const gchar *filename, gboolean is_first_file, gboolean is_first_fr
);

#ifdef G_OS_WIN32
if (options.is_conhost_mode){
ConhostRow * lines;
if (options.is_conhost_mode)
{
ConhostRow *lines;
gsize s;
s=canvas_to_conhost (canvas, &lines);

s = canvas_to_conhost (canvas, &lines);
write_image_conhost (lines, s);
destroy_lines (lines,s);
} else
}
else
#endif
{
chafa_canvas_print_rows (canvas, options.term_info, &gsa, NULL);
if (!write_image_prologue (is_first_file, is_first_frame, is_animation, dest_height)
|| !write_image (gsa, dest_width)
|| !write_image_epilogue (dest_width)
|| fflush (stdout) != 0){
|| fflush (stdout) != 0)
{
chafa_free_gstring_array (gsa);
goto out;
}
Expand Down
24 changes: 15 additions & 9 deletions tools/chafa/conhost.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ canvas_to_conhost (ChafaCanvas * canvas, ConhostRow ** lines)
const ChafaCanvasConfig * config;
ChafaCanvasMode canvas_mode;



config = chafa_canvas_peek_config (canvas);
canvas_mode = chafa_canvas_config_get_canvas_mode (config);
if (
Expand All @@ -41,7 +39,8 @@ canvas_to_conhost (ChafaCanvas * canvas, ConhostRow ** lines)
0,4,2,6,1,5,3,7,
8,12,10,14,9,13,11,15
};
for (gint y = 0; y<height; y++){
for (gint y = 0; y<height; y++)
{
ConhostRow * const line = (*lines)+y;
*line=(ConhostRow) {
.attributes = g_malloc (width*sizeof(attribute)),
Expand All @@ -50,7 +49,8 @@ canvas_to_conhost (ChafaCanvas * canvas, ConhostRow ** lines)
.utf16_string_length = 0
};

for (int x=0; x<width; x++){
for (int x=0; x<width; x++)
{
gunichar c = chafa_canvas_get_char_at (canvas,x,y);
gunichar2 utf16_codes[2];
gsize s = unichar_to_utf16 (c, utf16_codes);
Expand All @@ -61,12 +61,14 @@ canvas_to_conhost (ChafaCanvas * canvas, ConhostRow ** lines)

if (canvas_mode == CHAFA_CANVAS_MODE_FGBG)
line->attributes[x]=FOREGROUND_ALL;
else{
else
{
gint fg_out, bg_out;
chafa_canvas_get_raw_colors_at (canvas,x,y,&fg_out, &bg_out);
if (canvas_mode == CHAFA_CANVAS_MODE_FGBG_BGFG)
line->attributes[x] = bg_out?FOREGROUND_ALL:COMMON_LVB_REVERSE_VIDEO|FOREGROUND_ALL;
else {
else
{
fg_out=color_lut[fg_out];
bg_out=color_lut[bg_out];
line->attributes[x] = (bg_out<<4)|fg_out;
Expand All @@ -92,7 +94,8 @@ write_image_conhost (const ConhostRow * lines, gsize s)
curpos = bufinfo.dwCursorPosition;
}

for (gsize y=0 ;y<s ; y++){
for (gsize y=0 ;y<s ; y++)
{
const ConhostRow line = lines[y];
WriteConsoleOutputCharacterW (outh, line.str, line.utf16_string_length, curpos,&idc);
WriteConsoleOutputAttribute (outh, line.attributes, line.length, curpos, &idc);
Expand All @@ -108,7 +111,8 @@ write_image_conhost (const ConhostRow * lines, gsize s)
void
destroy_lines (ConhostRow * lines, gsize s)
{
for (gsize i=0; i<s; i++){
for (gsize i=0; i<s; i++)
{
g_free (lines[i].attributes);
g_free (lines[i].str);
}
Expand Down Expand Up @@ -155,6 +159,7 @@ safe_WriteConsoleA (HANDLE chd, const gchar *data, gsize len)

return TRUE;
}
#if 0
gboolean
safe_WriteConsoleW (HANDLE chd, const gunichar2 *data, gsize len)
{
Expand Down Expand Up @@ -190,4 +195,5 @@ safe_WriteConsoleW (HANDLE chd, const gunichar2 *data, gsize len)
}

return TRUE;
}
}
#endif

0 comments on commit 2d4f18a

Please sign in to comment.