Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multicolumn prompts in selection mode #731

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -36,6 +36,7 @@ and since Bareos version 20 this project adheres to [Semantic Versioning](https:
- support for shorter date formats, where shorter dates are compensated with lowest value possible to make a full date [PR #707]
- added external repo bareos-contrib as subtree [PR #752]
- add "copy button" to code snippets in documentation for easy copying [PR #802]
- added multicolumn prompt selection for selection of more than 20 items [PR #731]

### Changed
. core: Make the jansson library mandatory when compiling the Bareos Director [PR #793]
Expand Down
75 changes: 67 additions & 8 deletions core/src/dird/ua_select.cc
Expand Up @@ -3,7 +3,7 @@

Copyright (C) 2001-2012 Free Software Foundation Europe e.V.
Copyright (C) 2011-2016 Planets Communications B.V.
Copyright (C) 2013-2019 Bareos GmbH & Co. KG
Copyright (C) 2013-2021 Bareos GmbH & Co. KG

This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -1069,6 +1069,59 @@ void AddPrompt(UaContext* ua, std::string&& prompt)
AddPrompt(ua, p.c_str());
}


/**
* Formats the prompts of a UaContext to be displayed in a multicolumn output
* when possible
*/
std::string FormatMulticolumnPrompts(const UaContext* ua,
const int window_width,
const int min_lines_threshold)
{
size_t max_prompt_length = 1;

const int max_prompt_index_length = std::to_string(ua->num_prompts).length();

for (int i = 1; i < ua->num_prompts; i++) {
if (strlen(ua->prompt[i]) > max_prompt_length) {
max_prompt_length = strlen(ua->prompt[i]);
}
}

const int extra_formatting_characters = 4;
const int max_formatted_prompt_length = max_prompt_length
+ max_prompt_index_length
+ extra_formatting_characters;
const int prompts_perline
= std::max(1, window_width / (max_formatted_prompt_length - 1));

std::vector<char> formatted_prompt(max_formatted_prompt_length);

std::string output{};

for (int i = 1; i < ua->num_prompts; i++) {
std::string prompt = ua->prompt[i];
if (ua->num_prompts > min_lines_threshold) {
if (i % prompts_perline == 0 || i == ua->num_prompts - 1) {
snprintf(formatted_prompt.data(), max_formatted_prompt_length,
"%*d: %s\n", max_prompt_index_length, i, prompt.c_str());
} else {
snprintf(formatted_prompt.data(), max_formatted_prompt_length,
"%*d: %-*s ", max_prompt_index_length, i, max_prompt_length,
prompt.c_str());
}
} else {
snprintf(formatted_prompt.data(), max_formatted_prompt_length,
"%*d: %s\n", max_prompt_index_length, i, prompt.c_str());
}

output += formatted_prompt.data();
}

return output;
}


/**
* Display prompts and get user's choice
*
Expand All @@ -1086,6 +1139,9 @@ int DoPrompt(UaContext* ua,
PoolMem pmsg(PM_MESSAGE);
BareosSocket* user = ua->UA_sock;

int window_width = 80;
int min_lines_threshold = 20;

if (prompt) { *prompt = 0; }
if (ua->num_prompts == 2) {
item = 1;
Expand All @@ -1100,9 +1156,8 @@ int DoPrompt(UaContext* ua,
if (ua->batch) {
// First print the choices he wanted to make
ua->SendMsg(ua->prompt[0]);
for (int i = 1; i < ua->num_prompts; i++) {
ua->SendMsg("%6d: %s\n", i, ua->prompt[i]);
}
ua->SendMsg(FormatMulticolumnPrompts(ua, window_width, min_lines_threshold)
.c_str());

// Now print error message
ua->SendMsg(_("Your request has multiple choices for \"%s\". Selection is "
Expand All @@ -1115,14 +1170,18 @@ int DoPrompt(UaContext* ua,
if (ua->api) { user->signal(BNET_START_SELECT); }

ua->SendMsg(ua->prompt[0]);
for (int i = 1; i < ua->num_prompts; i++) {
if (ua->api) {


if (ua->api) {
for (int i = 1; i < ua->num_prompts; i++) {
ua->SendMsg("%s", ua->prompt[i]);
} else {
ua->SendMsg("%6d: %s\n", i, ua->prompt[i]);
}
} else {
ua->SendMsg(FormatMulticolumnPrompts(ua, window_width, min_lines_threshold)
.c_str());
}


if (ua->api) { user->signal(BNET_END_SELECT); }

while (1) {
Expand Down
5 changes: 4 additions & 1 deletion core/src/dird/ua_select.h
@@ -1,7 +1,7 @@
/*
BAREOS® - Backup Archiving REcovery Open Sourced

Copyright (C) 2018-2020 Bareos GmbH & Co. KG
Copyright (C) 2018-2021 Bareos GmbH & Co. KG

This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
Expand Down Expand Up @@ -54,6 +54,9 @@ bool SelectClientDbr(UaContext* ua, ClientDbRecord* cr);
void StartPrompt(UaContext* ua, const char* msg);
void AddPrompt(UaContext* ua, const char* prompt);
void AddPrompt(UaContext* ua, std::string&& prompt);
std::string FormatMulticolumnPrompts(const UaContext* ua,
int window_width,
int min_lines_threshold);
int DoPrompt(UaContext* ua,
const char* automsg,
const char* msg,
Expand Down
8 changes: 8 additions & 0 deletions core/src/tests/CMakeLists.txt
Expand Up @@ -393,3 +393,11 @@ if(NOT client-only)
$<$<BOOL:HAVE_PAM>:${PAM_LIBRARIES}> GTest::gtest_main
)
endif() # NOT client-only

if(NOT client-only)
bareos_add_test(
multicolumn_prompts
LINK_LIBRARIES bareos dird_objects bareosfind bareoscats bareossql
$<$<BOOL:HAVE_PAM>:${PAM_LIBRARIES}> GTest::gtest_main
)
endif() # NOT client-only
198 changes: 198 additions & 0 deletions core/src/tests/multicolumn_prompts.cc
@@ -0,0 +1,198 @@
/*
BAREOS® - Backup Archiving REcovery Open Sourced

Copyright (C) 2021-2021 Bareos GmbH & Co. KG

This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation, which is
listed in the file LICENSE.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#if defined(HAVE_MINGW)
# include "include/bareos.h"
# include "gtest/gtest.h"
#else
# include "gtest/gtest.h"
# include "include/bareos.h"
#endif

#include "dird/ua_select.h"

using namespace directordaemon;

namespace directordaemon {
bool DoReloadConfig() { return false; }
} // namespace directordaemon

TEST(multicolumprompts, test0)
{
UaContext ua;

const char* list[] = {
_("bareos1"), _("bareos2"), _("bareos3"), _("bareos4"), _("bareos5"),
_("bareos6"), _("bareos7"), _("bareos8"), _("bareos9"), _("bareos10"),
_("bareos11"), _("bareos12"), _("bareos13"), _("bareos14"), _("bareos15"),

nullptr};

StartPrompt(&ua, "start");
for (int i = 0; list[i]; ++i) { AddPrompt(&ua, list[i]); }

std::string output = FormatMulticolumnPrompts(&ua, 80, 1);

EXPECT_STREQ(output.c_str(),
" 1: bareos1 2: bareos2 3: bareos3 4: bareos4 5: "
"bareos5 6: bareos6\n"
" 7: bareos7 8: bareos8 9: bareos9 10: bareos10 11: "
"bareos11 12: bareos12\n"
"13: bareos13 14: bareos14 15: bareos15\n");
}

TEST(multicolumprompts, test1)
{
UaContext ua;

const char* list[]
= {_("List last 20 Jobs run"),
_("List Jobs where a given File is saved"),
_("Enter list of comma separated JobIds to select"),
_("Enter SQL list command"),
_("Select the most recent backup for a client"),
_("Select backup for a client before a specified time"),
_("Enter a list of files to restore"),
_("Enter a list of files to restore before a specified time"),
_("Find the JobIds of the most recent backup for a client"),
_("Find the JobIds for a backup for a client before a specified time"),
_("Enter a list of directories to restore for found JobIds"),
_("Select full restore to a specified Job date"),
_("Cancel"),
nullptr};
StartPrompt(&ua, "start");
for (int i = 0; list[i]; ++i) { AddPrompt(&ua, list[i]); }

std::string output = FormatMulticolumnPrompts(&ua, 80, 20);

EXPECT_STREQ(
output.c_str(),
" 1: List last 20 Jobs run\n"
" 2: List Jobs where a given File is saved\n"
" 3: Enter list of comma separated JobIds to select\n"
" 4: Enter SQL list command\n"
" 5: Select the most recent backup for a client\n"
" 6: Select backup for a client before a specified time\n"
" 7: Enter a list of files to restore\n"
" 8: Enter a list of files to restore before a specified time\n"
" 9: Find the JobIds of the most recent backup for a client\n"
"10: Find the JobIds for a backup for a client before a specified time\n"
"11: Enter a list of directories to restore for found JobIds\n"
"12: Select full restore to a specified Job date\n"
"13: Cancel\n");
}

TEST(multicolumprompts, test2)
{
UaContext ua;

const char* list[]
= {_("List last 20 Jobs run"),
_("List Jobs where a given File is saved"),
_("Enter list of comma separated JobIds to select"),
_("Enter SQL list command"),
_("Select the most recent backup for a client"),
_("Select backup for a client before a specified time"),
_("Enter a list of files to restore"),
_("Enter a list of files to restore before a specified time"),
_("Find the JobIds of the most recent backup for a client"),
_("Find the JobIds for a backup for a client before a specified time"),
_("Enter a list of directories to restore for found JobIds"),
_("Select full restore to a specified Job date"),
_("Cancel"),
nullptr};
StartPrompt(&ua, "start");
for (int i = 0; list[i]; ++i) { AddPrompt(&ua, list[i]); }

std::string output = FormatMulticolumnPrompts(&ua, 5000, 1);

EXPECT_STREQ(
output.c_str(),
" 1: List last 20 Jobs run "
" 2: List Jobs where a given File is saved "
" 3: Enter list of comma separated JobIds to select "
" 4: Enter SQL list command "
" 5: Select the most recent backup for a client "
" 6: Select backup for a client before a specified time "
" 7: Enter a list of files to restore "
" 8: Enter a list of files to restore before a specified time "
" 9: Find the JobIds of the most recent backup for a client "
"10: Find the JobIds for a backup for a client before a specified time "
"11: Enter a list of directories to restore for found JobIds "
"12: Select full restore to a specified Job date "
"13: Cancel\n");
}

TEST(multicolumprompts, test3)
{
UaContext ua;

const char* list[] = {
_("bareos1"), _("bareos2"), _("bareos3"), _("bareos4"), _("bareos5"),
_("bareos6"), _("bareos7"), _("bareos8"), _("bareos9"), _("bareos10"),
_("bareos11"), _("bareos12"), _("bareos13"), _("bareos14"), _("bareos15"),

nullptr};

StartPrompt(&ua, "start");
for (int i = 0; list[i]; ++i) { AddPrompt(&ua, list[i]); }

std::string output = FormatMulticolumnPrompts(&ua, 60, 10);

EXPECT_STREQ(output.c_str(),
" 1: bareos1 2: bareos2 3: bareos3 4: bareos4\n"
" 5: bareos5 6: bareos6 7: bareos7 8: bareos8\n"
" 9: bareos9 10: bareos10 11: bareos11 12: bareos12\n"
"13: bareos13 14: bareos14 15: bareos15\n");
}

TEST(multicolumprompts, test4)
{
UaContext ua;

const char* list[] = {_(""), _("Listsaved"), _("Cancel"), nullptr};

StartPrompt(&ua, "start");
for (int i = 0; list[i]; ++i) { AddPrompt(&ua, list[i]); }

std::string output = FormatMulticolumnPrompts(&ua, 80, 1);

EXPECT_STREQ(output.c_str(),
"1: "
"2: Listsaved "
"3: Cancel\n");
}

TEST(multicolumprompts, test5)
{
UaContext ua;

const char* list[] = {_(""), _(" "), _(" "), nullptr};

StartPrompt(&ua, "start");
for (int i = 0; list[i]; ++i) { AddPrompt(&ua, list[i]); }

std::string output = FormatMulticolumnPrompts(&ua, 80, 20);

EXPECT_STREQ(output.c_str(),
"1: \n"
"2: \n"
"3: \n");
}