*/
    private void drawBuffer(final int clear) throws IOException {
        // debug ("drawBuffer: " + clear);
        int nbChars = buf.length() - buf.cursor;
        if (buf.cursor != buf.length() || clear != 0) {
            if (mask != null) {
                if (mask != NULL_MASK) {
                    rawPrint(mask, nbChars);
                } else {
                    nbChars = 0;
                }
            } else {
                print(buf.buffer, buf.cursor, buf.length());
            }
        }
        int cursorPos = promptLen + wcwidth(buf.buffer, 0, buf.length(), promptLen);
        if (terminal.hasWeirdWrap() && !cursorOk) {
            int width = terminal.getWidth();
            // best guess on whether the cursor is in that weird location...
            // Need to do this without calling ansi cursor location methods
            // otherwise it breaks paste of wrapped lines in xterm.
            if (cursorPos > 0 && (cursorPos % width == 0)) {
                // the following workaround is reverse-engineered from looking
                // at what bash sent to the terminal in the same situation
                rawPrint(' '); // move cursor to next line by printing dummy space
                tputs("carriage_return"); // CR / not newline.
            }
            cursorOk = true;
        }
        clearAhead(clear, cursorPos);
        back(nbChars);
    }