mirror of
https://github.com/eried/portapack-mayhem.git
synced 2025-08-13 17:05:37 -04:00
Progress bar for Notepad IO (#1322)
This commit is contained in:
parent
0a3aa706ef
commit
411f6c0a34
3 changed files with 73 additions and 3 deletions
|
@ -26,6 +26,7 @@
|
|||
#include "file.hpp"
|
||||
#include "optional.hpp"
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string_view>
|
||||
|
||||
|
@ -75,6 +76,8 @@ class BufferWrapper {
|
|||
}
|
||||
virtual ~BufferWrapper() {}
|
||||
|
||||
std::function<void(Size, Size)> on_read_progress{};
|
||||
|
||||
/* Prevent copies */
|
||||
BufferWrapper(const BufferWrapper&) = delete;
|
||||
BufferWrapper& operator=(const BufferWrapper&) = delete;
|
||||
|
@ -234,13 +237,23 @@ class BufferWrapper {
|
|||
|
||||
line_count_ = start_line_;
|
||||
Offset offset = start_offset_;
|
||||
|
||||
// Report progress every N lines.
|
||||
constexpr auto report_interval = 100u;
|
||||
auto result = next_newline(offset);
|
||||
auto next_report = report_interval;
|
||||
|
||||
while (result) {
|
||||
++line_count_;
|
||||
if (newlines_.size() < max_newlines)
|
||||
newlines_.push_back(*result);
|
||||
offset = *result + 1;
|
||||
|
||||
if (on_read_progress && line_count_ > next_report) {
|
||||
on_read_progress(offset, size());
|
||||
next_report = line_count_ + report_interval;
|
||||
}
|
||||
|
||||
result = next_newline(offset);
|
||||
}
|
||||
}
|
||||
|
@ -397,6 +410,9 @@ class BufferWrapper {
|
|||
// Number of bytes left to shift.
|
||||
Offset remaining = size() - src;
|
||||
Offset offset = size();
|
||||
Size report_total = remaining;
|
||||
Size report_interval = report_total / 8;
|
||||
Size next_report = remaining - report_interval;
|
||||
|
||||
while (remaining > 0) {
|
||||
offset -= std::min(remaining, buffer_size);
|
||||
|
@ -413,6 +429,11 @@ class BufferWrapper {
|
|||
break;
|
||||
|
||||
remaining -= *result;
|
||||
|
||||
if (on_read_progress && remaining <= next_report) {
|
||||
on_read_progress(report_total - remaining, report_total);
|
||||
next_report = remaining > report_interval ? remaining - report_interval : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -424,6 +445,9 @@ class BufferWrapper {
|
|||
|
||||
char buffer[buffer_size];
|
||||
auto offset = src;
|
||||
Size report_total = size();
|
||||
Size report_interval = report_total / 8;
|
||||
Size next_report = offset + report_interval;
|
||||
|
||||
while (true) {
|
||||
wrapped_->seek(offset);
|
||||
|
@ -438,6 +462,11 @@ class BufferWrapper {
|
|||
break;
|
||||
|
||||
offset += *result;
|
||||
|
||||
if (on_read_progress && offset >= next_report) {
|
||||
on_read_progress(offset, report_total);
|
||||
next_report = offset + report_interval;
|
||||
}
|
||||
}
|
||||
|
||||
// Delete the extra bytes at the end of the file.
|
||||
|
@ -463,13 +492,19 @@ class FileWrapper : public BufferWrapper<File, 64> {
|
|||
template <typename T>
|
||||
using Result = File::Result<T>;
|
||||
using Error = File::Error;
|
||||
static Result<std::unique_ptr<FileWrapper>> open(const std::filesystem::path& path, bool create = false) {
|
||||
static Result<std::unique_ptr<FileWrapper>> open(
|
||||
const std::filesystem::path& path,
|
||||
bool create = false,
|
||||
std::function<void(Size, Size)> on_read_progress = nullptr) {
|
||||
auto fw = std::unique_ptr<FileWrapper>(new FileWrapper());
|
||||
auto error = fw->file_.open(path, /*read_only*/ false, create);
|
||||
|
||||
if (error)
|
||||
return *error;
|
||||
|
||||
if (on_read_progress)
|
||||
fw->on_read_progress = on_read_progress;
|
||||
|
||||
fw->initialize();
|
||||
return fw;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue