diff --git a/joomer-ftxui-file-browser.cpp b/joomer-ftxui-file-browser.cpp index 32f1c93..e69aa6b 100644 --- a/joomer-ftxui-file-browser.cpp +++ b/joomer-ftxui-file-browser.cpp @@ -5,23 +5,23 @@ #include #include // Required for std::filesystem -using namespace std; -namespace fs = std::filesystem; // Create a shorter alias for std::filesystem +//using namespace std; +//namespace fs = std::filesystem; // Create a shorter alias for std::filesystem std::string directory_path = "."; // Replace with your directory path -string test; +std::string test; // Note: You must pass the ScreenInteractive object to trigger a redraw. -void ReloadDirectory(ftxui::ScreenInteractive& screen, const fs::path& new_path, std::vector& entries) { +void ReloadDirectory(ftxui::ScreenInteractive& screen, const std::filesystem::path& new_path, std::vector& entries) { test = "ReloadDirectory"; - namespace fs = std::filesystem; + //namespace fs = std::filesystem; //std::string directory_path = new_path.string; // Replace with your directory path // linux std::string directory_path = new_path.string(); // Replace with your directory path //windows // Check if the path is a valid directory - if (!fs::exists(new_path) || !fs::is_directory(new_path)) { + if (!std::filesystem::exists(new_path) || !std::filesystem::is_directory(new_path)) { // Handle error case (optional: display a warning in the TUI) - cerr << "Error: Not a directory or path does not exist." << endl; + std::cerr << "Error: Not a directory or path does not exist." << std::endl; test = "path not a directory: " + new_path.string(); return; } @@ -30,9 +30,9 @@ void ReloadDirectory(ftxui::ScreenInteractive& screen, const fs::path& new_path, int i = 1; test = "list dir"; entries.clear(); - for (const auto& entry : fs::directory_iterator(new_path)) { + for (const auto& entry : std::filesystem::directory_iterator(new_path)) { test = "got in for loop, new path is " + new_path.string(); - if (fs::is_directory(entry.path()) || fs::is_regular_file(entry.path())) { + if (std::filesystem::is_directory(entry.path()) || std::filesystem::is_regular_file(entry.path())) { i++; std::string name = entry.path().filename().string(); //if (fs::is_directory(entry.path())) { @@ -46,8 +46,8 @@ void ReloadDirectory(ftxui::ScreenInteractive& screen, const fs::path& new_path, } int main(int argc, char* argv[]) { - using namespace ftxui; - using namespace std; + //using namespace ftxui; + //using namespace std; // Define a variable to hold the final selection index int final_selected_index = -1; // Use -1 to indicate no selection was made std::vector entries; @@ -56,22 +56,22 @@ int main(int argc, char* argv[]) { directory_path = argv[1]; // Check if the directory exists - if (!fs::exists(directory_path) || !fs::is_directory(directory_path)) { + 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 = ScreenInteractive::TerminalOutput(); + auto screen = ftxui::ScreenInteractive::TerminalOutput(); - fs::directory_iterator entryIt(directory_path); + std::filesystem::directory_iterator entryIt(directory_path); int i = 0; // Iterate through the directory entries - for (const auto& entry : fs::directory_iterator(directory_path)) + for (const auto& entry : std::filesystem::directory_iterator(directory_path)) //for (entryIt = entries.begin(), i = 0; entryIt != entries.end(); ++ entryIt, i++) { - i++; - string str_num = to_string(i); + i++; + std::string str_num = std::to_string(i); entries.push_back( entry.path().filename().string()); } /* = { @@ -80,15 +80,15 @@ int main(int argc, char* argv[]) { "entry 3", };*/ int selected = 0; - auto menu = Menu({ + auto menu = ftxui::Menu({ .entries = &entries, .selected = &selected, }); // --- The key part: Applying CatchEvent() --- - auto menu_with_event_handler = menu | CatchEvent([&](Event event) { + auto menu_with_event_handler = menu | ftxui::CatchEvent([&](ftxui::Event event) { // Handle the 'Enter' key press - if (event == Event::Return) { + if (event == ftxui::Event::Return) { // 1. Store the selected index final_selected_index = selected; std::cout << "Selected Entry: " << entries[selected] << std::endl; @@ -100,7 +100,7 @@ int main(int argc, char* argv[]) { } // Handle the 'q' key press to quit anytime - if (event == Event::Character('q')) { + if (event == ftxui::Event::Character('q')) { screen.Exit(); return true; // Event handled } @@ -115,7 +115,7 @@ int main(int argc, char* argv[]) { // ---------------------------------------------------- // --- OUTPUT AFTER THE LOOP HAS CLEANLY EXITED --- // ---------------------------------------------------- - cout<< "directory path: " << directory_path << endl; + std::cout<< "directory path: " << directory_path << std::endl; //cout << "test: " << test << endl; if (final_selected_index >= 0 && final_selected_index < entries.size()) { std::cout << "Selected Entry: " << entries[final_selected_index] << std::endl;