src/cforth/linenoise.c
static void refreshMultiLine(struct linenoiseState *l) {
    char seq[64];
    int plen = strlen(l->prompt);
    int rows = (plen+l->len+l->cols-1)/l->cols; /* rows used by current buf. */
    int rpos = (plen+l->oldpos+l->cols)/l->cols; /* cursor relative row. */
    int rpos2; /* rpos after refresh. */
    int old_rows = l->maxrows;
    int j;

    /* Update maxrows if needed. */
    if (rows > (int)l->maxrows) l->maxrows = rows;

#ifdef LN_DEBUG
    FILE *fp = fopen("/tmp/debug.txt","a");
    fprintf(fp,"[%d %d %d] p: %d, rows: %d, rpos: %d, max: %d, oldmax: %d",
        (int)l->len,(int)l->pos,(int)l->oldpos,plen,rows,rpos,(int)l->maxrows,old_rows);
#endif

    /* First step: clear all the lines used before. To do so start by
     * going to the last row. */
    if (old_rows-rpos > 0) {
#ifdef LN_DEBUG
        fprintf(fp,", go down %d", old_rows-rpos);
#endif
        snprintf(seq,64,"\x1b[%dB", old_rows-rpos);
        write_console(seq,strlen(seq));
    }

    /* Now for every row clear it, go up. */
    for (j = 0; j < old_rows-1; j++) {
#ifdef LN_DEBUG
        fprintf(fp,", clear+up");
#endif
        snprintf(seq,64,"\x1b[0G\x1b[0K\x1b[1A");
        write_console(seq,strlen(seq));
    }

    /* Clean the top line. */
#ifdef LN_DEBUG
    fprintf(fp,", clear");
#endif
    snprintf(seq,64,"\x1b[0G\x1b[0K");
    write_console(seq,strlen(seq));
    
    /* Write the prompt and the current buffer content */
    write_console(l->prompt,strlen(l->prompt));
    write_console(l->buf,l->len);

    /* If we are at the very end of the screen with our prompt, we need to
     * emit a newline and move the prompt to the first column. */
    if (l->pos &&
        l->pos == l->len &&
        (l->pos+plen) % l->cols == 0)
    {
#ifdef LN_DEBUG
        fprintf(fp,", <newline>");
#endif
        write_console("\n",1);
        snprintf(seq,64,"\x1b[0G");
        write_console(seq,strlen(seq));
        rows++;
        if (rows > (int)l->maxrows) l->maxrows = rows;
    }

    /* Move cursor to right position. */
    rpos2 = (plen+l->pos+l->cols)/l->cols; /* current cursor relative row. */
#ifdef LN_DEBUG
    fprintf(fp,", rpos2 %d", rpos2);
#endif
    /* Go up till we reach the expected positon. */
    if (rows-rpos2 > 0) {
#ifdef LN_DEBUG
        fprintf(fp,", go-up %d", rows-rpos2);
#endif
        snprintf(seq,64,"\x1b[%dA", rows-rpos2);
        write_console(seq,strlen(seq));
    }
    /* Set column. */
#ifdef LN_DEBUG
    fprintf(fp,", set col %d", 1+((plen+(int)l->pos) % (int)l->cols));
#endif
    snprintf(seq,64,"\x1b[%dG", 1+((plen+(int)l->pos) % (int)l->cols));
    write_console(seq,strlen(seq));

    l->oldpos = l->pos;

#ifdef LN_DEBUG
    fprintf(fp,"\n");
    fclose(fp);
#endif
}
/* Multi line low level line refresh.
 *
 * Rewrite the currently edited line accordingly to the buffer content,
 * cursor position, and number of columns of the terminal. */
