format file size

This commit is contained in:
Jason Ly 2025-12-27 19:32:03 -05:00
parent b68f69f1e3
commit 3a22c19fa8

View File

@ -16,6 +16,20 @@ void CleanTrailingSlash(std::string& path) {
#endif #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<double>(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. // Note: You must pass the ScreenInteractive object to trigger a redraw.
void ReloadDirectory(ftxui::ScreenInteractive& screen, const std::filesystem::path& new_path, std::vector<std::string>& entries, std::vector<std::string>& clean_names) { void ReloadDirectory(ftxui::ScreenInteractive& screen, const std::filesystem::path& new_path, std::vector<std::string>& entries, std::vector<std::string>& clean_names) {
if (!std::filesystem::exists(new_path) || !std::filesystem::is_directory(new_path)) return; 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 << "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 << "Full Path: " << std::filesystem::path(directory_path) / clean_names[final_selected_index] << std::endl;
std::cout << "current_path: " << current_path << 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 << "file size: " << std::filesystem::file_size(std::filesystem::path(current_path)) << " bytes" << std::endl;
std::cout << "Formatted Size: " << FormatSize(size) << std::endl;
} else { } else {
std::cout << "Selection cancelled or no valid entry selected." << std::endl; std::cout << "Selection cancelled or no valid entry selected." << std::endl;
} }