display file size without termination
This commit is contained in:
parent
3a22c19fa8
commit
0d4e0e4db8
@ -6,6 +6,7 @@
|
|||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
|
||||||
std::string directory_path = ".";
|
std::string directory_path = ".";
|
||||||
|
int selected_index = -1; // Global variable to hold the selected index
|
||||||
|
|
||||||
void CleanTrailingSlash(std::string& path) {
|
void CleanTrailingSlash(std::string& path) {
|
||||||
// Only strip if it's longer than a root path (e.g., "/" or "C:\")
|
// Only strip if it's longer than a root path (e.g., "/" or "C:\")
|
||||||
@ -53,6 +54,8 @@ void ReloadDirectory(ftxui::ScreenInteractive& screen, const std::filesystem::pa
|
|||||||
entries.clear();
|
entries.clear();
|
||||||
entries.push_back("0. .. (go up one level)"); // Option to go up one level
|
entries.push_back("0. .. (go up one level)"); // Option to go up one level
|
||||||
clean_names.push_back("..");
|
clean_names.push_back("..");
|
||||||
|
if (std::filesystem::is_directory(new_path)) {
|
||||||
|
// Append slash to directories for clarity
|
||||||
for (const auto& entry : std::filesystem::directory_iterator(new_path)) {
|
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())) {
|
||||||
@ -63,12 +66,18 @@ void ReloadDirectory(ftxui::ScreenInteractive& screen, const std::filesystem::pa
|
|||||||
if (std::filesystem::is_directory(entry.path())) {
|
if (std::filesystem::is_directory(entry.path())) {
|
||||||
name += "/"; // Append slash to directories for clarity
|
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);
|
entries.push_back(std::to_string(i) + ". " + name);
|
||||||
clean_names.push_back(name);
|
clean_names.push_back(name);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
// Define a variable to hold the final selection index
|
// Define a variable to hold the final selection index
|
||||||
@ -161,17 +170,22 @@ int main(int argc, char* argv[]) {
|
|||||||
|
|
||||||
new_path.make_preferred();
|
new_path.make_preferred();
|
||||||
|
|
||||||
current_path = new_path.string();
|
|
||||||
|
|
||||||
CleanTrailingSlash(current_path);
|
CleanTrailingSlash(current_path);
|
||||||
directory_path = current_path; // Update global variable
|
directory_path = current_path; // Update global variable
|
||||||
if (std::filesystem::is_regular_file(new_path))
|
if (std::filesystem::is_regular_file(new_path))
|
||||||
{
|
{
|
||||||
|
selected_index = selected; // Update global selected index
|
||||||
final_selected_index = selected; // Store the final selection 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
|
// 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
|
selected = 0; // Reset selection to the first item
|
||||||
|
|
||||||
return true; // Event handled
|
return true; // Event handled
|
||||||
@ -228,12 +242,6 @@ int main(int argc, char* argv[]) {
|
|||||||
std::cout<< "directory path: " << directory_path << std::endl;
|
std::cout<< "directory path: " << directory_path << std::endl;
|
||||||
if (final_selected_index >= 0 && final_selected_index < entries.size()) {
|
if (final_selected_index >= 0 && final_selected_index < entries.size()) {
|
||||||
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 << "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 {
|
} else {
|
||||||
std::cout << "Selection cancelled or no valid entry selected." << std::endl;
|
std::cout << "Selection cancelled or no valid entry selected." << std::endl;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user