Skip to content

Commit

Permalink
add multiline prints
Browse files Browse the repository at this point in the history
  • Loading branch information
PA055 committed Jul 6, 2024
1 parent 599d3c1 commit 20138bb
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/gamepad/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,11 @@ void Controller::add_alert(uint8_t line, std::string str, uint32_t duration) {
TODO("warn instead of throw error if there are too many lines")
if (std::ranges::count(str, '\n') > 2) std::exit(1);

std::vector<std::string> strs = {"", "", ""};
std::vector<std::string> strs(3);
std::stringstream ss(str);
std::string to;

for (int i = line; i < 3; i++) {
if (!std::getline(ss, to, '\n')) break;
strs[i] = std::move(to);
if (!std::getline(ss, strs[i], '\n')) break;
}

add_alerts(strs, duration);
Expand All @@ -149,15 +147,12 @@ void Controller::add_alerts(std::vector<std::string> strs, uint32_t duration) {
TODO("change handling for too many lines")
if (strs.size() > 3) std::exit(1);


// get nex available time slot for all lines
uint minSpot = -1; // max uint value
for (uint8_t line = 0; line < 3; line++) {

if (getTotalDuration(line) < minSpot)
minSpot = getTotalDuration(line);
}
for (uint8_t line = 0; line < 3; line++)
minSpot = std::min(minSpot, getTotalDuration(line));

// Schedule alerts
for (int i = 0; i < 3; i++) {
if (getTotalDuration(i) < minSpot)
this->screen_buffer[i].push_back({ .text = "", .duration = (minSpot - getTotalDuration(i)) });
Expand All @@ -169,6 +164,22 @@ void Controller::print_line(uint8_t line, std::string str) {
TODO("change handling for off screen lines")
if (line > 2) std::exit(1);

if (str.find('\n') != std::string::npos) {
TODO("warn instead of throw error if there are too many lines")
if (std::ranges::count(str, '\n') > 2) std::exit(1);

std::vector<std::string> strs(3);
std::stringstream ss(str);

for (int i = line; i < 3; i++) {
if (!std::getline(ss, strs[i], '\n')) break;
}

for (uint8_t l = 0; l < 3; l++)
this->next_print[l] = std::move(strs[l]);
return;
}

this->next_print[line] = std::move(str);
}

Expand Down

0 comments on commit 20138bb

Please sign in to comment.