Skip to content

Commit

Permalink
attempt to fix progress bar. it seems that spdmon fails for small ter…
Browse files Browse the repository at this point in the history
…minals
  • Loading branch information
amock committed Dec 19, 2024
1 parent 17e3ffd commit c81c495
Showing 1 changed file with 41 additions and 28 deletions.
69 changes: 41 additions & 28 deletions ext/spdmon/include/spdmon/spdmon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ namespace spdmon
else
{
for (size_t i = 0; i < n; i++)
{
std::copy_n(bar_syms_[idx], len_sym, it);
}
}
};

Expand Down Expand Up @@ -191,7 +193,7 @@ namespace spdmon
}
}

void RenderProgress(timepoint_t now, unsigned int width, fmt::memory_buffer &buf)
bool RenderProgress(timepoint_t now, unsigned int width, fmt::memory_buffer &buf)
{
last_print_n_ = n_;
//const auto elapsed = now - started_at_;
Expand All @@ -201,7 +203,7 @@ namespace spdmon
fmt::format_to(
fmt::appender(buf), fmt::runtime(kNoTotalFmt), fmt::arg("desc", desc_), fmt::arg("n", n_), fmt::arg("elapsed", elapsed),
fmt::arg("eol", kTermEol));
return;
return true;
}

const float frac = static_cast<float>(n_.load()) / static_cast<float>(total_);
Expand All @@ -210,29 +212,35 @@ namespace spdmon
fmt::memory_buffer right;

const float percent = frac * 100;
try
{
fmt::format_to(fmt::appender(buf), fmt::runtime(kLbarFmt), fmt::arg("desc", desc_), fmt::arg("frac", percent));

fmt::format_to(
fmt::appender(right), fmt::runtime(kRbarFmt), fmt::arg("n", n_), fmt::arg("total", total_), fmt::arg("elapsed", elapsed),
fmt::arg("remaining", remaining), fmt::arg("eol", kTermEol));
fmt::format_to(fmt::appender(buf), fmt::runtime(kLbarFmt), fmt::arg("desc", desc_), fmt::arg("frac", percent));

const auto space_for_bar = static_cast<unsigned int>(width - buf.size() - right.size() + kTermEol.size());
fmt::format_to(
fmt::appender(right), fmt::runtime(kRbarFmt), fmt::arg("n", n_), fmt::arg("total", total_), fmt::arg("elapsed", elapsed),
fmt::arg("remaining", remaining), fmt::arg("eol", kTermEol));

if (space_for_bar > 0) {
FormatBarTo(buf, space_for_bar, frac);
}

const int space_for_bar = static_cast<int>(width) - static_cast<int>(buf.size()) - static_cast<int>(right.size());

if (space_for_bar > 0)
{
FormatBarTo(buf, space_for_bar, frac);
buf.reserve(buf.size() + right.size());
std::copy(right.begin(), right.end(), std::back_inserter(buf));
}
catch (...)
{
// In rare cases a format exception might happen.
// We currently just ignore it as it will only
// cause a missing update in the status information
} else {
buf.clear();
// fmt::format_to(fmt::appender(buf), "Enlarge terminal to see progress!");

// std::cout << "Stats:" << std::endl;
// std::cout << "- width: " << width << std::endl;
// std::cout << "- buf: " << buf.size() << std::endl;
// std::cout << "- right: " << right.size() << std::endl;
// std::cout << "- space for bar: " << space_for_bar << std::endl;

return false;
}

return true;
}

virtual void ShowProgress(timepoint_t now = clock_t::now()) = 0;
Expand Down Expand Up @@ -294,22 +302,27 @@ namespace spdmon

void ShowProgress(timepoint_t now = clock_t::now()) final
{
// The performance is not too bad checking this all the time
UpdateTermWidth();
// called from BaseProgress when it decided the spdmon bar needs
// to be printed again
try {
fmt::memory_buffer buf;
RenderProgress(now, width_, buf);
std::copy(kTermMoveUp.begin(), kTermMoveUp.end(), std::back_inserter(buf));
fwrite(buf.data(), 1, buf.size(), file_);
fflush(file_);
}
catch(...)
fmt::memory_buffer buf;

if(RenderProgress(now, width_, buf))
{
std::cout << "ShowProgress" << std::endl;
std::copy(kTermMoveUp.begin(), kTermMoveUp.end(), std::back_inserter(buf));
} else {
// TODO: write this message instead
// doesnt work right now bc the terminal prints with line breaks
// buf.clear();
// fmt::format_to(fmt::appender(buf), "Enlarge terminal to see progress!");
}

fwrite(buf.data(), 1, buf.size(), file_);
fflush(file_);

}


void Flush()
{
fmt::memory_buffer buf;
Expand Down

0 comments on commit c81c495

Please sign in to comment.