Mihai Nita
unread,Jan 3, 2012, 6:50:59 PM1/3/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to jansi-dev
Here it would help to add some screen-shots, but I will try
without :-)
I have tried comparing the results of several functions:
System.out.println(message);
WinConsole.printString(message);
org.fusesource.jansi.internal.WindowsSupport.writeConsole(message);
AnsiConsole.out.println(message);
WinConsole.printString is my own jni implementation calling
WriteConsole, and has identical results to
org.fusesource.jansi.internal.WindowsSupport.writeConsole.
message contains text in a mixture of scripts (added at the end of
this message).
Results:
WinConsole.printString and
org.fusesource.jansi.internal.WindowsSupport.writeConsole behave the
same (very well, everything supported by the console font works)
System.out.println and AnsiConsole.out.println can't handle utf-8 by
default, and are both affected by chcp.
Only if one calls chcp 1252 we get the right result, and only for
Latin 1.
With System.out.println I can use System.setOut to set the output
charset to utf-8, and it it also calls chcp 65001, the result is good
(same as the the one from WriteConsole).
So one can display Unicode with all methods, except
AnsiConsole.out.println.
Redirecting to a file also results in correct utf-8 encoding.
So it would be nice to change AnsiConsole.out.println to have
something similar to System.setOut
And maybe change AnsiConsole.out.println to use WriteConsole when the
console is not redirected
(so that the result on the console does not depend on the chcp
settings).
Or configurable? (something like AnsiConsole.systemInstall(), set and
forget?)
Not sure what would be the best public API to expose, depend on your
preferred style of API design :-)
Thanks a lot (and let me know if I you need some clarification/code/
whatever),
Mihai
====================================
Some code to test things
You can try with/without SetOutputUtf8(), with/without
AnsiConsole.systemInstall(), with
various chcp values. I have tried 437 (default), 1252 (Latin 1), 1251
(Cyrillic), 65001 (utf-8).
====================================
import org.fusesource.jansi.*;
import static org.fusesource.jansi.Ansi.ansi;
import static org.fusesource.jansi.Ansi.Color.*;
import static org.fusesource.jansi.Ansi.Attribute.*;
// import tzu.utils.WinConsole;
// UTF-8 output
import java.io.File;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
public class OutputUnicode {
private static void SetOutputUtf8() {
try {
System.setOut(new PrintStream(System.out, true, "UTF-8"));
}
catch (UnsupportedEncodingException e) {
System.out.println("UnsupportedEncodingException: " +
e.getMessage());
}
}
public static void main(String[] args)
{
String message =
"Latin 1 : \u00BF
\u00C7\u00E3\u00E5\u00E6\u00DF?" +
" "
+ "Arabic : \u0627\u0644\u0639\u0631\u0628\u064A
\u0629" + "\r\n"
+ "Latin 2 : \u0218\u021A\u0102\u00EE
\u00E2" + " "
+ "Hebrew :
\u05E2\u05B4\u05D1\u05B0\u05E8\u05B4\u05D9\u05EA" +
"\r\n"
+ "Greek : \u0395\u03BB\u03BB\u03B7\u03BD\u03B9\u03BA
\u03AC" + " "
+ "Korean : \uD55C\uAD6D
\uC5B4" + "\r\n"
+ "Cyrillic : \u0440\u0443\u0441\u0441\u043A
\u0438\u0439" + " "
+ "Japanese : \u65E5\u672C
\u8A9E" + "\r\n"
+ "Armenian :
\u0540\u0561\u0575\u0565\u0580\u0565\u0576" +
" "
+ "Thai :
\u0E44\u0E17\u0E22" +
"\r\n"
+ "Georgian : \u10E5\u10D0\u10E0\u10D7\u10E3\u10DA
\u10D8\u0020\u10D4\u10DC\u10D0" + " "
+ "Hindi : \u092E\u093E\u0928\u0915\u0020\u0939\u093F
\u0928\u094D\u0926\u0940" + "\r\n"
+ "Amharic : \u12A0\u121B\u122D
\u129B" + "
"
+ "Tamil : \u0BA4\u0BAE\u0BBF
\u0BB4\u0BCD" + "\r\n"
+ "Vietnamese: Ti\u1EBFng Vi
\u1EC7t" + "
"
+ "Bengali : \u09AC\u09BE
\u0982\u09B2\u09BE" + "\r\n"
;
SetOutputUtf8();
for (int i = 0; i < args.length; ++i) {
if ("s".equals(args[i])) {
AnsiConsole.out.println(ansi().fg(RED).a(INTENSITY_BOLD).toString());
AnsiConsole.out.println("== System.out.println ==");
System.out.println(message);
}
// else if ("w".equals(args[i])) {
//
AnsiConsole.out.println(ansi().fg(YELLOW).a(INTENSITY_BOLD).toString());
// AnsiConsole.out.println("== My WinConsole.printString ==");
// WinConsole.printString(message);
// }
else if ("j".equals(args[i])) {
AnsiConsole.out.println(ansi().fg(CYAN).a(INTENSITY_BOLD).toString());
AnsiConsole.out.println("== Jansi
org.fusesource.jansi.internal.WindowsSupport.writeConsole ==");
org.fusesource.jansi.internal.WindowsSupport.writeConsole(message);
}
else if ("a".equals(args[i])) {
AnsiConsole.out.println(ansi().fg(GREEN).a(INTENSITY_BOLD).toString());
AnsiConsole.out.println("== AnsiConsole.out.println ==");
AnsiConsole.out.println(message);
}
}
AnsiConsole.out.println(ansi().reset().toString());
}
// static {
// AnsiConsole.systemInstall();
// }
}
====================================
The message:
Latin 1 : ¿Çãåæß?
Latin 2 : ȘȚĂîâ
Greek : Ελληνικά
Cyrillic : русский
Armenian : Հայերեն
Georgian : ქართული ენა
Amharic : አማርኛ
Vietnamese: Tiếng Việt
Arabic : العربية
Hebrew : עִבְרִית
Korean : 한국어
Japanese : 日本語
Thai : ไทย
Hindi : मानक हिन्दी
Tamil : தமிழ்
Bengali : বাংলা