Skip to content

Commit

Permalink
Merge pull request #1072
Browse files Browse the repository at this point in the history
console: multicolumn output: fill columns first
  • Loading branch information
pstorz committed Mar 18, 2022
2 parents 9c4a806 + 1e362b9 commit d15583f
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 94 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ and since Bareos version 20 this project adheres to [Semantic Versioning](https:
- Consolidation now purges candidate jobs with no files instead of ignoring them [PR #1056]
- Virtual Full will now terminate if one if the input jobs had its files pruned [PR #1070]
- webui: new login screen background and adapted logo to support Ukraine [PR #1122]
- console: multicolumn output: fill columns first [PR #1072]

### Deprecated

Expand Down Expand Up @@ -95,6 +96,7 @@ and since Bareos version 20 this project adheres to [Semantic Versioning](https:
[PR #1062]: https://github.com/bareos/bareos/pull/1062
[PR #1067]: https://github.com/bareos/bareos/pull/1067
[PR #1070]: https://github.com/bareos/bareos/pull/1070
[PR #1072]: https://github.com/bareos/bareos/pull/1072
[PR #1079]: https://github.com/bareos/bareos/pull/1079
[PR #1082]: https://github.com/bareos/bareos/pull/1082
[PR #1083]: https://github.com/bareos/bareos/pull/1083
Expand Down
66 changes: 44 additions & 22 deletions core/src/dird/ua_select.cc
Original file line number Diff line number Diff line change
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-2021 Bareos GmbH & Co. KG
Copyright (C) 2013-2022 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 @@ -1093,14 +1093,13 @@ 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)
std::string FormatPrompts(const UaContext* ua,
const int window_width,
const int min_lines_threshold)
{
unsigned int max_prompt_length = 1;

Expand All @@ -1112,34 +1111,59 @@ std::string FormatMulticolumnPrompts(const UaContext* ua,
}
}

const int extra_formatting_characters = 4;
const short int colon_after_index = 1;
const short int space_between_colon_and_prompt = 1;
const short int space_after_prompt = 1;
const short int extra_character_room_for_snprintf = 1;

constexpr const short int extra_formatting_characters
= colon_after_index + space_between_colon_and_prompt + space_after_prompt
+ extra_character_room_for_snprintf;

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));

const int number_output_lines
= (ua->num_prompts + prompts_perline - 1) / prompts_perline;

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

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 {
if (ua->num_prompts > min_lines_threshold
&& window_width > max_formatted_prompt_length * 2) {
for (int i = 1; i < ua->num_prompts; i++) {
{
snprintf(formatted_prompt.data(), max_formatted_prompt_length,
"%*d: %-*s ", max_prompt_index_length, i, max_prompt_length,
prompt.c_str());
ua->prompt[i]);
}
} else {
snprintf(formatted_prompt.data(), max_formatted_prompt_length,
"%*d: %s\n", max_prompt_index_length, i, prompt.c_str());

formatted_prompts_container.push_back(formatted_prompt);
}

output += formatted_prompt.data();
int index = 0;
for (int i = 0; i < number_output_lines; i++) {
index = i;
while (static_cast<size_t>(index) < formatted_prompts_container.size()) {
output.append(formatted_prompts_container[index].data());
index += number_output_lines;
}

output.append("\n");
}
} else {
for (int i = 1; i < ua->num_prompts; i++) {
{
snprintf(formatted_prompt.data(), max_formatted_prompt_length,
"%*d: %s\n", max_prompt_index_length, i, ua->prompt[i]);
output.append(formatted_prompt.data());
}
}
}

return output;
Expand Down Expand Up @@ -1180,8 +1204,7 @@ int DoPrompt(UaContext* ua,
if (ua->batch) {
// First print the choices he wanted to make
ua->SendMsg(ua->prompt[0]);
ua->SendMsg(FormatMulticolumnPrompts(ua, window_width, min_lines_threshold)
.c_str());
ua->SendMsg(FormatPrompts(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 @@ -1201,8 +1224,7 @@ int DoPrompt(UaContext* ua,
ua->SendMsg("%s", ua->prompt[i]);
}
} else {
ua->SendMsg(FormatMulticolumnPrompts(ua, window_width, min_lines_threshold)
.c_str());
ua->SendMsg(FormatPrompts(ua, window_width, min_lines_threshold).c_str());
}


Expand Down
8 changes: 4 additions & 4 deletions core/src/dird/ua_select.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2018-2021 Bareos GmbH & Co. KG
Copyright (C) 2018-2022 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 @@ -59,9 +59,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);
std::string FormatPrompts(const UaContext* ua,
int window_width,
int min_lines_threshold);
int DoPrompt(UaContext* ua,
const char* automsg,
const char* msg,
Expand Down

0 comments on commit d15583f

Please sign in to comment.