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

Posted dalanjing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用ncurses库开发终端工具箱—— 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,"")

 1 #include <iostream>
 2 #include <ncurses.h>
 3 #include <locale.h>
 4 #include "tool.h"
 5 #include "todolist.h"
 6 using namespace std;
 7 
 8 
 9 int main()
10 {
11     setlocale(LC_ALL,"");
12     initscr();
13     cbreak();
14     noecho();
15     curs_set(0);
16     keypad(stdscr,TRUE);
17 
18     refresh();
19 
20     TODOLIST *tt = new TODOLIST();
21     tt->display();
22 
23 
24     getch();
25     endwin();
26     return 0;    
27 
28 }

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

tool类

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

 1 #ifndef _TOOL_H_
 2 #define _TOOL_H_
 3 #include <string>
 4 #include <sstream>
 5 #include <ctime>
 6 #include <ncurses.h>
 7 using namespace std;
 8 
 9 class TOOL
10 {
11 public:
12     string int2str(int num);
13     string getCurrentDate();
14     WINDOW *create_newwin(int height, int width, int starty, int startx);
15     void destory_win(WINDOW *local_win);
16 };
17 
18 #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+1900)+""+int2str(ptminfo->tm_mon+1)+""
            +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,0,0);
    wrefresh(local_win);
    return local_win;
}

void TOOL::destory_win(WINDOW *local_win)
{
    wborder(local_win,  ,  ,  ,  ,  ,  ,  ,  );
    wrefresh(local_win);
    delwin(local_win);
}

TODOLIST类

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

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

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

 1 #ifndef _TODOLIST_H_
 2 #define _TODOLIST_H_
 3 #include <ncurses.h>
 4 #include "tool.h"
 5 #include <string>
 6 #include <cstring>
 7 using namespace std;
 8 
 9 class TODOLIST
10 {
11 public:
12     TODOLIST();
13     ~TODOLIST();
14     void display();
15 
16     TOOL tool;
17     WINDOW *title_win, *todo_win, *done_win, *info_win;
18     
19     int title_win_h, title_win_w, title_win_y, title_win_x;
20     int todo_win_h, todo_win_w, todo_win_y, todo_win_x;
21     int done_win_h, done_win_w, done_win_y, done_win_x;
22     int info_win_h, info_win_w, info_win_y, info_win_x;
23 };
24 #endif
 1 #include "todolist.h"
 2 
 3 TODOLIST::TODOLIST()
 4 {
 5     title_win_h = 3;
 6     
 7     title_win_y = 0;
 8     title_win_x = 0;
 9 
10     todo_win_h = 20;
11     todo_win_w = 40;
12     todo_win_y = title_win_y + title_win_h + 1;
13     todo_win_x = 0;
14 
15     done_win_h = todo_win_h;
16     done_win_w = todo_win_w;
17     done_win_y = todo_win_y;
18     done_win_x = todo_win_x + todo_win_w + 1;
19 
20     info_win_h = 3;
21     
22     info_win_y = todo_win_y + todo_win_h + 1;
23     info_win_x = 0;
24 
25     title_win_w = todo_win_w + done_win_w + 1;
26     info_win_w = title_win_w;
27 }
28 
29 void TODOLIST::display()
30 {
31 //显示标题窗口
32     title_win = tool.create_newwin(title_win_h,title_win_w,title_win_y,title_win_x);
33     char currdate[128];
34     string title;
35     title = tool.getCurrentDate() + "          " + "ToDoList 1.0         by 大蓝鲸";
36     strcpy(currdate,title.c_str());
37 
38     mvwprintw(title_win,1,1, currdate);
39     wrefresh(title_win);
40 
41 //待办事项窗口
42     todo_win = tool.create_newwin(todo_win_h,todo_win_w,todo_win_y,todo_win_x);
43     mvwprintw(todo_win, 0, 2, "未完成事项");
44     wrefresh(todo_win);
45 
46 //已完成事项窗口
47     done_win = tool.create_newwin(done_win_h,done_win_w,done_win_y,done_win_x);
48     mvwprintw(done_win, 0, 2, "已完成事项");
49     wrefresh(done_win);
50 
51 //信息提示窗口
52     info_win = tool.create_newwin(info_win_h,info_win_w,info_win_y,info_win_x);    
53     mvwprintw(info_win, 0, 2, "提示");
54     wrefresh(info_win);
55 }
56 
57 TODOLIST::~TODOLIST()
58 {
59     
60 }

编译运行

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

运行结果

 

代码放在Github上了,链接

 

以上是关于利用ncurses库开发终端工具箱—— ToDoList小工具开发的主要内容,如果未能解决你的问题,请参考以下文章

C指针原理(29)-Ncurses-文本终端的图形

Linux From Scratch(LFS11.0)交叉编译临时工具 - Ncurses-6.2

Linux系统下安装ncurses库

基于文本图形(ncurses)的文本搜索工具 ncgrep

mysqul数据库的安装

ncurses-devel