Skip to content

Commit

Permalink
dir: added multi column prompts to selection
Browse files Browse the repository at this point in the history
- bconsole now shows selection options in multiple columns for selections of more than 20 options
  • Loading branch information
alaaeddineelamri committed Mar 17, 2021
1 parent 85a2c52 commit 0d0d96f
Show file tree
Hide file tree
Showing 4 changed files with 259 additions and 7 deletions.
70 changes: 63 additions & 7 deletions core/src/dird/ua_select.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,56 @@ 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 windowsize,
const int min_lines_threshold)
{
unsigned int max_prompt_length = 1;
int prompts_perline = 1;
int max_line_length = 1;

int prompt_max_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]);
}
}
max_line_length = max_prompt_length + prompt_max_index_length;
prompts_perline = (windowsize / max_line_length) - 1;
if (prompts_perline == 0) { prompts_perline = 1; }


max_line_length += 4;
char formatted_prompt[max_line_length];

std::string output("");

for (int i = 1; i < ua->num_prompts; i++) {
if (ua->num_prompts > min_lines_threshold) {
if (i % prompts_perline == 0 || i == ua->num_prompts - 1) {
Bsnprintf(formatted_prompt, max_line_length, "%*d: %s\n",
prompt_max_index_length, i, ua->prompt[i]);
} else {
Bsnprintf(formatted_prompt, max_line_length, "%*d: %-*s ",
prompt_max_index_length, i, max_prompt_length, ua->prompt[i]);
}
} else {
Bsnprintf(formatted_prompt, max_line_length, "%*d: %s\n",
prompt_max_index_length, i, ua->prompt[i]);
}

output += formatted_prompt;
}

return output;
}

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

int windowsize = 80;
int min_lines_threshold = 20;

if (prompt) { *prompt = 0; }
if (ua->num_prompts == 2) {
item = 1;
Expand All @@ -1142,9 +1195,8 @@ int DoPrompt(UaContext* ua,
* 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, windowsize, min_lines_threshold).c_str());

/*
* Now print error message
Expand All @@ -1159,14 +1211,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, windowsize, min_lines_threshold).c_str());
}


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

while (1) {
Expand Down
3 changes: 3 additions & 0 deletions core/src/dird/ua_select.h
Original file line number Diff line number Diff line change
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,
const int windowsize,
const int min_lines_threshold);
int DoPrompt(UaContext* ua,
const char* automsg,
const char* msg,
Expand Down
16 changes: 16 additions & 0 deletions core/src/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -502,3 +502,19 @@ if(NOT client-only)
${GTEST_MAIN_LIBRARIES}
)
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}>
${LMDB_LIBS}
${GTEST_LIBRARIES}
${GTEST_MAIN_LIBRARIES}
)
endif() # NOT client-only
177 changes: 177 additions & 0 deletions core/src/tests/multicolumn_prompts.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2007-2011 Free Software Foundation Europe e.V.
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2019-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, 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 5: bareos5\n"
" 6: bareos6 7: bareos7 8: bareos8 9: bareos9 10: bareos10\n"
"11: bareos11 12: bareos12 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");
}

0 comments on commit 0d0d96f

Please sign in to comment.