display file size without termination

This commit is contained in:
Jason Ly 2025-12-27 20:27:17 -05:00
parent 3a22c19fa8
commit 0d4e0e4db8

View File

@ -6,6 +6,7 @@
#include <filesystem>
std::string directory_path = ".";
int selected_index = -1; // Global variable to hold the selected index
void CleanTrailingSlash(std::string& path) {
// Only strip if it's longer than a root path (e.g., "/" or "C:\")
@ -53,19 +54,27 @@ void ReloadDirectory(ftxui::ScreenInteractive& screen, const std::filesystem::pa
entries.clear();
entries.push_back("0. .. (go up one level)"); // Option to go up one level
clean_names.push_back("..");
for (const auto& entry : std::filesystem::directory_iterator(new_path)) {
if (std::filesystem::is_directory(new_path)) {
// Append slash to directories for clarity
for (const auto& entry : std::filesystem::directory_iterator(new_path)) {
if (std::filesystem::is_directory(entry.path()) || std::filesystem::is_regular_file(entry.path())) {
if (std::filesystem::is_directory(entry.path()) || std::filesystem::is_regular_file(entry.path())) {
std::string name = entry.path().filename().string();
if (!name.empty() && name[0] == '.') // Works in C++11, C++14, and C++17
continue; // Skip hidden files and directories
if (std::filesystem::is_directory(entry.path())) {
name += "/"; // Append slash to directories for clarity
}
entries.push_back(std::to_string(i) + ". " + name);
clean_names.push_back(name);
i++;
std::string name = entry.path().filename().string();
if (!name.empty() && name[0] == '.') // Works in C++11, C++14, and C++17
continue; // Skip hidden files and directories
if (std::filesystem::is_directory(entry.path())) {
name += "/"; // Append slash to directories for clarity
}
if (std::filesystem::is_regular_file(entry.path()) && i == selected_index) {
// Optionally, append file size
auto size = std::filesystem::file_size(entry.path());
name += " (" + FormatSize(size) + ")";
}
entries.push_back(std::to_string(i) + ". " + name);
clean_names.push_back(name);
i++;
}
}
}
}
@ -161,17 +170,22 @@ int main(int argc, char* argv[]) {
new_path.make_preferred();
current_path = new_path.string();
CleanTrailingSlash(current_path);
directory_path = current_path; // Update global variable
if (std::filesystem::is_regular_file(new_path))
{
selected_index = selected; // Update global selected index
final_selected_index = selected; // Store the final selection index
screen.Exit(); // Exit the application
// screen.Exit(); // Exit the application
}
else
{
current_path = new_path.string();
}
// Call the function to switch and reload
ReloadDirectory(screen, new_path, entries,clean_names);
ReloadDirectory(screen, current_path, entries, clean_names);
selected = 0; // Reset selection to the first item
return true; // Event handled
@ -228,12 +242,6 @@ int main(int argc, char* argv[]) {
std::cout<< "directory path: " << directory_path << std::endl;
if (final_selected_index >= 0 && final_selected_index < entries.size()) {
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;
}