ncurses 与 panel
ncurses 与 panel
一下是ncurses使用面板库panel的一个demo程序。
#include <ncurses.h>
#include <panel.h>
#include <string.h>
void init_wins(WINDOW **wins, int n);
void win_show(WINDOW *win, char *label, int label_color);
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
int main()
{
WINDOW *my_wins[3];
PANEL *my_panels[3];
PANEL *top;
int ch;
int new_x;
int new_y;
int widht;
int height;
int panel_c;
enum {
set_size,
move_,
none_
} status = none_;
/*初始化curses */
initscr();
start_color();
raw();
noecho();
keypad(stdscr, TRUE);
curs_set(0);
/* 初始化所有的颜色*/
init_pair(1, COLOR_RED, COLOR_WHITE);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_BLUE, COLOR_BLACK);
init_pair(4, COLOR_CYAN, COLOR_BLACK);
init_wins(my_wins, 3);
my_panels[0] = new_panel(my_wins[0]);
my_panels[1] = new_panel(my_wins[1]);
my_panels[2] = new_panel(my_wins[2]);
set_panel_userptr(my_panels[0],my_panels[1]);
set_panel_userptr(my_panels[1],my_panels[2]);
set_panel_userptr(my_panels[2],my_panels[0]);
update_panels();
attron(COLOR_PAIR(4));
attroff(COLOR_PAIR(4));
doupdate();
top = my_panels[2];
panel_c = 2;
mvprintw(LINES-2, 0, "Use tab to browse through the windows (F1 to Exit) [%d]", panel_c + 1);
while((ch = getch()) != KEY_F(1))
{
switch(ch) {
case '\t':
if (status == none_) {
bool is_hide;
top = (PANEL *) panel_userptr(top);
is_hide = panel_hidden(top);
if (panel_c == 2)
panel_c = 0;
else
panel_c ++;
mvprintw(LINES-2, 0, "Use tab to browse through the windows (F1 to Exit) [%d]", panel_c + 1);
if (is_hide)
printw(" [hide] ");
else {
top_panel(top);
printw(" [show] ");
}
}
break;
case 'm': {
WINDOW *win = panel_window(top);
getbegyx(win, new_y, new_x);
status = move_;
mvprintw(LINES-2, 0, "Moveing Windows (ENTER to Exit) [%d] ", panel_c + 1);
break;
}
case 'r': {
WINDOW *win = panel_window(top);
getbegyx(win, new_y, new_x);
getmaxyx(win, height, widht);
status = set_size;
mvprintw(LINES-2, 0, "Set Windows Size (ENTER to Exit) [%d] %d %d ", panel_c + 1, height, widht);
break;
}
case 's':
if (status == none_)
show_panel(top);
break;
case 'h':
if (status == none_) {
if (panel_hidden(top))
show_panel(top);
else
hide_panel(top);
}
break;
case KEY_RIGHT:
if (status == move_)
new_x ++;
else
widht ++;
break;
case KEY_LEFT:
if (status == move_)
new_x --;
else
widht --;
break;
case KEY_UP:
if (status == move_)
new_y --;
else
height --;
break;
case KEY_DOWN:
if (status == move_)
new_y ++;
else
height ++;
break;
case 10:
status = none_;
mvprintw(LINES-2, 0, "Use tab to browse through the windows (F1 to Exit) [%d]\n", panel_c + 1);
break;
default:
mvprintw(LINES-3, 0, "ch = %d\n", ch);
break;
}
if (status == move_)
move_panel(top, new_y, new_x);
else if (status == set_size) {
char label[80] = {};
WINDOW *old = panel_window(top);
WINDOW *win = newwin(height, widht, new_y, new_x);
replace_panel(my_panels[panel_c], win);
delwin(old);
sprintf(label, "Window Number %d", panel_c + 1);
win_show(win, label, panel_c + 1);
}
update_panels();
doupdate();
}
endwin();
return 0;
}
// 初始化三个窗体并放入数组wins中
void init_wins(WINDOW *wins[], int n) {
char label[80] = {};
int x = 10;
int y = 2;
for(int i = 0; i < n; i++) {
wins[i] = newwin(10, 40, y, x);
sprintf(label, "Window Number %d", i + 1); // 设置窗体名字
win_show(wins[i], label, i + 1); // 绘制窗体内的内容
y += 3;
x += 7;
}
}
void win_show(WINDOW *win, char *label, int label_color) {
int startx;
int starty;
int height;
int width;
getbegyx(win, starty, startx);
getmaxyx(win, height, width);
box(win, 0, 0);
mvwhline(win, 2, 1, '-', width-2); // 画一条线
print_in_middle(win, 1, 0, width, label, COLOR_PAIR(label_color)); // 在中间显示文字
}
void print_in_middle(WINDOW *win, int y, int startx, int width, char *string, chtype color) {
int x;
if(win == NULL)
win = stdscr;
if(y == 0)
getyx(win, y, x);
x = startx + (width - strlen(string)) / 2;
wattron(win, color);
mvwprintw(win, y, x, "%s", string);
wattroff(win, color);
refresh();
}
按tab切换窗体,按m改变窗体位置,按r设定窗体大小。按h显示或隐藏窗体。
ncurses 与 panel的更多相关文章
- 从头开始构建LINUX [LFS 脚本]
脚本共享在这 http://pan.baidu.com/s/1nt6yiH7 version-check.sh : 这个是检查HOST机器的软件依赖情况 host-dep.sh:针对ubuntu10_ ...
- [原]打造Python开发环境之Python环境
人生苦短,我用Python 一.升级到python2.7 开发环境的系统是centos 6.0, 默认的python版本是2.6.6, 由于线上环境是python2.7,为了防止版本差异产生的问题,所 ...
- 重新安装python
1. 在上次进行安装python的时候,很多东西不能用,例如后退键,删除键,都是不能在命令行中使用,主要原因是在编译python的时候,相关的安装包没有进行安装,从而导致出现乱码,在编译最新版本的py ...
- LFS7.4编译笔记(2)
上一篇我们已经搭建好了临时系统,这一篇我们就开始正式构建我们的最终LFS系统. 首先切换到root,准备虚拟内核文件系统并挂载: su - export LFS=/mnt/lfs mkdir -pv ...
- CentOS6下编译安装Python2.7.6方法
关于在CentOS6下编译安装Python2.7.6的方法非常的多了,小编以前也介绍过相关的文章了,下面一聚教程小编再来为各位介绍一下吧,希望文章能帮助到各位. CentOS下面Python在升级 ...
- 2、CentOS下编译安装Python2.7.6(转)
CentOS系统下面Python在升级到2.7.6的时候,没有找到安装包直接安装,只能通过源代码编译的方式来安装Python 2.7.6版本.这篇是编译和安装Python2.7.6的过程记录. Cen ...
- Python编译安装遇到的问题
1.python在make时候报错 Python build finished, but the necessary bits to build these modules were not foun ...
- lfs(systemd版本)学习笔记-第3页
我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! lfs(systemd)学习笔记-第2页 的地址:https://www.cnblogs.com/renren-study-no ...
- lfs(systemv版本)学习笔记-第3页
我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! lfs(systemv版本)学习笔记-第2页的地址:https://www.cnblogs.com/renren-study-n ...
- 从零开始构建Linux
目的:深入了解以Linux内核为基础的系统是如何组成,运行,以构建一个最基础的,纯净的系统. LFS构建步骤宿主机准备- linux操作系统安装- 使用独立硬盘,创建分区- 配置用户和组- 下载所有需 ...
随机推荐
- Solution -「洛谷 P6156」简单题
Description Link. 求 \(\sum\limits_{i=1}^n\sum\limits_{j=1}^n(i+j)^kf(\gcd(i,j))\gcd(i,j)\). Solution ...
- Solution -「CSP 2019」Partition
Description Link. 给出一个数列,要求将其分成几段,使每段的和非严格递增,求最小的每段的和的平方和. Solution 注意到 \(a_{i}\) 为正,对于正数有这样的结论: \[a ...
- 【python爬虫】爬虫所需要的爬虫代理ip是什么?
前言 在进行爬虫程序开发时,经常会遇到访问被限制的网站,这时就需要使用代理 IP 来进行访问.本文将介绍代理 IP 的概念及使用方法,帮助读者更好地应对爬虫程序中的访问限制问题.同时,本文还将提供一些 ...
- Apache(2.4.49 2.4.50)--目录遍历--命令执行--(CVE-2021-42013)&&(CVE-2021-41773)
Apache(2.4.49 2.4.50)--目录遍历--命令执行--(CVE-2021-42013)&&(CVE-2021-41773) 复现环境 采用Vulfocus靶场环境进行复 ...
- ⭐malloc(易造成内存泄漏)(及时释放内存)
1,关于malloc以及相关的几个函数 #include <stdlib.h>(Linux下) void *malloc(size_t size); void fr ...
- Linux常用命令大全 Linux Commands Line - v1.0
The most complete and updated list of commands on linux by LinuxGuide.it - over 350 commands! ...
- 文心一言 VS 讯飞星火 VS chatgpt (110)-- 算法导论10.2 1题
一.用go语言,单链表上的动态集合操作 INSERT 能否在 O(1)时间内实现?DELETE 操作呢? 文心一言: 在单链表中,INSERT 和 DELETE 操作的时间复杂度通常是 O(n),其中 ...
- 23集训 Day4 数论
快速幂 定义 快速幂,是一个在 \(\Theta(\log n)\) 的时间内计算 \(a^n\) 的小技巧,而暴力的计算需要 \(\Theta(n)\) 的时间. 解释 \[\because a^{ ...
- 网格布局grid
起因 昨天面试的时候,出了一道面试题,模拟面试公司的列表的元素宽度伸缩变化,根据屏幕大小的不同,一行上放置最多的元素,元素宽度不固定,间距固定,可换行,靠左对齐,当时猜出来用flexjs监听resiz ...
- 小白也能看懂的 AUC 曲线详解
小白也能看懂的 AUC 曲线详解 简介 上篇文章 小白也能看懂的 ROC 曲线详解 介绍了 ROC 曲线.本文介绍 AUC.AUC 的全名为Area Under the ROC Curve,即 ROC ...