Displaying Midnight Commander screen in JTextPane

試著忘記壹切 提交于 2019-12-10 12:14:16

问题


I've tried to connect by SSH to a server and get an output for my commands. Everything works fine if I put out into System.out. Else if I want to put it into JTextPane, it puts it, but MC is unreadable.

This is my code:

JSch jsch = new JSch();

String command = "";
String commandR = "";
host = null;

if (arg.length > 1) {
    host = arg[0];
    command = arg[2];
    commandR = arg[3];
}

String user = host.substring(0, host.indexOf('@'));
host = host.substring(host.indexOf('@') + 1);
Session session = jsch.getSession(user, host, 22);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);

session.setPassword(arg[1]);
if (!session.isConnected()) {
    session.connect();
}

Channel channel = session.openChannel("shell");

String ans = " ";
InputStream in = channel.getInputStream();
OutputStream out = channel.getOutputStream();
//channel.setInputStream(System.in);
if (!channel.isConnected()) {
    channel.connect(10 * 1000);
}
boolean logon = false;
boolean started = false;
boolean brtuser = false;
boolean log = false;
byte[] tmp = new byte[1024];

JFrame j = new JFrame();
JTextPane jj = new JTextPane();
jj.setContentType("text/html");
StyledDocument  doc = (StyledDocument )jj.getDocument();
jj.setPreferredSize(new Dimension(500, 600));
j.setDefaultCloseOperation(2);
j.add(new JScrollPane(jj));
j.pack();
j.setVisible(true);
while (!started) {

    while (in.available() > 0) {
        int i = in.read(tmp, 0, 1024);
        if (i < 0) {
            break;
        }

        // jj.append();

        doc.insertString(doc.getLength(), new String(tmp, 0, i), null);
        ans += new String(tmp, 0, i);
        System.out.print(new String(tmp, 0, i));

Then I write pwd and mc - pwd I get fine, but mc is such as

39m[49m                                                                              
    [37m[40m                                                                                [23;3H[22;1H[39m[49mGNU Midnight Commander 4.6.0                                                    
      [1;1H[30m[46m  Left     File     Command     Options     Right                               
    [37m[44m+[0m[37m[44m<[37m[44m-[0m[37m[44m~[37m[44m---------------------------------[0m[37m[44mv>[37m[44m++[0m[30m[47m<[37m[44m-[0m[30m[47m~[37m[44m---------------------------------[0m[30m[47mv>[37m[44m+
    |[0m[1m[33m[44m       Name      [0m[37m[44m|[0m[1m[33m[44m Size  [0m[37m[44m|[0m[1m[33m[44m   MTime    [0m[37m[44m||[0m[1m[33m[44m       Name      [0m[37m[44m|[0m[1m[33m[44m Size  [0m[37m[44m|[0m[1m[33m[44m   MTime    [0m[37m[44m|
    |[0m[1m[37m[44m/..              [0m[37m[44m|[0m[1m[37m[44mUP--DIR[0m[37m[44m|[0m[1m[37m[44m            [0m[37m[44m||[0m[30m[46m/..              [30m[46m|[0m[30m[46mUP--DIR[30m[46m|[0m[30m[46m            [37m[44m|
    |[0m[1m[37m[44m/.mc             [0m[37m[44m|[0m[1m[37m[44m   1024[0m[37m[44m|[0m[1m[37m[44mJan 29 11:35[0m[37m[44m||[0m[1m[37m[44m/.mc    

Please help me to get it viewable!


回答1:


The "garbage" that you see in the JTextPane are ANSI escape codes. These define colors and other formatting. They used by Midnight Commander (and other applications) to render a GUI-like interface on a text console.

The ANSI escape codes are understood by Terminal/SSH clients like PuTTY. Even by console windows in various OSes (e.g. Windows). That's probably, why it looks good when you print the output to System.out.

But they are not understood by JTextPane, it displays them as they are.

See ANSI colors in Java Swing text fields for "ANSI colored JTextPane subclass".



来源:https://stackoverflow.com/questions/28211577/displaying-midnight-commander-screen-in-jtextpane

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!