beryx / text-io

A library for creating interactive console applications in Java
http://text-io.beryx.org/
Apache License 2.0
343 stars 45 forks source link

refresh line #21

Closed mokun closed 5 years ago

mokun commented 6 years ago

Is there a way to go beyond changing the value on the whole screen or the value on, say, the 5th line from the bottom of the screen ?

I can see in the Weather that it calls terminal.resetLine() and terminal.moveToLineStart() to refresh the value of the very last line on the screen.

In TextTerminal, there is :

    @Override
    public boolean resetLine() {
        return resetToOffset(startLineOffset);
    }

    @Override
    public boolean moveToLineStart() {
        overwriteOffset = startLineOffset;
        return true;
    }

We can literally build a game out of the text terminal if we can give the ability to change the value of a text with, say, overwrite(x,y), where x being the x-th position from the left, y being the y-th line from the bottom.

siordache commented 6 years ago

No, this is beyond the scope of Text-IO. You could in principle add this capability to the SwingTextTerminal, but it's not trivial.

mokun commented 5 years ago

oh okay. Now I'll be less ambitious :D

But say, I'd like to refresh a bunch of numbers to the terminal and unfortunately, it needs to occupy 3 lines, instead of just 1 line that your Weather class can do.

Is there any quick way to delete the last 3 lines from the bottom of the screen and then redraw them over ?

I'm looking at the method resetLine() in SwingTextTerminal. It calls up resetToOffset(startLineOffset).

    @Override
    public boolean resetLine() {
        return resetToOffset(startLineOffset);
    }

    public boolean resetToOffset(int offset) {
        if(offset < 0) return false;
        display();
        synchronized (editLock) {
            boolean result = true;
            int len = document.getLength() - offset;
            if(len > 0) {
                int oldStartReadLen = startReadLen;
                if(startReadLen > offset) {
                    startReadLen = offset;
                }
                try {
                    document.remove(offset, len);
                } catch (BadLocationException e) {
                    logger.error("Cannot reset to offset " + offset, e);
                    startReadLen = oldStartReadLen;
                    result = false;
                }
                textPane.setCaretPosition(document.getLength());
            }
            return result;
        }
    }
siordache commented 5 years ago

You need to set a bookmark at the beginning of the block you want to refresh. Then, you call resetToBookmark when you want to delete all lines after this bookmark. The Weather application also uses this technique.

mokun commented 5 years ago

okay. Closing this.