From 3a22c19fa8d83467b91df3d00e12250c6d0f927a Mon Sep 17 00:00:00 2001 From: Jason Ly Date: Sat, 27 Dec 2025 19:32:03 -0500 Subject: [PATCH] format file size --- joomer-ftxui-file-browser.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/joomer-ftxui-file-browser.cpp b/joomer-ftxui-file-browser.cpp index 6da5076..effd572 100644 --- a/joomer-ftxui-file-browser.cpp +++ b/joomer-ftxui-file-browser.cpp @@ -16,6 +16,20 @@ void CleanTrailingSlash(std::string& path) { #endif } +std::string FormatSize(uintmax_t size) { + if (size == 0) return "0 B"; + const char* units[] = { "B", "KB", "MB", "GB", "TB" }; + int i = 0; + double d_size = static_cast(size); + while (d_size >= 1024 && i < 4) { + d_size /= 1024; + i++; + } + std::stringstream ss; + ss << std::fixed << std::setprecision(1) << d_size << " " << units[i]; + return ss.str(); +} + // Note: You must pass the ScreenInteractive object to trigger a redraw. void ReloadDirectory(ftxui::ScreenInteractive& screen, const std::filesystem::path& new_path, std::vector& entries, std::vector& clean_names) { if (!std::filesystem::exists(new_path) || !std::filesystem::is_directory(new_path)) return; @@ -216,7 +230,10 @@ int main(int argc, char* argv[]) { std::cout << "Selected Entry: " << entries[final_selected_index] << std::endl; std::cout << "Full Path: " << std::filesystem::path(directory_path) / clean_names[final_selected_index] << std::endl; std::cout << "current_path: " << current_path << std::endl; + // Get file size and format it + auto size = std::filesystem::file_size(current_path); std::cout << "file size: " << std::filesystem::file_size(std::filesystem::path(current_path)) << " bytes" << std::endl; + std::cout << "Formatted Size: " << FormatSize(size) << std::endl; } else { std::cout << "Selection cancelled or no valid entry selected." << std::endl; }