Question

I'm writing a simple Client-Server protocol in java and am dealing with the server CLI right now. What I want to do is have a > at the beginning of the user input line. The function I have that prints the > character in console is consoleWriteln:

public static void consoleWriteln(String message) {
    System.out.println(message);
    System.out.print(">");
}

With this function, the server starts, but the character is displayed at the beginning of EVERY line.

Server running, listening on port 25565
>Tim has connected
>Boom
Server: Boom
>That's nice
Server: That's nice
>Tim: I agree!
>

The above function is called when anything comes in from a client, or something is broadcast from the server CLI.

How can I go about only having it on the user input line? IE:

Server running, listening on port 25565
Tim has connected
>Boom
Server: Boom
>That's nice
Server: That's nice
Tim: I agree!
>
Was it helpful?

Solution

you can catch the System.out, add a thread that listens to any output, process your task and then do setOut(PrintStream out)

Then you make the server output directly to the PrintStream you used in setOut

Here is some code in one of my projects: In the server you would do serverOut.println()...

    if(OUTPUT_IN_OVERLAY_CONSOLE || OUTPUT_IN_FILE || !OUTPUT_IN_CONSOLE) {
        try {
            JFrame console = new JFrame("Console");
            console.setSize(720,320);
            final JTextArea tarea = new JTextArea();
            JScrollPane scroll = new JScrollPane(tarea);
            console.add(scroll);                

            final OutputStream consoleOut = System.out;
            final OutputStream fileOut  = new FileOutputStream("output.txt");
            final OutputStream o = new OutputStream() {
                StringBuilder sb = new StringBuilder();
                @Override
                public void write(int b) throws IOException {
                    sb.append((char)b);
                    tarea.setText(sb.toString());
                    tarea.setCaretPosition(sb.length());
                    //tarea.requestFocus();
                }
            };

            OutputStream overrideOut = new OutputStream() {

                @Override
                public void write(int b) throws IOException {
                    if(OUTPUT_IN_CONSOLE) consoleOut.write(b);
                    if(OUTPUT_IN_OVERLAY_CONSOLE) o.write(b);
                    if(OUTPUT_IN_FILE)  fileOut.write(b);
                }
            };
            System.setOut(new PrintStream(overrideOut));

            if(OUTPUT_IN_OVERLAY_CONSOLE) {
                console.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                console.setVisible(true);
            } 
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

Instead of doing System.setOut(new PrintStream(overrideOut));, create a class that extends PrintStream and override the println(String str) method to always add a ">" at the beginning

OTHER TIPS

Depending on which console you use, you may be able to simulate the behavior you want by pre-pending your server lines with the backspace character \b. Here is a test program

public class Prompt
{
   public static void main(String [] args)
   {
      System.out.print(">");
      System.out.println("line1");
      System.out.print(">");
      System.out.println("line2");
      System.out.print(">");
      System.out.println("\bline3");
      System.out.print(">");
      System.out.println("");
   }
}

When I run this program in my bash shell I get the following output:

>line1
>line2
line3
>

Note that the program did print out the prompt before line3 however the backspace character ate the prompt character by moving the cursor back one character and overwriting the character.

In your case, when you have something broadcast from the server just prepend \b to the message before calling consoleWriteln.

This does not always work however. It definitely does not work in my Intellij console window - which appears to just ignore the backspace character. So the success of this approach depends on which console you use and how your console handles backspace.

It should work on Mac, Linux and Windows if you use the system supplied console program (bash or cmd.exe) to run your program.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top