状态功能栏 - C.

时间:2017-04-02 18:49:39

标签: c function progress-bar statusbar

我有这个功能:

void choiceTwo(void){

    system("clear");
    printf("================ RUN ALL FUNCTION ================\n\n");

    printf("\nTest case 'create/destroy'... ");
    fflush(stdout);
    test_create_destroy();
    printf("OK\n");

    printf("Test case 'add/remove edge'... ");
    fflush(stdout);
    test_add_remove_edge();
    printf("OK\n");

    printf("Test case 'print graph'... ");
    fflush(stdout);
    test_print_graph();
    printf("OK\n");

    printf("Test case 'null'... ");
    fflush(stdout);
    test_null();
    printf("OK\n");

    printf("\nPress enter to continue ");
    getchar();
    getchar();
    main();
}

如何使用图片中的进度条(百分比)打印此功能的状态?

Status Bar

我尝试使用python执行此操作,但不知道如何完全控制功能状态

2 个答案:

答案 0 :(得分:1)

精度字段%.precision可让您打印特定数量的字符。星号将允许使用变量。 %.*s将从done数组中打印x个字符,然后从50 - done数组中打印dash个字符。
使用回车符\r,应该在循环进行时将循环打印在同一行上。

#include <stdio.h>

int main()
{
    int loop = 1;
    int done = 0;
    int end = 200000;
    char dash[51] = "--------------------------------------------------";
    char x[51] =    "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
    float percent = 0.0f;
    while ( loop <= end) {
        percent = ( (float)loop * 100) / end;
        done = percent / 2.0f;
        printf ( "Progress: [%.*s%.*s]%6.1f%% Complete\r", done, x, 50 - done, dash, percent);
        loop++;
    }
    printf ( "\n");
    return 0;
}

如果您的终端支持转义码,则可以使用此码来获得图像中指示的颜色。

#include <stdio.h>

int main()
{
    int loop = 1;
    int done = 0;
    int end = 200000;
    char dash[51]  = "--------------------------------------------------";
    char blank[51] = "                                                  ";
    float percent = 0.0f;
    printf ( "\033[?25l");//hide cursor
    printf ( "\033[38;5;10m");//green text
    printf ( "\033[48;5;0m");//on black
    while ( loop <= end) {
        percent = ( (float)loop * 100) / end;
        done = percent / 2.0f;
        printf ( "Progress: [\033[38;5;0m\033[48;5;10m");//black text on green
        printf ( "%.*s\033[38;5;10m\033[48;5;0m%.*s]%6.1f%% Complete\r"//back to green text on black
        , done, blank, 50 - done, dash, percent);
        loop++;
    }
    printf ( "\033[0m");//reset color
    printf ( "\033[?25h");//show cursor
    printf ( "\n");
    return 0;
}

这包含了函数的进度。使用您想要显示条形图的行(0到23),到目前为止完成的项目和总项目来调用此函数。

#include <stdio.h>

void working ( int showat, int done, int total) {
    char dash[51]  = "--------------------------------------------------";
    char blank[51] = "                                                  ";
    int portion = 0;
    float percent = 0.0f;

    printf ( "\033[?25l");//hide cursor
    printf ( "\033[38;5;10m");//green text
    printf ( "\033[48;5;0m");//on black
    printf ( "\033[%d;H", showat);//move curson to line showat
    percent = ( (float)done * 100) / total;
    portion = percent / 2.0f;
    printf ( "Progress: [\033[38;5;0m\033[48;5;10m");//black text on green
    printf ( "%.*s\033[38;5;10m\033[48;5;0m%.*s]%6.1f%% Complete\r"//back to green text on black
    , portion, blank, 50 - portion, dash, percent);

    printf ( "\033[0m");//reset color
    printf ( "\033[?25h");//show cursor
}

int main()
{
    int loop = 1;
    int end  = 20000;
    while ( loop <= end) {
        working ( 23, loop, end);
        loop++;
    }
    printf ( "\n");
    return 0;
}

答案 1 :(得分:0)

试试这段代码

#include <stdio.h>
#include <unistd.h>

void working ( int showat, int done, int total) {
    char dash[51]  = "--------------------------------------------------";
    char blank[51] = "                                                  ";
    int portion = 0;
    float percent = 0.0f;

    printf ( "\033[?25l");//hide cursor
    printf ( "\033[38;5;10m");//green text
    printf ( "\033[48;5;0m");//on black
    printf ( "\033[%d;H", showat);//move curson to line showat
    percent = ( (float)done * 100) / total;
    portion = percent / 2.0f;
    printf ( "Progress: [\033[38;5;0m\033[48;5;10m");//black text on green
    printf ( "%.*s\033[38;5;10m\033[48;5;0m%.*s]%6.1f%% Complete\r"//back to green text on black
    , portion, blank, 50 - portion, dash, percent);

    printf ( "\033[0m");//reset color
    printf ( "\033[?25h");//show cursor
}

void test_progress ( ) {
    int item = 0;
    int allitems = 200;
    //do things in functioin where you need to show progress
    for ( item = 0; item <= allitems; item++) {
        working ( 5, item, allitems);
        usleep ( 50000);
    }
}

void test_loop ( ) {
    int item = 0;
    int allitems = 500;
    //do things in functioin where you need to show progress
    for ( item = 0; item <= allitems; item++) {
        working ( 9, item, allitems);
        usleep ( 50000);
    }
}

void choiceTwo(void){

    printf ( "\033[2J");//clear screen
    printf ( "\033[0;H");//move curson to line 0
    printf("================ RUN PROGRESS TEST ================\n\n");

    printf("\nTest progress'... ");
    test_progress();
    printf("\nOK\n");


    printf("\nTest loop'... ");
    test_loop();
    printf("\nOK\n");

}

int main()
{
    choiceTwo ( );
    return 0;
}