From 7a07a7eedce6b6e00d0449e53230a312f9f4be28 Mon Sep 17 00:00:00 2001 From: Jason Ly Date: Mon, 22 Dec 2025 18:47:09 -0500 Subject: [PATCH] clean up code more --- joomer-ftxui-file-browser.cpp | 75 ++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 36 deletions(-) diff --git a/joomer-ftxui-file-browser.cpp b/joomer-ftxui-file-browser.cpp index bb03c18..aa25dd2 100644 --- a/joomer-ftxui-file-browser.cpp +++ b/joomer-ftxui-file-browser.cpp @@ -37,6 +37,9 @@ void ReloadDirectory(ftxui::ScreenInteractive& screen, const std::filesystem::pa 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++; @@ -49,33 +52,37 @@ int main(int argc, char* argv[]) { int final_selected_index = -1; // Use -1 to indicate no selection was made std::vector entries; std::vector clean_names; // New vector for logic + std::string current_path = std::filesystem::current_path().string(); std::filesystem::path start_path = std::filesystem::current_path(); if (argc > 1) { - //directory_path = argv[1]; // Convert relative input (like "../../..") into a full absolute path - std::cout << "argv[1]: " << argv[1] << std::endl; - start_path = std::filesystem::absolute(argv[1]).lexically_normal(); - directory_path = start_path.string(); // Update global - std::cout << "directory_path: " << directory_path << std::endl; - current_path = directory_path; // Sync local - std::cout << "current_path: " << current_path << std::endl; - //current_path = directory_path.string(); - //current_path = directory_path; - } - std::cout << "Initial current_path after argc: " << current_path << std::endl; + start_path = std::filesystem::absolute(argv[1]); + + // Convert to string + start_path = start_path.lexically_normal(); + std::string path_str = start_path.string(); + // Manually remove trailing slash if it exists and isn't the root directory + if (path_str.size() > 1 && (path_str.back() == '/' || path_str.back() == '\\')) { + path_str.pop_back(); + } + + current_path = path_str; + + } + + directory_path = current_path; // Update global + // Check if the directory exists if (!std::filesystem::exists(directory_path) || !std::filesystem::is_directory(directory_path)) { std::cerr << "Error: Directory '" << directory_path << "' not found or is not a directory." << std::endl; return 1; } - auto screen = ftxui::ScreenInteractive::TerminalOutput(); - std::cout << "Initial current_path before reload: " << current_path << std::endl; - ReloadDirectory(screen, start_path, entries, clean_names); - std::cout << "Initial current_path after reload: " << current_path << std::endl; + auto screen = ftxui::ScreenInteractive::TerminalOutput(); + ReloadDirectory(screen, start_path, entries, clean_names); int selected = 0; auto menu = ftxui::Menu({ .entries = &entries, @@ -90,8 +97,7 @@ int main(int argc, char* argv[]) { if (event.is_character()) { char c = event.character()[0]; if (std::isdigit(c)) { - input_buffer += c; // Add the digit to our "typing" buffer - //std::cout << "Typed so far: " << input_buffer << std::endl; // For debugging + input_buffer += c; // Add the digit to our "typing" buffer return true; } } @@ -103,14 +109,16 @@ int main(int argc, char* argv[]) { } // Handle the 'Enter' key press - if (event == ftxui::Event::Return) { - std::cout << "Enter pressed. Input buffer: " << input_buffer << std::endl; + if (event == ftxui::Event::Return) { if (!input_buffer.empty()) { - // Convert buffer to integer and check bounds - int requested_index = std::stoi(input_buffer);// -1; - if (requested_index >= 0 && requested_index < (int)clean_names.size()) { - selected = requested_index; // Move the highlight to the typed index - } + try + { + // Convert buffer to integer and check bounds + int requested_index = std::stoi(input_buffer);// -1; + if (requested_index >= 0 && requested_index < (int)clean_names.size()) { + selected = requested_index; // Move the highlight to the typed index + } + } catch (...) {} input_buffer.clear(); // Clear buffer after use } std::string selected_name; @@ -128,13 +136,15 @@ int main(int argc, char* argv[]) { new_path = std::filesystem::absolute(new_path).lexically_normal(); new_path.make_preferred(); + current_path = new_path.string(); directory_path = current_path; // Update global variable - + if (std::filesystem::is_regular_file(new_path)) + screen.Exit(); // Exit the application // Call the function to switch and reload ReloadDirectory(screen, new_path, entries,clean_names); selected = 0; // Reset selection to the first item - //screen.Exit(); // Exit the application + return true; // Event handled } @@ -181,7 +191,8 @@ int main(int argc, char* argv[]) { }); screen.Loop(main_renderer); - + // This will print to your terminal AFTER you press 'q' to quit + std::cout << "Final Path on Exit: " << current_path << std::endl; // ---------------------------------------------------- // --- OUTPUT AFTER THE LOOP HAS CLEANLY EXITED --- // ---------------------------------------------------- @@ -191,13 +202,5 @@ int main(int argc, char* argv[]) { } else { std::cout << "Selection cancelled or no valid entry selected." << std::endl; } - /*Element document = vbox({ - text("left") | border, - text("middle") | border | flex, - text("right") | border, - }); - - auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document)); - Render(screen, document); - screen.Print();*/ + } \ No newline at end of file