93 lines
3.4 KiB
C++
93 lines
3.4 KiB
C++
/********************************************************
|
|
* ██████╗ ██████╗████████╗██╗
|
|
* ██╔════╝ ██╔════╝╚══██╔══╝██║
|
|
* ██║ ███╗██║ ██║ ██║
|
|
* ██║ ██║██║ ██║ ██║
|
|
* ╚██████╔╝╚██████╗ ██║ ███████╗
|
|
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
|
|
* Geophysical Computational Tools & Library (GCTL)
|
|
*
|
|
* Copyright (c) 2023 Yi Zhang (yizhang-geo@zju.edu.cn)
|
|
*
|
|
* GCTL is distributed under a dual licensing scheme. You can redistribute
|
|
* it and/or modify it under the terms of the GNU Lesser General Public
|
|
* License as published by the Free Software Foundation, either version 2
|
|
* of the License, or (at your option) any later version. You should have
|
|
* received a copy of the GNU Lesser General Public License along with this
|
|
* program. If not, see <http://www.gnu.org/licenses/>.
|
|
*
|
|
* If the terms and conditions of the LGPL v.2. would prevent you from using
|
|
* the GCTL, please consider the option to obtain a commercial license for a
|
|
* fee. These licenses are offered by the GCTL's original author. As a rule,
|
|
* licenses are provided "as-is", unlimited in time for a one time fee. Please
|
|
* send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget
|
|
* to include some description of your company and the realm of its activities.
|
|
* Also add information on how to contact you by electronic and paper mail.
|
|
******************************************************/
|
|
|
|
#include "cli_viewer.h"
|
|
|
|
gctl::cli_viewer::cli_viewer()
|
|
{
|
|
// 初始化ncurses
|
|
initscr();
|
|
cbreak(); // 禁用行缓冲
|
|
noecho(); // 不显示输入字符
|
|
keypad(stdscr, TRUE); // 启用键盘扩展键
|
|
timeout(0); // 设置非阻塞输入
|
|
curs_set(0); // 隐藏光标
|
|
}
|
|
|
|
// 析构函数
|
|
gctl::cli_viewer::~cli_viewer()
|
|
{
|
|
// 清理ncurses
|
|
endwin();
|
|
}
|
|
|
|
// 设置显示内容
|
|
void gctl::cli_viewer::setData(const std::vector<std::string>& data)
|
|
{
|
|
lines = data;
|
|
return;
|
|
}
|
|
|
|
void gctl::cli_viewer::addData(const std::string& l)
|
|
{
|
|
lines.push_back(l);
|
|
return;
|
|
}
|
|
|
|
// 显示文件内容并进入查看循环
|
|
void gctl::cli_viewer::display()
|
|
{
|
|
int maxLines = LINES - 1; // 终端的行数(减去提示行)
|
|
int startLine = 0; // 当前显示的起始行
|
|
// 主循环
|
|
while (true)
|
|
{
|
|
// 清屏并显示文件内容
|
|
werase(stdscr); // 清屏但不闪烁
|
|
for (int i = startLine; i < startLine + maxLines && i < lines.size(); ++i)
|
|
{
|
|
mvprintw(i - startLine, 0, "%s", lines[i].c_str());
|
|
}
|
|
// 在最后一行显示提示信息
|
|
mvprintw(LINES - 1, 0, "Use arrow keys or mouse wheel to scroll. Press Q or ESC to exit.");
|
|
int ch = getch(); // 获取按键
|
|
if (ch == KEY_UP)
|
|
{
|
|
startLine = std::max(0, startLine - 1); // 向上滚动
|
|
}
|
|
else if (ch == KEY_DOWN)
|
|
{
|
|
startLine = std::min(startLine + 1, static_cast<int>(lines.size()) - maxLines); // 向下滚动
|
|
}
|
|
else if (ch == 'q' || ch == 'Q' || ch == 27)
|
|
{ // 按Q或ESC退出
|
|
break;
|
|
}
|
|
wrefresh(stdscr); // 刷新屏幕
|
|
}
|
|
return;
|
|
} |