linux 用c程序写整个控制台

linux 用c程序写整个控制台

 

我们知道使用

printf("test \n");

这样只能输出一行。而我们看到过很多程序,启动后程序占满整个控制台窗口,这是怎么做到的呢?

原理就是先清掉整个整个屏幕

system( "clear");

让后画满整个屏幕

这个也是通过

printf()

这个函数实现。

然后

画完一屏之后,

再次调用 system("clear");清屏,然后再画。

这个过程可以在工作线程中实现,主线程读取用户输入,用来控制屏幕画什么样的内容。

直接上程序,下面的程序使用下面的命令,编译

$ gcc draw_console.c -lpthread -o draw_console

运行程序:

$ ./draw_console

按s停止刷新,按r继续刷新,按q退出程序。

代码:

#include <stdio.h>
#include <stdlib.h>
#include <wchar.h> // for future trials with wide chars
#include <locale.h> // to set Unicode locale
#include <string.h>
#include <pthread.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>

#define RB "\e(0\x6a\e(B" // 188 Right Bottom corner
#define RT "\e(0\x6b\e(B" // 187 Right Top corner
#define LT "\e(0\x6c\e(B" // 201 Left Top cornet
#define LB "\e(0\x6d\e(B" // 200 Left Bottom corner
#define MC "\e(0\x6e\e(B" // 206 Midle Cross
#define HL "\e(0\x71\e(B" // 205 Horizontal Line
#define LC "\e(0\x74\e(B" // 204 Left Cross
#define RC "\e(0\x75\e(B" // 185 Right Cross
#define BC "\e(0\x76\e(B" // 202 Bottom Cross
#define TC "\e(0\x77\e(B" // 203 Top Cross
#define VL "\e(0\x78\e(B" // 186 Vertical Line
#define SP " " // space string
#define TRUE 1
#define FALSE 0
// Move the whole box to the right 'RIGHT' number of spaces
#define RIGHT 8
// IF DBL_BOX TRUE draw a second box around
#define DBL_BOX TRUE

