Skip to content

Commit 3a682ec

Browse files
committed
term.ui: fix non-ASCII output on Windows consoles
1 parent cd1b78c commit 3a682ec

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

vlib/term/ui/ui.c.v

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,28 @@ pub fn (mut ctx Context) write(s string) {
3434
unsafe { ctx.print_buf.push_many(s.str, s.len) }
3535
}
3636

37+
// write_to_stdout writes a string to stdout.
38+
// On Windows, V's standard output path is used, since it converts UTF-8
39+
// to UTF-16 for consoles, where the code page may not be UTF-8.
40+
@[inline]
41+
fn write_to_stdout(s string) {
42+
$if windows {
43+
print(s)
44+
return
45+
}
46+
C.write(1, s.str, s.len)
47+
}
48+
3749
// flush displays the accumulated print buffer to the screen.
3850
@[inline]
3951
pub fn (mut ctx Context) flush() {
4052
// TODO: Diff the previous frame against this one, and only render things that changed?
4153
if !ctx.enable_su {
42-
C.write(1, ctx.print_buf.data, ctx.print_buf.len)
54+
write_to_stdout(unsafe { tos(ctx.print_buf.data, ctx.print_buf.len) })
4355
} else {
44-
C.write(1, bsu.str, bsu.len)
45-
C.write(1, ctx.print_buf.data, ctx.print_buf.len)
46-
C.write(1, esu.str, esu.len)
56+
write_to_stdout(bsu)
57+
write_to_stdout(unsafe { tos(ctx.print_buf.data, ctx.print_buf.len) })
58+
write_to_stdout(esu)
4759
}
4860
ctx.print_buf.clear()
4961
}

0 commit comments

Comments
 (0)