* @param pos the current screen cursor position
     */
    private void clearAhead(int num, final int pos) throws IOException {
        if (num == 0) return;

        int width = terminal.getWidth();
        // Use kill line
        if (terminal.getStringCapability("clr_eol") != null) {
            int cur = pos;
            int c0 = cur % width;
            // Erase end of current line
            int nb = Math.min(num, width - c0);
            tputs("clr_eol");
            num -= nb;
            // Loop
            while (num > 0) {
                // Move to beginning of next line
                int prev = cur;
                cur = cur - cur % width + width;
                moveCursorFromTo(prev, cur);
                // Erase
                nb = Math.min(num, width);
                tputs("clr_eol");
                num -= nb;
            }
            moveCursorFromTo(cur, pos);
        }
        // Terminal does not wrap on the right margin
        else if (!terminal.getBooleanCapability("auto_right_margin")) {
            int cur = pos;
            int c0 = cur % width;
            // Erase end of current line
            int nb = Math.min(num, width - c0);
            rawPrint(' ', nb);
            num -= nb;
            cur += nb;
            // Loop
            while (num > 0) {
                // Move to beginning of next line
                moveCursorFromTo(cur, ++cur);
                // Erase
                nb = Math.min(num, width);
                rawPrint(' ', nb);
                num -= nb;
                cur += nb;
            }
            moveCursorFromTo(cur, pos);
        }
        // Simple erasure
        else {
            rawPrint(' ', num);
            moveCursorFromTo(pos + num, pos);
        }
    }