Skip to content

Commit

Permalink
make file message file size readable
Browse files Browse the repository at this point in the history
  • Loading branch information
Green-Sky committed Jun 27, 2024
1 parent 780e1e0 commit 37239f1
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/chat_gui4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,28 @@ static constexpr float lerp(float a, float b, float t) {
return a + t * (b - a);
}

// returns divider and places static suffix string into suffix_out
static int64_t sizeToHumanReadable(int64_t file_size, const char*& suffix_out) {
static const char* suffix_arr[] {
"bytes",
"KiB",
"MiB",
"GiB",
"TiB",
"PiB",
// TODO: fix upper bound behaviour
};
int64_t divider = 1024;
for (size_t ij = 0; ij < std::size(suffix_arr); ij++, divider *= 1024) {
if (file_size < divider) {
suffix_out = suffix_arr[ij];
break;
}
}

return (divider > 1024) ? (divider / 1024) : 1;
}

static std::string file_path_url_escape(const std::string&& value) {
std::ostringstream escaped;

Expand Down Expand Up @@ -1055,8 +1077,16 @@ void ChatGui4::renderMessageBodyFile(Message3Registry& reg, const Message3 e) {
for (size_t i = 0; i < file_list.size(); i++) {
ImGui::PushID(i);

const char* byte_suffix = "???";
int64_t byte_divider = sizeToHumanReadable(file_list[i].file_size, byte_suffix);

// TODO: selectable text widget ?
ImGui::Bullet(); ImGui::Text("%s (%lu)", file_list[i].file_name.c_str(), file_list[i].file_size);
ImGui::Bullet(); ImGui::Text("%s (%.2lf %s)", file_list[i].file_name.c_str(), double(file_list[i].file_size)/byte_divider, byte_suffix);
if (ImGui::BeginItemTooltip()) {
ImGui::Text("TODO: file path?");
ImGui::Text("%lu bytes", file_list[i].file_size);
ImGui::EndTooltip();
}

if (reg.all_of<Message::Components::Transfer::FileInfoLocal>(e)) {
const auto& local_info = reg.get<Message::Components::Transfer::FileInfoLocal>(e);
Expand Down

0 comments on commit 37239f1

Please sign in to comment.