简体   繁体   English

C printf:在打印文本上方插入行

[英]C printf: insert line above printed text

I want to print with printf above printed lines WITHOUT overwriting old ones.我想在打印行上方使用 printf 打印而不覆盖旧行。 The code below prints下面的代码打印

0
1
Hallo
A0
A1
A2
A3
A4

I want the output to be我希望输出是

0
1
Hallo
A0
A1
A2
A3
A4
2
3
4

Is there a possibilty to do this with escape codes (like line up)?有没有可能用转义码(比如排队)来做到这一点?

#include "stdio.h" 

#define ANSI_LINE_UP                "\033[3A"


int main() {
    for (int i = 0; i < 5; i++) {
        printf("%d\n", i);
    }

    printf(ANSI_LINE_UP "Hallo\n");
    for (int i = 0; i < 5; i++) {
        printf("A%d\n", i);
    }


    return 0;
}

I use Kubuntu 18.04, if this is relevant.如果相关,我使用 Kubuntu 18.04。

To make it a bit clearer what I want:为了更清楚我想要什么:

I want to have the output of some printf at the end, so it looks like this:我想在最后得到一些 printf 的输出,所以它看起来像这样:

printf(...) printf(...) printf(...) printf(...)

specialprintf(,,,) specialprintf(,,,) specialprintf(,,,) specialprintf(,,,)

Note: The order of all prints might be different while execution.注意:执行时所有打印的顺序可能不同。 I just need the normal ones first, then the special ones.我只需要先正常的,然后是特殊的。 If the special ones are in a different order, it is still fine.如果特殊的顺序不同,它仍然可以。

This is part of an embedded project.这是嵌入式项目的一部分。 @Arkku solution is good, but I don't quite get it working as I want. @Arkku 解决方案很好,但我并没有完全按照我的意愿工作。

Obviously this depends on what your terminal supports, but in case it supports the \\033[<count>L insertion, it should work as follows:显然这取决于你的终端支持什么,但如果它支持\\033[<count>L插入,它应该如下工作:

int main() {
    const int up_lines = 3;
    const int new_lines = 6;

    for (int i = 0; i < 5; ++i) {
        printf("%d\n", i);
    }

    for (int i = 0; i < new_lines; ++i) {
        // make space for existing lines to move down
        putchar('\n');
    }
    // move up and insert the new (empty lines)
    printf("\033[%dA\033[%dL", up_lines + new_lines, new_lines);

    for (int i = 0; i < new_lines; ++i) {
        printf("A%d\n", i);
    }

    // move back down below original lines
    printf("\033[%dB", up_lines);

    return 0;
}

Basically you first make room for the original lines to move down by outputting a number of newlines equal or greater to the number of lines you mean to insert (this step becomes relevant when the terminal position is so near the bottom that the old lines would move off screen).基本上,您首先通过输出等于或大于您要插入的行数的换行符来为原始行向下移动留出空间(当终端位置太靠近底部以至于旧行会移动时,此步骤变得相关屏幕外)。 Then you move up to the insertion position (which has now moved by the number of newlines you printed) and insert the number of lines with \\033[<count>L .然后向上移动到插入位置(现在已移动了您打印的换行符数)并使用\\033[<count>L插入行\\033[<count>L

edit: Obviously you can repeat this as many times as you like, ie, you don't have to know in advance how many lines you will insert and where, as long as you move back down to the last line after every batch of inserts.编辑:显然,您可以根据需要多次重复此操作,即,您不必事先知道要插入多少行以及插入位置,只要在每批插入后返回到最后一行即可. For example, you could insert one line at a time by always printing just one newline at the bottom, then moving up to the insertion position (remembering to account for the one line you just added in between), inserting the line there, and moving back down to the bottom.例如,您可以通过始终在底部打印一个换行符来一次插入一行,然后向上移动到插入位置(记住考虑您刚刚添加的一行),在那里插入该行,然后移动回到底部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM