Skip to content

Commit

Permalink
Cleanup in console code
Browse files Browse the repository at this point in the history
  • Loading branch information
wichtounet committed Sep 27, 2023
1 parent 519d663 commit 8d281d7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
46 changes: 24 additions & 22 deletions src/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ size_t budget::rsize(const std::string& value) {
v = v.substr(7);
}

static wchar_t buf[1025];
return mbstowcs(buf, v.c_str(), 1024);
static std::array<wchar_t, 1025> buf;
return mbstowcs(buf.data(), v.c_str(), 1024);
}

size_t budget::rsize_after(const std::string& value) {
Expand All @@ -80,8 +80,8 @@ size_t budget::rsize_after(const std::string& value) {
index = v.find('\033');
}

static wchar_t buf[1025];
return mbstowcs(buf, v.c_str(), 1024);
static std::array<wchar_t, 1025> buf;
return mbstowcs(buf.data(), v.c_str(), 1024);
}

bool budget::option(std::string_view option, std::vector<std::string>& args) {
Expand Down Expand Up @@ -159,32 +159,34 @@ std::string budget::get_string_complete(const std::vector<std::string>& choices)
const char c = getch();

if (+c == 127) {
if (!answer.empty()) {
std::cout << "\b \b";
answer.pop_back();
if (answer.empty()) {
continue;
}

std::cout << "\b \b";
answer.pop_back();
} else if (c == '\r' || c == '\n') {
std::cout << std::endl;
return answer;
} else if (c == '\t') {
if (!answer.empty()) {
size_t count = 0;
std::string valid;

for (const auto& choice : choices) {
if (choice.size() > answer.size()) {
if (choice.starts_with(answer)) {
++count;
valid = choice;
}
}
}
if (answer.empty()) {
continue;
}

if (count == 1) {
std::cout << valid.substr(answer.size(), valid.size());
answer = valid;
size_t count = 0;
std::string valid;

for (const auto& choice : choices) {
if (choice.size() > answer.size() && choice.starts_with(answer)) {
++count;
valid = choice;
}
}

if (count == 1) {
std::cout << valid.substr(answer.size(), valid.size());
answer = valid;
}
} else if (c == '\033') {
getch();

Expand Down
4 changes: 2 additions & 2 deletions src/console_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace {

std::string success_to_string(int success) {
std::string success_to_string(unsigned long success) {
std::stringstream ss;

if (success < 25) {
Expand All @@ -29,7 +29,7 @@ std::string success_to_string(int success) {
budget::print_minimum(ss, success, 5);
ss << "%\033[0m ";

success = std::min(success, 109);
success = std::min(success, 109UL);
const size_t good = success == 0 ? 0 : (success / 10) + 1;

for (size_t i = 0; i < good; ++i) {
Expand Down

0 comments on commit 8d281d7

Please sign in to comment.