TerminalGfx

C header file that provides a few functions and colors for creating a basic UI in a terminal.

[GitHub: TerminalGfx]https://github.com/MihaiChirculete/TerminalGfx

文件树

$ tree
.
├── README.md
├── termgfx.h
├── termgfx_MANUAL.txt
├── TerminalGfx.md
└── test.c

termgfx.h文件内容如下:

/*
    Header file that allows:
        --> drawing certain UI elements within the terminal such as colored boxes
        --> moving the cursor within the terminal
        --> getting the width and height of the terminal in Lines and Columns
        --> clearing the screen

    Author: Chirculete Vlad Mihai
    Date (created): 12.10.2016
    Date (last edit): 19.11.2016
*/#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <string.h>// text colors
#define NRM  "\x1B[0m"
#define BLK  "\x1B[30m"
#define RED  "\x1B[31m"
#define GRN  "\x1B[32m"
#define YEL  "\x1B[33m"
#define BLU  "\x1B[34m"
#define MAG  "\x1B[35m"
#define CYN  "\x1B[36m"
#define WHT  "\x1B[37m"// background colors
#define BGBLK  "\x1B[40m"
#define BGRED  "\x1B[41m"
#define BGGRN  "\x1B[42m"
#define BGYEL  "\x1B[43m"
#define BGBLU  "\x1B[44m"
#define BGMAG  "\x1B[45m"
#define BGCYN  "\x1B[46m"
#define BGWHT  "\x1B[47m"// other text attributes
#define BOLD_ON "\x1b[1m"      // Bold on(enable foreground intensity)
#define UNDERLINE_ON "\x1b[4m"     // Underline on
#define BLINK_ON "\x1b[5m"         // Blink on(enable background intensity)
#define BOLD_OFF "\x1b[21m"        // Bold off(disable foreground intensity)
#define UNDERLINE_OFF "\x1b[24m" //   Underline off
#define BLINK_OFF "\x1b[25m"   // Blink off(disable background intensity)typedef struct window window;struct window
{int id;                // the id of the windowchar *title;     // the title of the windowchar *titleColor; // color of the window's titleint x, y;            // x and y coordinates of the top left corner of the windowint width, height;   // the width and the height of the windowint drawBorder;        // 1 or 0, enables/disables drawing of the border and titlechar *borderColor;   // color of the window's borderint drawBackground;     // 1 or 0, enables/disables drawing of the window background char *backgroundColor; // background color of the box
};// sets the window's title and enables border drawing
void setWindowTitle(window *w, char *title, char *title_color)
{(*w).title = title;(*w).titleColor = title_color;(*w).drawBorder = 1;
}// sets the window's border color and enables border drawing
void setWindowBorderColor(window *w, char *color)
{(*w).borderColor = color;(*w).drawBorder = 1;
}// moves the cursor to X and Y in terminal
void gotoxy(int x,int y)
{printf("%c[%d;%df",0x1B,y,x);
}// get the terminal height in lines
int getTermHeight()
{// get the terminal height in linesint lines = 0;#ifdef TIOCGSIZEstruct ttysize ts;ioctl(STDIN_FILENO, TIOCGSIZE, &ts);lines = ts.ts_lines;#elif defined(TIOCGWINSZ)struct winsize ts;ioctl(STDIN_FILENO, TIOCGWINSZ, &ts);lines = ts.ws_row;#endif /* TIOCGSIZE */return lines;
}int getTermWidth()
{// get the terminal width in columnsint cols = 0;#ifdef TIOCGSIZEstruct ttysize ts;ioctl(STDIN_FILENO, TIOCGSIZE, &ts);cols = ts.ts_cols;#elif defined(TIOCGWINSZ)struct winsize ts;ioctl(STDIN_FILENO, TIOCGWINSZ, &ts);cols = ts.ws_col;#endif /* TIOCGSIZE */return cols;
}// draws a filled bar and then moves the cursor to the top of the terminal
void drawBar(int x, int y, int width, int height, char *color)
{width--;height--;int i, j=x;gotoxy(x, y);for(i=y; i<=y+height; i++){gotoxy(x, i);j=x;while(j<=x+width){printf("%s ", color);  j++;}}printf("%s", NRM);gotoxy(0, 0);
}// draws a box (different from a bar since it has no filling)  then moves the cursor to the top of the terminal
void drawBox(int x, int y, int width, int height, char *color)
{width--;height--;int i, j=x;gotoxy(x, y);for(i=y; i<=y+height; i++){gotoxy(x, i);j=x;while(j<=x+width){if(j==x || j==x+width || i==y || i == y+height) printf("%s ", color);    else printf("%s ", NRM);j++;}//printf("%s\n", NRM);}printf("%s", NRM);gotoxy(0, 0);
}// prints text at X and Y then moves the cursor to the top of the terminal
void textXY(char* text, int x, int y)
{gotoxy(x, y);printf("%s", text);gotoxy(0, 0);
}// prints colored text at X and Y then moves the cursor to the top of the terminal
void textXYcolor(char* text, int x, int y, char* text_color, char* text_bg)
{gotoxy(x, y);printf("%s%s%s%s", text_color, text_bg, text, NRM);gotoxy(0, 0);
}// sets the current text color to TEXT_COLOR
void setTextColor(char* text_color)
{printf("%s", text_color);
}// sets the current text background to TEXT_BG
void setTextBackground(char* text_bg)
{printf("%s", text_bg);
}// sets text attributes
void setTextAttr(char* text_attr)
{printf("%s", text_attr);
}// clear the screen
void clearscr()
{system("clear");
}// draw a given window
void drawWindow(window w)
{int titlePosition; // position on X axis where to start drawing the texttitlePosition = (w.width / 2) - (strlen(w.title) / 2);// if border drawing is set to true (1), draw the border and the titleif(w.drawBorder){drawBox(w.x, w.y, w.width, w.height, w.borderColor);// set the bold and underline attributes to ON for title then switch it back offsetTextAttr(BOLD_ON);    setTextAttr(UNDERLINE_ON);textXYcolor(w.title, w.x + titlePosition, w.y, w.titleColor, w.borderColor);setTextAttr(BOLD_OFF);   setTextAttr(UNDERLINE_OFF);}// if background drawing is set to true (1), draw the backgroundif(w.drawBackground){drawBar(w.x+1, w.y+1, w.width-1, w.height-1, w.backgroundColor);}
}// draw a given array of windows
void drawWindows(window windows[], int n)
{int i;for(i=0; i<n; i++)drawWindow(windows[i]);
}

termgfx_MANUAL.txt内容如下:

Terminal graphics header file Author: Chirculete Vlad Mihai
Date: 12.10.2016Functions provided by termgfx.h:--> clearscr()Clears the screen.--> gotoxy(int x, int y)Moves the cursor inside the terminal at position given by x and y.--> getTermHeight()Returns an integer which represents the height of the terminal measured in columns.--> getTermWidth()Returns an integer which represents the width of the terminal measured in lines.--> drawBar(int x, int y, int WIDTH, int HEIGHT, char *FILL_COLOR)Draws a filled bar at x and y that is WIDTH wide and HEIGHT tall. The filling color is determined by FILL_COLOR.For the variable FILL_COLOR you should use one of the background colors defined. (see the list of availible colors below)--> drawBox(int x, int y, int WIDTH, int HEIGHT, char *COLOR)Draws a box that unlike the bar it isn't filled.--> textXY(char *TEXT, int x, int y)Prints TEXT at the given x and y position inside the terminal.--> textXYcolor(char *TEXT, int x, int y, char *TEXT_COLOR , char *TEXT_BACKGROUND)Prints TEXT at the given x and y position inside the terminal.The color of the text is given by TEXT_COLOR, and it's background color by TEXT_BACKGROUND.You should use the colors defined in the list below.Colors provided by termgfx.h:Foreground colors (text colors)NRM  - normal (default terminal color)REDGRN  - greenYEL  - yellowBLU  - blueMAG  - magentaCYN  - cyanWHT  - whiteBackground colors (for text background and boxes)BGRED   - background redBGGRN  - background greenBGYEL - background yellowBGBLU    - background blueBGMAG  - background magentaBGCYN   - background cyanBGWHT  - background white

一个简单的测试:test.c

#include <stdlib.h>
#include <stdio.h>
#include "termgfx.h"void drawBar();int main(int argc, char **argv)
{system("clear");window w[10];// window 0w[0].title = "Window 0";w[0].drawBorder = 1;w[0].titleColor = WHT;w[0].borderColor = BGBLU;w[0].drawBackground = 0;w[0].backgroundColor = BGWHT;w[0].x = 0;w[0].y = 5;w[0].width = (getTermWidth()/2)-1;w[0].height = getTermHeight()-3;// window 1w[1].title = "Window 1";w[1].drawBorder = 1;w[1].titleColor = YEL;w[1].borderColor = BGGRN;w[1].drawBackground = 1;w[1].backgroundColor = BGWHT;w[1].x = getTermWidth()/2;w[1].y = 5;w[1].width = getTermWidth()/3;w[1].height = getTermHeight()/3;// window 2w[2].title = "Window 2";w[2].drawBorder = 1;w[2].titleColor = GRN;w[2].borderColor = BGYEL;w[2].drawBackground = 0;w[2].backgroundColor = BGWHT;w[2].x = getTermWidth()/2;w[2].y = getTermHeight()/2;w[2].width = getTermWidth()/2;w[2].height = getTermHeight()/2;drawWindows(w, 3);return 0;
}

结果图如下:

GitHub#C#:在终端里面显示一个UI窗口(TerminalGfx)相关推荐

  1. 命令行终端怎么显示√2̅?这其实是一个博客的Unicode测试文章

    命令行终端怎么显示√2̅?这其实是一个博客的Unicode测试文章 文章目录 命令行终端怎么显示√2̅?这其实是一个博客的Unicode测试文章 安装 `uni` 命令 `uni` 命令的基本使用方法 ...

  2. 试编写一个汇编语言程序,要求从键盘接收一个四位的十六进制数,并在终端上显示与它等值的二进制数

    试编写一个汇编语言程序,要求从键盘接收一个四位的十六进制数,并在终端上显示与它等值的二进制数 data segment data ends stack segment stack dw 30h dup ...

  3. ubuntu使用git时,终端不显示git分支。

    1:问题描述: 在Windows环境下习惯使用git bash操作git分支,最近学习linux环境,发现linux环境终端不显示git分支,相关现象如下:      期望效果是: 我的linux环境 ...

  4. Unity的ScrollRect如何裁切粒子特效,以及如何使粒子特效显示在UI上

    在功能开发中,有时候为了更好的效果会在UI上添加一些特效,比如在头像框上增加一个圆环的粒子特效,但由于粒子和UI的渲染方式有些不同,导致会出现UI和特效之间穿插,显示上不理想.并且如果在ScrollR ...

  5. PyQt的一个UI单元测试框架思路

    專 欄 ❈ 丁果,Python中文社区作者.对 django.pyqt.opencv.tornado感兴趣. GitHub:https://github.com/lidingke ❈ 一.思路 PyQ ...

  6. Quick-cocos2d-x3.3 Study (一) --------- 创建一个UI标签

    创建一个UI标签: 1 cc.ui.UILabel.new({ 2 UILabelType = 2, text = "Hello ,World", size = 64 3 }) 4 ...

  7. 如何在python中显示电脑中的图片-python在终端里面显示一张图片

    Linux终端里面可谓是奇妙无限,很多优秀的软件都诞生在终端里面.相较之下,Windows本身的理念和Linux就不一致,所以,你懂得. 下面,我们不妨先思考一下,如何在终端里面显示一张图片? 在终端 ...

  8. 如何在python中显示电脑中的图片-python如何在终端里面显示一张图片

    Linux终端里面可谓是奇妙无限,很多优秀的软件都诞生在终端里面.相较之下,Windows本身的理念和Linux就不一致,所以,你懂得. 下面,我们不妨先思考一下,如何在终端里面显示一张图片? 在终端 ...

  9. Linux基本C编程fork、signal、time以及用printf在终端打印一个GUI窗口 - 使用cygwin

    1 fork, vfork示例 创建一个新进程的方法只有由某个已存在的进程调用fork()或vfork(): vfork创建新进程的主要目的在于调用exec函数执行另外的一个新程序,在没调用exec或 ...

最新文章

  1. Python ATM
  2. Solidity合约记录——(三)如何在合约中对操作进行权限控制
  3. 点到面距离公式向量法_点到线或面的距离公式
  4. 用一个实际例子理解Docker volume工作原理 1
  5. 记一次微信数据库解密过程
  6. Java提高篇——equals()方法和“==”运算符
  7. 使用memcpy()时报错
  8. 数据分析工具具备什么功能
  9. Linux shell脚本详解及实战(五)——shell脚本函数
  10. 201521123070 《JAVA程序设计》第6周学习总结
  11. paip.微信菜单直接跳转url和获取openid流程总结
  12. 数学建模算法总结(一)
  13. 口腔科的固定修复计算机辅助设计收,3D打印技术在口腔种植修复中的应用进展...
  14. 51单片机制作计算机1602显示,51单片机对LCD1602液晶显示器的控制
  15. 市场营销策划书大纲怎么写
  16. 【有利可图网】PS保存图片提示“无法完成请求”,教你4种解决方法!
  17. 等待任务执行完成时,界面上转圈圈,不让用户操作软件
  18. 计算机系统未来发展方向论文,浅谈计算机未来发展趋势(期末论文)
  19. 个人申请企业邮箱还是163个人邮箱?个人邮箱怎么申请登录呢?
  20. 关于 字号、PX像素、PT点数、em、CM厘米、MM毫米之间的换算

热门文章

  1. 部分基于layui的时间函数
  2. [Bootstrap]bootstrap的简单原理
  3. xml没有提示解决办法eclipse
  4. 线程池的submit和execute方法区别
  5. 关于提示对话框的总结
  6. 使用Velocity(VTL)调用自定义C# .net 中的方法
  7. 班图ubuntu linux 5.1相当好用,windows危险了!
  8. signature pad java_2020-07-08 JSsignature_pad 无纸化电子签名
  9. mysql server8 jdbc_mysql8.0 jdbc连接注意事项
  10. mysql条件填充命令_MySQL如何填充范围内的缺失日期?