console监控窗口:动态显示信息(和top命令一样动态输出信息)
- #include <time.h>
-
#include <string.h>
-
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <unistd.h>
-
-
void show_new( char *str )
-
{
-
static int need_clear = 0;
-
char buff[1024] = {0x00};
-
memset( buff, 0x00, sizeof(buff) );
-
-
char back[1024] = {0x00};
-
int len = 0;
-
-
snprintf( buff, sizeof(buff),
-
"************ \033[40;31m \033[0m************\n"
-
"%s"
-
"\033[40;33m----------------------------------------\033[0m\n"
-
"\033[40;31m 提示: 退出 ctrl+c\033[0m\n"
-
"\033[40;36m Version: epoller 3.5.4 \033[0m\n"
-
"****************************************\n", str );
-
-
int i = 0;
-
len = strlen( buff );
-
//生成回退格 \b
-
while ( i < 512)
-
{
-
snprintf( back+i, sizeof(back)-i, "%s", "\b" );
-
i++;
-
}
-
int line = 0;
-
i = 0;
-
while ( i < len )
-
{
-
if ( buff[i] == '\n' )
-
line++;
-
i++;
-
}
-
-
// 清除屏幕
-
if ( need_clear != 0 )
-
{
-
for( i=0; i<line; i++ )
-
{
-
printf( "\033[1A" );//光标上移n行
-
printf( back ); // 回退清空本行
-
}
-
}
-
else
-
{
-
printf( "\033[2J" );//清除整个屏幕
-
need_clear = 1;
-
}
-
printf( "\033[0;0H" );//定位到屏幕左上角
-
-
// 打印新的内容
-
printf( buff);
-
//printf( "\033[?25l" );// 隐藏光标
-
fflush(stdout);
-
-
}
-
-
int main( int argc, char *argv[] )
-
{
-
int i;
-
for (i = 1; i <= 10000; ++i)
-
{
-
char buff[1024] = {0x00};
-
memset( buff, 0x00, sizeof(buff) );
-
-
char *user = getenv("USER");
-
time_t t = time( NULL );
-
snprintf( buff, sizeof(buff), "user: %s \ntime: %lu, index=%d\n", user, t, i );
-
show_new( buff );
-
sleep(1);
-
}
-
-
return 0;
-
}
阅读(2219) | 评论(0) | 转发(0) |