cmake/Source/CursesDialog/cmCursesLongMessageForm.cxx

201 lines
4.7 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2016-07-09 11:21:54 +02:00
#include "cmCursesLongMessageForm.h"
2020-02-01 23:06:01 +01:00
#include <cstdio>
#include <cstring>
2016-10-30 18:24:19 +01:00
#include "cmCursesForm.h"
#include "cmCursesMainForm.h"
2016-10-30 18:24:19 +01:00
#include "cmCursesStandardIncludes.h"
2020-08-30 11:54:41 +02:00
#include "cmStringAlgorithms.h"
2016-10-30 18:24:19 +01:00
#include "cmVersion.h"
inline int ctrl(int z)
{
2016-07-09 11:21:54 +02:00
return (z & 037);
2013-03-16 19:13:01 +02:00
}
2016-07-09 11:21:54 +02:00
cmCursesLongMessageForm::cmCursesLongMessageForm(
2020-08-30 11:54:41 +02:00
std::vector<std::string> const& messages, const char* title,
ScrollBehavior scrollBehavior)
: Scrolling(scrollBehavior)
{
// Append all messages into on big string
2020-08-30 11:54:41 +02:00
this->Messages = cmJoin(messages, "\n");
this->Title = title;
2018-01-26 17:06:56 +01:00
this->Fields[0] = nullptr;
this->Fields[1] = nullptr;
}
cmCursesLongMessageForm::~cmCursesLongMessageForm()
{
2016-07-09 11:21:54 +02:00
if (this->Fields[0]) {
free_field(this->Fields[0]);
2016-07-09 11:21:54 +02:00
}
}
2020-08-30 11:54:41 +02:00
void cmCursesLongMessageForm::UpdateContent(std::string const& output,
std::string const& title)
{
this->Title = title;
if (!output.empty() && this->Messages.size() < MAX_CONTENT_SIZE) {
this->Messages.push_back('\n');
this->Messages.append(output);
form_driver(this->Form, REQ_NEW_LINE);
this->DrawMessage(output.c_str());
}
this->UpdateStatusBar();
touchwin(stdscr);
refresh();
}
void cmCursesLongMessageForm::UpdateStatusBar()
{
2020-02-01 23:06:01 +01:00
int x;
int y;
getmaxyx(stdscr, y, x);
char bar[cmCursesMainForm::MAX_WIDTH];
2018-10-28 12:09:07 +01:00
size_t size = this->Title.size();
2016-07-09 11:21:54 +02:00
if (size >= cmCursesMainForm::MAX_WIDTH) {
size = cmCursesMainForm::MAX_WIDTH - 1;
}
strncpy(bar, this->Title.c_str(), size);
2020-08-30 11:54:41 +02:00
for (size_t i = size; i < cmCursesMainForm::MAX_WIDTH; i++) {
2016-07-09 11:21:54 +02:00
bar[i] = ' ';
2016-10-30 18:24:19 +01:00
}
int width;
2020-08-30 11:54:41 +02:00
if (x >= 0 && x < cmCursesMainForm::MAX_WIDTH) {
width = x;
2016-07-09 11:21:54 +02:00
} else {
width = cmCursesMainForm::MAX_WIDTH - 1;
}
bar[width] = '\0';
char version[cmCursesMainForm::MAX_WIDTH];
char vertmp[128];
2016-07-09 11:21:54 +02:00
sprintf(vertmp, "CMake Version %s", cmVersion::GetCMakeVersion());
size_t sideSpace = (width - strlen(vertmp));
for (size_t i = 0; i < sideSpace; i++) {
version[i] = ' ';
}
sprintf(version + sideSpace, "%s", vertmp);
version[width] = '\0';
2015-11-17 17:22:37 +01:00
char fmt_s[] = "%s";
2016-07-09 11:21:54 +02:00
curses_move(y - 4, 0);
attron(A_STANDOUT);
2015-11-17 17:22:37 +01:00
printw(fmt_s, bar);
2013-03-16 19:13:01 +02:00
attroff(A_STANDOUT);
2016-07-09 11:21:54 +02:00
curses_move(y - 3, 0);
2015-11-17 17:22:37 +01:00
printw(fmt_s, version);
pos_form_cursor(this->Form);
}
void cmCursesLongMessageForm::PrintKeys()
{
2020-02-01 23:06:01 +01:00
int x;
int y;
getmaxyx(stdscr, y, x);
2016-07-09 11:21:54 +02:00
if (x < cmCursesMainForm::MIN_WIDTH || y < cmCursesMainForm::MIN_HEIGHT) {
return;
2016-07-09 11:21:54 +02:00
}
char firstLine[512];
2020-08-30 11:54:41 +02:00
sprintf(firstLine, "Press [e] to exit screen");
2015-11-17 17:22:37 +01:00
char fmt_s[] = "%s";
2016-07-09 11:21:54 +02:00
curses_move(y - 2, 0);
2015-11-17 17:22:37 +01:00
printw(fmt_s, firstLine);
pos_form_cursor(this->Form);
}
2016-10-30 18:24:19 +01:00
void cmCursesLongMessageForm::Render(int /*left*/, int /*top*/, int /*width*/,
int /*height*/)
{
2020-02-01 23:06:01 +01:00
int x;
int y;
getmaxyx(stdscr, y, x);
2016-07-09 11:21:54 +02:00
if (this->Form) {
unpost_form(this->Form);
free_form(this->Form);
2018-01-26 17:06:56 +01:00
this->Form = nullptr;
2016-07-09 11:21:54 +02:00
}
2016-07-09 11:21:54 +02:00
if (this->Fields[0]) {
free_field(this->Fields[0]);
2018-01-26 17:06:56 +01:00
this->Fields[0] = nullptr;
2016-07-09 11:21:54 +02:00
}
2016-07-09 11:21:54 +02:00
this->Fields[0] = new_field(y - 6, x - 2, 1, 1, 0, 0);
2016-07-09 11:21:54 +02:00
field_opts_off(this->Fields[0], O_STATIC);
this->Form = new_form(this->Fields);
post_form(this->Form);
form_driver(this->Form, REQ_BEG_FIELD);
2020-08-30 11:54:41 +02:00
this->DrawMessage(this->Messages.c_str());
this->UpdateStatusBar();
touchwin(stdscr);
refresh();
}
void cmCursesLongMessageForm::DrawMessage(const char* msg) const
{
int i = 0;
while (msg[i] != '\0' && i < MAX_CONTENT_SIZE) {
2016-07-09 11:21:54 +02:00
if (msg[i] == '\n' && msg[i + 1] != '\0') {
form_driver(this->Form, REQ_NEW_LINE);
2016-07-09 11:21:54 +02:00
} else {
form_driver(this->Form, msg[i]);
}
2016-07-09 11:21:54 +02:00
i++;
}
2020-08-30 11:54:41 +02:00
if (this->Scrolling == ScrollBehavior::ScrollDown) {
form_driver(this->Form, REQ_END_FIELD);
} else {
form_driver(this->Form, REQ_BEG_FIELD);
}
}
void cmCursesLongMessageForm::HandleInput()
{
2016-07-09 11:21:54 +02:00
if (!this->Form) {
return;
2016-07-09 11:21:54 +02:00
}
char debugMessage[128];
2016-07-09 11:21:54 +02:00
for (;;) {
2020-08-30 11:54:41 +02:00
this->PrintKeys();
int key = getch();
sprintf(debugMessage, "Message widget handling input, key: %d", key);
cmCursesForm::LogMessage(debugMessage);
// quit
2016-07-09 11:21:54 +02:00
if (key == 'o' || key == 'e') {
break;
2017-07-20 19:35:53 +02:00
}
if (key == KEY_DOWN || key == ctrl('n')) {
form_driver(this->Form, REQ_SCR_FLINE);
2016-07-09 11:21:54 +02:00
} else if (key == KEY_UP || key == ctrl('p')) {
form_driver(this->Form, REQ_SCR_BLINE);
2016-07-09 11:21:54 +02:00
} else if (key == KEY_NPAGE || key == ctrl('d')) {
form_driver(this->Form, REQ_SCR_FPAGE);
2016-07-09 11:21:54 +02:00
} else if (key == KEY_PPAGE || key == ctrl('u')) {
form_driver(this->Form, REQ_SCR_BPAGE);
2016-07-09 11:21:54 +02:00
}
this->UpdateStatusBar();
2013-03-16 19:13:01 +02:00
touchwin(stdscr);
wrefresh(stdscr);
2016-07-09 11:21:54 +02:00
}
}