Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prompt line rendering incorrectly on asynchronous writes. #227

Open
ghost opened this issue Dec 26, 2015 · 0 comments
Open

Prompt line rendering incorrectly on asynchronous writes. #227

ghost opened this issue Dec 26, 2015 · 0 comments

Comments

@ghost
Copy link

ghost commented Dec 26, 2015

I am trying to write to a console and read from it asynchronously using jline.console.ConsoleReader#print and jline.console.ConsoleReader#readLine methods.

Every write to the console is handlet by following instructions:

private void stashLine() {
    stashed = reader.getCursorBuffer().copy();
    try {
        reader.getOutput().write("\u001b[1G\u001b[K");
        reader.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}


private void unstashLine() {
    try {
        reader.resetPromptLine(reader.getPrompt(), stashed.toString(), stashed.cursor);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@Override
protected void subAppend(E event) {
    if (this.isStarted()) {
        try {
            if (event instanceof DeferredProcessingAware) {
                ((DeferredProcessingAware) event).prepareForDeferredProcessing();
            }

            this.lock.lock();

            try {
                this.writeOut(event);
                if (reader != null) {
                    ByteArrayOutputStream stream = ((ByteArrayOutputStream) getOutputStream());
                    stream.flush();
                    stashLine();
                    reader.print(ConsoleReader.RESET_LINE + stream.toString(encoding) + Ansi.ansi().reset().toString());
                    unstashLine();
                    reader.drawLine();
                    reader.flush();
                    stream.reset();
                }
            } finally {
                this.lock.unlock();
            }
        } catch (IOException var6) {
            this.started = false;
            this.addStatus(new ErrorStatus("IO failure in appender", this, var6));
        }
    }
}

while every read from a console is handled in separate thread using:

public void start() {

    String command;

    try {
        while (Clockwork.getServer().isRunning() && (command = reader.readLine("> ")) != null) {
            logger.info("User input: {}", command);
        }
    } catch (IOException e) {
        logger.error("An exception occurred while processing console input.", e);
    }
}

I would like to achieve such begaviour that the prompt line is always located at the most bottom line of the console. This behaviour works as long as only one thread reads and writes to the console, however, whenever one thread is reading and another one tries to write to the console, the prompt line goes blank, even though I'm trying to reset it after writing (see methods like 'stashLine' and 'unstashLine' in provided source code).

I'm using Java 8 on Windows 7 alongside with JLine version 2.13. I've also tried JLine versions 2.7 and 2.12 where the same issue was reproduced.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants