利用ncurses库开发终端工具箱(1)—— ToDoList小工具开发

时间:2023-03-09 15:02:25
利用ncurses库开发终端工具箱(1)—— ToDoList小工具开发

准备工作

腾讯云服务器(Ubuntu),C++编程语言

由于想输出界面中包含中文,所以安装库 libncursesw5,依次输入下面三行命令

sudo apt-get install libncursesw5
sudo apt-get install libncursesw5-dbg
sudo apt-get install libncursesw5-dev

编译用如下命令

g++ main.cpp -l ncursesw

ToDoList小工具开发

思路

由于一开始想的就比较多,想做一个工具箱,所以Todolist工具只是其中的一个,这样的话用c++每个小工具封装成一个类肯定更适合。

主程序很简单,都用一个套路,注意由于要输出中文,所以头文件有locale.h, 主程序中也添加了setlocale(LC_ALL,"")

 #include <iostream>
#include <ncurses.h>
#include <locale.h>
#include "tool.h"
#include "todolist.h"
using namespace std; int main()
{
setlocale(LC_ALL,"");
initscr();
cbreak();
noecho();
curs_set();
keypad(stdscr,TRUE); refresh(); TODOLIST *tt = new TODOLIST();
tt->display(); getch();
endwin();
return ; }

注意到头文件中有两个自己设定的类tool和todolist

tool类

这里面主要放一些通用的,或者用的比较多的功能,如创建一个窗口,销毁一个窗口,获取当前时间等。

 #ifndef _TOOL_H_
#define _TOOL_H_
#include <string>
#include <sstream>
#include <ctime>
#include <ncurses.h>
using namespace std; class TOOL
{
public:
string int2str(int num);
string getCurrentDate();
WINDOW *create_newwin(int height, int width, int starty, int startx);
void destory_win(WINDOW *local_win);
}; #endif
#include "tool.h"

string TOOL::int2str(int num)
{
stringstream stream;
stream<<num;
return stream.str();
} string TOOL::getCurrentDate()
{
time_t rawtime;
struct tm *ptminfo; time(&rawtime);
ptminfo = localtime(&rawtime); string ss="时间:"+int2str(ptminfo->tm_year+)+"年"+int2str(ptminfo->tm_mon+)+"月"
+int2str(ptminfo->tm_mday)+"日";
return ss;
} WINDOW *TOOL::create_newwin(int height, int width, int starty, int startx)
{
WINDOW *local_win;
local_win = newwin(height, width, starty, startx);
box(local_win,,);
wrefresh(local_win);
return local_win;
} void TOOL::destory_win(WINDOW *local_win)
{
wborder(local_win, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ');
wrefresh(local_win);
delwin(local_win);
}

TODOLIST类

这里面就是我们小工具的功能实现

一个简单的todolist我在这里分成了四个部分,标题,未完成,已完成,输出/显示窗口,因此要建立四个窗口

每个窗口的尺寸位置都要注意分配。

 #ifndef _TODOLIST_H_
#define _TODOLIST_H_
#include <ncurses.h>
#include "tool.h"
#include <string>
#include <cstring>
using namespace std; class TODOLIST
{
public:
TODOLIST();
~TODOLIST();
void display(); TOOL tool;
WINDOW *title_win, *todo_win, *done_win, *info_win; int title_win_h, title_win_w, title_win_y, title_win_x;
int todo_win_h, todo_win_w, todo_win_y, todo_win_x;
int done_win_h, done_win_w, done_win_y, done_win_x;
int info_win_h, info_win_w, info_win_y, info_win_x;
};
#endif
 #include "todolist.h"

 TODOLIST::TODOLIST()
{
title_win_h = ; title_win_y = ;
title_win_x = ; todo_win_h = ;
todo_win_w = ;
todo_win_y = title_win_y + title_win_h + ;
todo_win_x = ; done_win_h = todo_win_h;
done_win_w = todo_win_w;
done_win_y = todo_win_y;
done_win_x = todo_win_x + todo_win_w + ; info_win_h = ; info_win_y = todo_win_y + todo_win_h + ;
info_win_x = ; title_win_w = todo_win_w + done_win_w + ;
info_win_w = title_win_w;
} void TODOLIST::display()
{
//显示标题窗口
title_win = tool.create_newwin(title_win_h,title_win_w,title_win_y,title_win_x);
char currdate[];
string title;
title = tool.getCurrentDate() + " " + "ToDoList 1.0 by 大蓝鲸";
strcpy(currdate,title.c_str()); mvwprintw(title_win,,, currdate);
wrefresh(title_win); //待办事项窗口
todo_win = tool.create_newwin(todo_win_h,todo_win_w,todo_win_y,todo_win_x);
mvwprintw(todo_win, , , "未完成事项");
wrefresh(todo_win); //已完成事项窗口
done_win = tool.create_newwin(done_win_h,done_win_w,done_win_y,done_win_x);
mvwprintw(done_win, , , "已完成事项");
wrefresh(done_win); //信息提示窗口
info_win = tool.create_newwin(info_win_h,info_win_w,info_win_y,info_win_x);
mvwprintw(info_win, , , "提示");
wrefresh(info_win);
} TODOLIST::~TODOLIST()
{ }

编译运行

g++ *.h *.cpp -l ncursesw
./a.out

运行结果

代码放在Github上了,链接