int g_bUpdate = 1;
/*
*/
//print number of box chars – two string variables
// if different able to print mixed box chars
// eg. first right cross, horizontal, right cross ect
// 'i' times.
void box_print(char *def_str1, char *def_str2, int i);
void print_nl(int i); // print 'i' number of newlines
// print string cgars within box characters
int box_letters(char *letters);
int top_out_line(int bars); //if DBL_BOX true draw top outline
int top_line(int bars); //draw top line of a box
int mid_line(int bars); //draw middle line ob a box
int bot_line(int bars); //draw bottol line of a box
int bot_out_line(int bars); // if DBL_BOX true draw second bottom line
void update_thread_proc( void* );
int kbhit(void);
int main(int n_args, char* args[])
{
    pthread_t tid;
    int uin;
    pthread_create( &tid, NULL, update_thread_proc, NULL);
    while( 1 )
    {
        if ( kbhit())
        {
            uin = getchar();
            switch ( uin )
            {
                case 'q':
                    exit(0);
                    break;
                case 's':
                    g_bUpdate = 0;
                    break;
                case 'r':
                    g_bUpdate = 1;
                    break;
                default:
                    break;

            }
        }
        else
        {
            usleep( 100000 );
        }
    }
}
void update_thread_proc( void* pVoid )
{
    int ltrs, i, j;
    char str1[] = "abggdfrtfdxzX\0";
    char str2[] = "BAGIHMTKBDXDe\0";
    char str3[] = "^%#*%^$)v^>jy\0";
    char str4[] = ",./';|@8&5~12\0";
    char str5[] = "+=+()8(7>>54B\0";
    char str6[] = "hfH*Ongf^^r_-\0";
    j = 0;
    for ( i = 0; ; i++ )
    {
        if( !g_bUpdate )
        {
            usleep( 100000 );
            continue;
        }
        system("clear");
        // setlocale(LC_ALL, "pl_PL.utf8"); //local na utf8-Polish type
        // Sample strings for boxed printing
        if ( j > strlen( str1 ) – 1)
        {
            j = 0;
        }
        if( str1[j] >= 127 )
        {
            str1[j] = 33;
        }
        if( str2[j] >= 127 )
        {
            str2[j] = 33;
        }
        if( str3[j] >= 127 )
        {
            str3[j] = 33;
        }
        if( str4[j] >= 127 )
        {
            str4[j] = 33;
        }
        if( str5[j] >= 127 )
        {
            str5[j] = 33;
        }
        if( str6[j] >= 127 )
        {
            str6[j] = 33;
        }
        str1[j] = str1[j] + 1;
        str2[j] = str2[j] + 1;
        str3[j] = str3[j] + 1;
        str4[j] = str4[j] + 1;
        str5[j] = str5[j] + 1;
        str6[j] = str6[j] + 1;

        j++;
        print_nl(3); // print 3 newlines
        top_line(strlen(str1)); // print top line of str1 length
        ltrs = box_letters(str1); // print boxed letters
        mid_line(strlen(str1)); // print middle box line
        ltrs = box_letters(str2);
        mid_line(strlen(str2));
        ltrs = box_letters(str3);
        mid_line(strlen(str3));
        ltrs = box_letters(str4);
        mid_line(strlen(str6));
        ltrs = box_letters(str5);
        mid_line(strlen(str5));
        ltrs = box_letters(str6);
        bot_line(strlen(str4)); // print the bottom line of the box
        print_nl(5);
        printf( "[input ‘q’ to exit program] [input ‘s’ to stop update] [input ‘r’ to update]\n");
        usleep( 100000 );    
    }
}
// ===============================================
void box_print(char *gr1, char *gr2, int repeat)
{
int i;
i = 0;
if (repeat > 1)
{
for (i = 1; i < repeat; i++)
{
printf("%s", gr1); printf("%s", gr2);
}
}
printf("%s", gr1);
return;
}
//==================
void print_nl(count)
{
int i;
if (count > 1)
{
for (i = 0; i < count; i++)
printf("\n");
}
else
printf("\n");
return;
}
//=============================
int box_letters(char *letters)
{
int i = 0;
int j = 0;
print_nl(1);
if (RIGHT)
{
if(DBL_BOX)
{
for( j = 1; j < RIGHT; j++) putchar(' '); box_print(VL, VL, 1);
}
else
{
for (j = 0; j < RIGHT; j++) putchar(' ');
}
}
box_print(VL, VL, 1);
while (letters[i])
{
printf("%c", letters[i++]);
box_print(VL, VL, 1);
}
if (DBL_BOX) box_print(VL, VL, 1);
print_nl(1);
return(i);
}
//================
int top_line(bars)
{
int i;
if (DBL_BOX)
{
top_out_line(bars + 1);
print_nl(1);
}
if (RIGHT)
{
if (DBL_BOX)
{
for( i = 1; i < RIGHT; i++) putchar(' ');
box_print(VL, VL, 1);
}
else
{
for( i = 0; i < RIGHT; i++) putchar(' ');
}
}
box_print(LT, LT, 1);
box_print(HL, TC, bars);
box_print(RT, RT,1);
if (DBL_BOX) box_print(VL, VL, 1);
return(0);
}
//=================
int bot_line(bars)
{
int i;
if (RIGHT)
{
if (DBL_BOX)
{
for( i = 1; i < RIGHT; i++) putchar(' ');
box_print(VL, VL, 1);
}
else
{
for( i = 0; i < RIGHT; i++) putchar(' ');
}
}
box_print(LB, LB, 1);
box_print(HL, BC, bars);
box_print(RB, RB,1);
if (DBL_BOX)
{
box_print(VL, VL, 1);
print_nl(1);
bot_out_line(bars + 1);
}
return(0);
}
//================
int mid_line(bars)
{
int i;
if (RIGHT)
{
if(DBL_BOX)
{
for( i = 1; i < RIGHT; i++) putchar(' '); box_print(VL, VL, 1);
}
else
{
for (i = 0; i < RIGHT; i++) putchar(' ');
}
}
box_print(LC, LC, 1);
box_print(HL, MC, bars);
box_print(RC, RC,1);
if (DBL_BOX) box_print(VL, VL, 1);
return(0);
}
//====================
int top_out_line(bars)
{
int i;
if (RIGHT)
{ for( i = 0; i < RIGHT – 1; i++) putchar(' '); }
box_print(LT, LT, 1);
box_print(HL, HL, bars);
box_print(RT, RT,1);
return(0);
}
//====================
int bot_out_line(bars)
{
int i;
if (RIGHT)
{ for( i = 0; i < RIGHT – 1; i++) putchar(' '); }
box_print(LB, LB, 1);
box_print(HL, HL, bars);
box_print(RB, RB,1);
return(0);
}

int kbhit(void)
{
  struct termios oldt, newt;
  int ch;
  int oldf;
 
  tcgetattr(STDIN_FILENO, &oldt);
  newt = oldt;
  newt.c_lflag &= ~(ICANON | ECHO);
  tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
  fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
 
  ch = getchar();
 
  tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  fcntl(STDIN_FILENO, F_SETFL, oldf);
 
  if(ch != EOF)
  {
    ungetc(ch, stdin);
    return 1;
  }
 
  return 0;
}

 

 

这里提供代码下载:

draw-console

下载后将文件后缀改成 .c 就好。

 

版权所有,禁止转载. 如需转载,请先征得博主的同意,并且表明文章出处,否则按侵权处理.

    分享到:

留言

你的邮箱是保密的 必填的信息用*表示