CleanTrailingSlash() after selecting a folder

This commit is contained in:
Jason Ly 2025-12-22 20:02:36 -05:00
parent 136df93fc5
commit 212a1882cf

View File

@ -7,6 +7,15 @@
std::string directory_path = "."; std::string directory_path = ".";
void CleanTrailingSlash(std::string& path) {
// Only strip if it's longer than a root path (e.g., "/" or "C:\")
#if defined(_WIN32)
if (path.size() > 3 && (path.back() == '/' || path.back() == '\\')) path.pop_back();
#else
if (path.size() > 1 && path.back() == '/') path.pop_back();
#endif
}
// 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;
@ -62,7 +71,7 @@ int main(int argc, char* argv[]) {
start_path = std::filesystem::absolute(argv[1]); start_path = std::filesystem::absolute(argv[1]);
// Convert to string // Convert to string
start_path = start_path.lexically_normal(); start_path = start_path.lexically_normal();
std::string path_str = start_path.string(); std::string path_str = start_path.string();
// Manually remove trailing slash if it exists and isn't the root directory // Manually remove trailing slash if it exists and isn't the root directory
if (path_str.size() > 1 && (path_str.back() == '/' || path_str.back() == '\\')) { if (path_str.size() > 1 && (path_str.back() == '/' || path_str.back() == '\\')) {
@ -135,9 +144,12 @@ int main(int argc, char* argv[]) {
} }
new_path = std::filesystem::absolute(new_path).lexically_normal(); new_path = std::filesystem::absolute(new_path).lexically_normal();
new_path.make_preferred(); new_path.make_preferred();
current_path = new_path.string(); current_path = new_path.string();
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))
screen.Exit(); // Exit the application screen.Exit(); // Exit the application
@ -158,7 +170,7 @@ int main(int argc, char* argv[]) {
// so the Menu can handle it (like arrow keys) // so the Menu can handle it (like arrow keys)
return false; return false;
}); });
std::cout << "Initial current_path: " << current_path << std::endl;
// Create a renderer that defines the layout of your application // Create a renderer that defines the layout of your application
auto main_renderer = ftxui::Renderer(menu_with_event_handler, [&] { auto main_renderer = ftxui::Renderer(menu_with_event_handler, [&] {
return ftxui::vbox({ return ftxui::vbox({