Smodics
Smodics

Reputation: 143

Is there any way to automatically break lines in a cmd window?

Me and my friend wrote a simple (but really long) text-based game, which plays in the Windows command prompt. The problem is, that the cmd window does not include any kind of auto-linebreak like it does in Microsoft word, and thus makes the game unplayable. If I wasn't very clear, this is an example on how the game plays now:

You wake up in a dark room. Wherev
er you look, you can't see anythin
g. You try to move, but you can't;
your hands are tied down by the lo
oks of it.

Is there any kind of easy way to fix this? The game is more than 2000 lines long, and thus we'd probably never be able to fix the sentences one-by-one.

Edit: Just to make clear, what I'd like to achieve is the following:

You wake up in a dark room.
Wherever you look, you can't see
anything. You try to move, but
you can't; your hands are tied
down by the looks of it.

Upvotes: 2

Views: 1264

Answers (3)

Florian Schaetz
Florian Schaetz

Reputation: 10662

A very simple example. In your case, you will probably need to store your input until the place for a line break comes in, etc. but I hope you get the basic idea...

public class RedirectPrintStream extends PrintStream {

    private static String NEW_LINE = String.format("%n"); // This creates the system-specific line break (depends on Windows/Linux/etc)

    public RedirectPrintStream(OutputStream out) {
        super(out); 
    }

    @Override
    public void print(String obj) {
        super.print(obj);  // or check here if the obj.length > 80 and add another line break, etc.
        super.print(NEW_LINE);

    }

    public static void main(String[] args) {

        PrintStream old = System.out;
        System.setOut( new RedirectPrintStream(old));

        System.out.print("Hello");
        System.out.print("World"); // will be printed in a new line instead of the same

    }
}

As for the wrapping itself, I would second the already mentioned WordUtils from Apache Commons lang. Should be simple enough.

Upvotes: 2

Marko Topolnik
Marko Topolnik

Reputation: 200206

Commonly, lines of width 72 are used to make text easily readable, so I advise sticking to this and not trying to work out the width of the text terminal. To achieve proper word wrapping your best option is to rely on Apache WordUtils, which contains the wrap method. Alternatively (if you don't want to download another JAR just for this), just copy its implementation into your project:

/*
 * Copyright 2002-2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
public static String wrap(String str, int wrapLength, String newLineStr, boolean wrapLongWords) {
    if (str == null) {
        return null;
    }
    if (newLineStr == null) {
        newLineStr = SystemUtils.LINE_SEPARATOR;
    }
    if (wrapLength < 1) {
        wrapLength = 1;
    }
    int inputLineLength = str.length();
    int offset = 0;
    StringBuilder wrappedLine = new StringBuilder(inputLineLength + 32);

    while ((inputLineLength - offset) > wrapLength) {
        if (str.charAt(offset) == ' ') {
            offset++;
            continue;
        }
        int spaceToWrapAt = str.lastIndexOf(' ', wrapLength + offset);

        if (spaceToWrapAt >= offset) {
            // normal case
            wrappedLine.append(str.substring(offset, spaceToWrapAt));
            wrappedLine.append(newLineStr);
            offset = spaceToWrapAt + 1;

        } else {
            // really long word or URL
            if (wrapLongWords) {
                // wrap really long word one line at a time
                wrappedLine.append(str.substring(offset, wrapLength + offset));
                wrappedLine.append(newLineStr);
                offset += wrapLength;
            } else {
                // do not wrap really long word, just extend beyond limit
                spaceToWrapAt = str.indexOf(' ', wrapLength + offset);
                if (spaceToWrapAt >= 0) {
                    wrappedLine.append(str.substring(offset, spaceToWrapAt));
                    wrappedLine.append(newLineStr);
                    offset = spaceToWrapAt + 1;
                } else {
                    wrappedLine.append(str.substring(offset));
                    offset = inputLineLength;
                }
            }
        }
    }

    // Whatever is left in line is short enough to just pass through
    wrappedLine.append(str.substring(offset));

    return wrappedLine.toString();
}

Upvotes: 3

piezol
piezol

Reputation: 963

This is not an actual java issue, as tags would suggest, but if you're using System.out as your output, You can:

  1. Change your windows cmd width to fit your needs
  2. According to this link, get your cmd width dynamically. Then, create an object of a class, which would be your output handler. Get the width and save it as a field. Then whenever you want an output, call its method, which would automatically put line breaks to your strings and then call redirect to system.out.

Upvotes: 0

Related Questions