Developing address book management software with C language under Linux (1)

A simple address book management software that can manage personal information. It can add, delete, modify and search contacts, write, save and read contact data from files

(1) Realize the main menu and its menu item selection function

(header file description:

CTRL cur. H: encapsulates some operations of the cursor under Linux (cursor moving and positioning, font foreground background color setting, screen clearing, etc.), detailed introduction and source code: https://blog.csdn.net/zhou_1999/article/details/81173261

termio.h: it is used to realize the character reading function getch() without echo. You can read characters without pressing enter. Reference blog: https://blog.csdn.net/gaopu12345/article/details/30467099)

#include <stdio.h>
#include <string.h>
#include <termio.h>
#include "ctrlcur.h"
#define MAIN_MENU_WIDTH 30

int getch(void);
void putLine(char *,int,int);
void mainMenu();
void delimiter(int);
//Length of menu
int hight = 1;

int main(int argc,char *argv[])
{
	
	mainMenu();
	return 0;
}
//Separator
void delimiter(int len)
{
	int i;
	printf("├");
	for(i = 0; i < len - 2; i++)
	{
		printf("─");
	}
	printf("┤\n");
	hight++;
	
}

void putLine(char *con,int y,int isSelect)
{
	int length = MAIN_MENU_WIDTH;
	printf("│");
	MOVETO(y,(length - 12)/2);
	if(isSelect != 0)
	{
		SET_FRONT_COLOR(FRONT_RED);
		SET_BACKGROUND_COLOR(BACKGROUND_WHITE);
		printf("%-16s",con);
		SET_FRONT_COLOR(FRONT_WHITE);
		SET_BACKGROUND_COLOR(BACKGROUND_BLACK);
	}
	else
	{
		printf("%-16s",con);
	}
	MOVETO(y,length);
	printf("│\n");
	hight++;
}
void mainMenu()
{
	int i;
	int isSelect = 1;
	char ch1,ch2,ch3;
	char item[][MAIN_MENU_WIDTH-2] = {"1.Enter address book",
										"2.Add a Contact ",
										"3.Import from file",
										"4.Export to file",
										"5.Empty contacts",
										"0.Exit address book"};
	int itemNum = sizeof(item)/sizeof(item[0]);
	while(1)
	{
		do
		{
			system("clear");
			hight = 1;
			printf("┌");
			for(i = 0; i < MAIN_MENU_WIDTH - 2; i++)
			{
				printf("─");
			}
			printf("┐\n");
			hight++;
			
			printf("│");
			MOVETO(hight,MAIN_MENU_WIDTH/3);
			printf("mail list");
			MOVETO(hight,MAIN_MENU_WIDTH);
			printf("│\n");
			hight++;
			delimiter(MAIN_MENU_WIDTH);
			putLine("",hight,0);
			for(i = 0; i < itemNum; i++)
			{
				if(isSelect == i)
				{
					putLine(item[i],hight,1);
				}
				else
				{
					putLine(item[i],hight,0);
				}
				putLine("",hight,0);
			}
			printf("└");
			for(i = 0; i < MAIN_MENU_WIDTH - 2; i++)
			{
				printf("─");
			}
			printf("┘\n");
			ch1 = getch();
			if(ch1 == 27)
			{
				ch2 = getch();
				if(ch2 == 91)
				{
					ch3 = getch();
					if(ch3 == 65)
					{
						if(isSelect > 0)
						{
							isSelect--;
						}
					}
					if(ch3 == 66)
					{
						if(isSelect < itemNum-1)
						{
							isSelect++;
						}
					}
				}
			}
			if(ch1 == '1')
			{
				isSelect = 0;
			}
			if(ch1 == '2')
			{
				isSelect = 1;
			}
			if(ch1 == '3')
			{
				isSelect = 2;
			}
			if(ch1 == '4')
			{
				isSelect = 3;
			}
			if(ch1 == '5')
			{
				isSelect = 4;
			}
			if(ch1 == '0')
			{
				isSelect = 5;
			}
		}while(ch1 != 13);
		return;
	}
}
int getch(void)
{
	struct termios tm, tm_old;
	int fd = 0, ch;
	if (tcgetattr(fd, &tm) < 0) {//Save current terminal settings
		return -1;
	}
	tm_old = tm;
	cfmakeraw(&tm);//Change the terminal setting to the original mode, in which all input data is processed in bytes
	if (tcsetattr(fd, TCSANOW, &tm) < 0) {//Settings after changes on settings
		return -1;
	}
	ch = getchar();
	if (tcsetattr(fd, TCSANOW, &tm_old) < 0) {//Change the setting to the original look
		return -1;
	}
	return ch;
}

The above code only realizes the display of the main interface and the selection menu of the direction key, but it does not realize any function, which needs to be improved in the future~~

Tags: Linux

Posted on Fri, 31 Jan 2020 06:58:13 -0800 by Divided