Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

How to use ANSI to return to a previous position out of the current window? (C++)

I have found some functions that use ANSI escape codes to handle the Command Line. The purpose is to print a lot of contents. Then I want to return where I started. However, the implementation below just jumps to the beginning of the window. I don't mind a Unix solution. I would prefer though a cross - platform solution.

My implementation:


#include "stdio.h"
#include "windows.h"

/**
 * Moves CLI cursor Up.
 * 
 * @param[in] positions number of rows to move cursor up
 */
void moveUp(int positions) 
{
    printf("x1b[%dA", positions);
}

/**
 * Moves CLI cursor Down.
 * 
 * @param[in] positions number of rows to move cursor down
 */
void moveDown(int positions) 
{
    printf("x1b[%dB", positions);
}

/**
 * Scrolls CLI window Up.
 * 
 * @param[in] positions number of rows to scroll up
 */
void scrollUp(int positions)
{
    printf("x1b[%dS", positions);
}

/**
 * Scrolls CLI window Down.
 * 
 * @param[in] positions number of rows to scroll down
 */
void scrollDown(int positions)
{
    printf("x1b[%dT", positions);
}

/**
 * Places CLI cursor at defined position in the CLI.
 * 
 * @param[in] x the row to place the cursor
 * @param[in] y the column to place the cursor
 */
void gotoxy(int x, int y)
{
    printf("x1b[%d;%df", x, y);
}

int main (void)
{
    for (int i = 1; i < 100; i += 1)
    {
        gotoxy(i,0);
        printf("%d
", i);
    }
    gotoxy(0,0);
    printf("Hello World
");
}

Any help is appreciated!

question from:https://stackoverflow.com/questions/65937599/how-to-use-ansi-to-return-to-a-previous-position-out-of-the-current-window-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...