mordant icon indicating copy to clipboard operation
mordant copied to clipboard

Table on Windows displays `?`

Open Jason5Lee opened this issue 1 year ago • 3 comments

Code:

Terminal()
    .println(table {
        header { row("Column 1", "Column 2") }
        body { row("1", "2") }
    })

Result: image

Jason5Lee avatar Jan 11 '25 10:01 Jason5Lee

This happens when your terminal isn't rendering unicode characters. Make sure your file encoding is utf-8 and your terminal is also utf-8. You can also use ascii-only characters with borderType = BorderType.ASCII 1 2

I'm not sure there's anything mordant can do about that.

ajalt avatar Jan 13 '25 16:01 ajalt

I managed to fix this in my app by setting the console output code page to 65001, using JNA.

https://github.com/juraj-hrivnak/Pakku/blob/main/src/commonMain/kotlin/teksturepako/pakku/cli/FixSystemOutEncoding.kt#L24-L28

Maybe Mordant could optionally do this?

juraj-hrivnak avatar Mar 10 '25 12:03 juraj-hrivnak

FTR, in Amper we call this function in our RootCommand:

    /**
     * Some Windows encoding used by default doesn't support symbols used in `show dependencies` output
     * Updating it to UTF-8 solves the issue.
     *
     * See https://github.com/ajalt/mordant/issues/249 for details.
     */
    private fun fixSystemOutEncodingOnWindows() {
        if ("win" !in System.getProperty("os.name").lowercase()) return
        if (System.out.charset() == Charsets.UTF_8) return

        // Set console code page to 65001 = UTF-8
        val success = Kernel32.INSTANCE.SetConsoleOutputCP(65001)
        if (success) {
            // Replace System.out and System.err with PrintStreams using UTF-8
            System.setOut(PrintStream(System.out, true, Charsets.UTF_8))
            System.setErr(PrintStream(System.err, true, Charsets.UTF_8))
        } else {
            terminal.warning("Failed to set UTF-8 as console output encoding: ${Kernel32Util.getLastErrorMessage()}")
        }
    }

joffrey-bion avatar Oct 08 '25 12:10 joffrey-bion