Multi line low level line refresh.
Rewrite the currently edited line accordingly to the buffer content,
cursor position, and number of columns of the terminal.
Multi line low level line refresh . Rewrite the currently edited line accordingly to the buffer content cursor position and number of columns of the terminal .
static void refreshMultiLine(struct linenoiseState *l) {
    char seq[64];
    int plen = strlen(l->prompt);
    int rows = (plen+l->len+l->cols-1)/l->cols; 
    int rpos = (plen+l->oldpos+l->cols)/l->cols; 
    int rpos2; 
    int old_rows = l->maxrows;
    int j;
    if (rows > (int)l->maxrows) l->maxrows = rows;
#ifdef LN_DEBUG
    FILE *fp = fopen("/tmp/debug.txt","a");
    fprintf(fp,"[%d %d %d] p: %d, rows: %d, rpos: %d, max: %d, oldmax: %d",
        (int)l->len,(int)l->pos,(int)l->oldpos,plen,rows,rpos,(int)l->maxrows,old_rows);
#endif
    if (old_rows-rpos > 0) {
#ifdef LN_DEBUG
        fprintf(fp,", go down %d", old_rows-rpos);
#endif
        snprintf(seq,64,"\x1b[%dB", old_rows-rpos);
        write_console(seq,strlen(seq));
    }
    for (j = 0; j < old_rows-1; j++) {
#ifdef LN_DEBUG
        fprintf(fp,", clear+up");
#endif
        snprintf(seq,64,"\x1b[0G\x1b[0K\x1b[1A");
        write_console(seq,strlen(seq));
    }
#ifdef LN_DEBUG
    fprintf(fp,", clear");
#endif
    snprintf(seq,64,"\x1b[0G\x1b[0K");
    write_console(seq,strlen(seq));
    write_console(l->prompt,strlen(l->prompt));
    write_console(l->buf,l->len);
    if (l->pos &&
        l->pos == l->len &&
        (l->pos+plen) % l->cols == 0)
    {
#ifdef LN_DEBUG
        fprintf(fp,", <newline>");
#endif
        write_console("\n",1);
        snprintf(seq,64,"\x1b[0G");
        write_console(seq,strlen(seq));
        rows++;
        if (rows > (int)l->maxrows) l->maxrows = rows;
    }
    rpos2 = (plen+l->pos+l->cols)/l->cols; 
#ifdef LN_DEBUG
    fprintf(fp,", rpos2 %d", rpos2);
#endif
    if (rows-rpos2 > 0) {
#ifdef LN_DEBUG
        fprintf(fp,", go-up %d", rows-rpos2);
#endif
        snprintf(seq,64,"\x1b[%dA", rows-rpos2);
        write_console(seq,strlen(seq));
    }
#ifdef LN_DEBUG
    fprintf(fp,", set col %d", 1+((plen+(int)l->pos) % (int)l->cols));
#endif
    snprintf(seq,64,"\x1b[%dG", 1+((plen+(int)l->pos) % (int)l->cols));
    write_console(seq,strlen(seq));
    l->oldpos = l->pos;
#ifdef LN_DEBUG
    fprintf(fp,"\n");
    fclose(fp);
#endif
}
static void refreshMultiLine ( struct linenoiseState * l ) { char seq [ 64 ] ; int plen = strlen ( l -> prompt ) ; int rows = ( plen + l -> len + l -> cols - 1 ) / l -> cols ; int rpos = ( plen + l -> oldpos + l -> cols ) / l -> cols ; int rpos2 ; int old_rows = l -> maxrows ; int j ; if ( rows > ( int ) l -> maxrows ) l -> maxrows = rows ; #ifdef LN_DEBUG FILE * fp = fopen ( " " , " " ) ; fprintf ( fp , " " , ( int ) l -> len , ( int ) l -> pos , ( int ) l -> oldpos , plen , rows , rpos , ( int ) l -> maxrows , old_rows ) ; #endif if ( old_rows - rpos > 0 ) { #ifdef LN_DEBUG fprintf ( fp , " " , old_rows - rpos ) ; #endif snprintf ( seq , 64 , " \x1b " , old_rows - rpos ) ; write_console ( seq , strlen ( seq ) ) ; } for ( j = 0 ; j < old_rows - 1 ; j ++ ) { #ifdef LN_DEBUG fprintf ( fp , " " ) ; #endif snprintf ( seq , 64 , " \x1b \x1b \x1b " ) ; write_console ( seq , strlen ( seq ) ) ; } #ifdef LN_DEBUG fprintf ( fp , " " ) ; #endif snprintf ( seq , 64 , " \x1b \x1b " ) ; write_console ( seq , strlen ( seq ) ) ; write_console ( l -> prompt , strlen ( l -> prompt ) ) ; write_console ( l -> buf , l -> len ) ; if ( l -> pos && l -> pos == l -> len && ( l -> pos + plen ) % l -> cols == 0 ) { #ifdef LN_DEBUG fprintf ( fp , " " ) ; #endif write_console ( " \n " , 1 ) ; snprintf ( seq , 64 , " \x1b " ) ; write_console ( seq , strlen ( seq ) ) ; rows ++ ; if ( rows > ( int ) l -> maxrows ) l -> maxrows = rows ; } rpos2 = ( plen + l -> pos + l -> cols ) / l -> cols ; #ifdef LN_DEBUG fprintf ( fp , " " , rpos2 ) ; #endif if ( rows - rpos2 > 0 ) { #ifdef LN_DEBUG fprintf ( fp , " " , rows - rpos2 ) ; #endif snprintf ( seq , 64 , " \x1b " , rows - rpos2 ) ; write_console ( seq , strlen ( seq ) ) ; } #ifdef LN_DEBUG fprintf ( fp , " " , 1 + ( ( plen + ( int ) l -> pos ) % ( int ) l -> cols ) ) ; #endif snprintf ( seq , 64 , " \x1b " , 1 + ( ( plen + ( int ) l -> pos ) % ( int ) l -> cols ) ) ; write_console ( seq , strlen ( seq ) ) ; l -> oldpos = l -> pos ; #ifdef LN_DEBUG fprintf ( fp , " \n " ) ; fclose ( fp ) ; #endif }
Multi line low level line refresh.
Multi line low level line refresh .
/* rows used by current buf. */ /* cursor relative row. */ /* rpos after refresh. */ /* Update maxrows if needed. */ /* First step: clear all the lines used before. To do so start by
     * going to the last row. */ /* Now for every row clear it, go up. */ /* Clean the top line. */ /* Write the prompt and the current buffer content */ /* If we are at the very end of the screen with our prompt, we need to
     * emit a newline and move the prompt to the first column. */ /* Move cursor to right position. */ /* current cursor relative row. */ /* Go up till we reach the expected positon. */ /* Set column. */