热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

TerminalGfxCheaderfilethatprovidesafewfunctionsandcolorsforcreatingabasicUIi

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 window
	char *title;		// the title of the window
	char *titleColor;	// color of the window's title

	int x, y;			// x and y coordinates of the top left corner of the window
	int width, height;	// the width and the height of the window

	int drawBorder;		// 1 or 0, enables/disables drawing of the border and title
	char *borderColor;	// color of the window's border

	int 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 lines
    int lines = 0;

	#ifdef TIOCGSIZE
    	struct 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 columns

	int cols = 0;

	#ifdef TIOCGSIZE
    	struct 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 text

	titlePosition = (w.width / 2) - (strlen(w.title) / 2);

	// if border drawing is set to true (1), draw the border and the title
	if(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 off
		setTextAttr(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 background
	if(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; idrawWindow(windows[i]);
}

termgfx_MANUAL.txt内容如下:

Terminal graphics header file 

Author: Chirculete Vlad Mihai
Date: 12.10.2016

	Functions 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)
		RED
		GRN  - green
		YEL  - yellow
		BLU  - blue
		MAG  - magenta
		CYN  - cyan
		WHT  - white

		Background colors (for text background and boxes)

		BGRED   - background red
		BGGRN	- background green
		BGYEL	- background yellow
		BGBLU	- background blue
		BGMAG	- background magenta
		BGCYN	- background cyan
		BGWHT	- 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 0
	w[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 1
	w[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 2
	w[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;
}

结果图如下:



推荐阅读
  • ScrollView嵌套Collectionview无痕衔接四向滚动,支持自定义TitleView
    本文介绍了如何实现ScrollView嵌套Collectionview无痕衔接四向滚动,并支持自定义TitleView。通过使用MainScrollView作为最底层,headView作为上部分,TitleView作为中间部分,Collectionview作为下面部分,实现了滚动效果。同时还介绍了使用runtime拦截_notifyDidScroll方法来实现滚动代理的方法。具体实现代码可以在github地址中找到。 ... [详细]
  • Go GUIlxn/walk 学习3.菜单栏和工具栏的具体实现
    本文介绍了使用Go语言的GUI库lxn/walk实现菜单栏和工具栏的具体方法,包括消息窗口的产生、文件放置动作响应和提示框的应用。部分代码来自上一篇博客和lxn/walk官方示例。文章提供了学习GUI开发的实际案例和代码示例。 ... [详细]
  • 欢乐的票圈重构之旅——RecyclerView的头尾布局增加
    项目重构的Git地址:https:github.comrazerdpFriendCircletreemain-dev项目同步更新的文集:http:www.jianshu.comno ... [详细]
  • 本文介绍了pack布局管理器在Perl/Tk中的使用方法及注意事项。通过调用pack()方法,可以控制部件在显示窗口中的位置和大小。同时,本文还提到了在使用pack布局管理器时,应注意将部件分组以便在水平和垂直方向上进行堆放。此外,还介绍了使用Frame部件或Toplevel部件来组织部件在窗口内的方法。最后,本文强调了在使用pack布局管理器时,应避免在中间切换到grid布局管理器,以免造成混乱。 ... [详细]
  • 如何优化Webpack打包后的代码分割
    本文介绍了如何通过优化Webpack的代码分割来减小打包后的文件大小。主要包括拆分业务逻辑代码和引入第三方包的代码、配置Webpack插件、异步代码的处理、代码分割重命名、配置vendors和cacheGroups等方面的内容。通过合理配置和优化,可以有效减小打包后的文件大小,提高应用的加载速度。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 这是原文链接:sendingformdata许多情况下,我们使用表单发送数据到服务器。服务器处理数据并返回响应给用户。这看起来很简单,但是 ... [详细]
  • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文介绍了PE文件结构中的导出表的解析方法,包括获取区段头表、遍历查找所在的区段等步骤。通过该方法可以准确地解析PE文件中的导出表信息。 ... [详细]
  • phpcomposer 那个中文镜像是不是凉了 ... [详细]
  • 使用圣杯布局模式实现网站首页的内容布局
    本文介绍了使用圣杯布局模式实现网站首页的内容布局的方法,包括HTML部分代码和实例。同时还提供了公司新闻、最新产品、关于我们、联系我们等页面的布局示例。商品展示区包括了车里子和农家生态土鸡蛋等产品的价格信息。 ... [详细]
  • .NetCoreWebApi生成Swagger接口文档的使用方法
    本文介绍了使用.NetCoreWebApi生成Swagger接口文档的方法,并详细说明了Swagger的定义和功能。通过使用Swagger,可以实现接口和服务的可视化,方便测试人员进行接口测试。同时,还提供了Github链接和具体的步骤,包括创建WebApi工程、引入swagger的包、配置XML文档文件和跨域处理。通过本文,读者可以了解到如何使用Swagger生成接口文档,并加深对Swagger的理解。 ... [详细]
  • 颜色迁移(reinhard VS welsh)
    不要谈什么天分,运气,你需要的是一个截稿日,以及一个不交稿就能打爆你狗头的人,然后你就会被自己的才华吓到。------ ... [详细]
author-avatar
mobiledu2502879833
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有