put full namespace

This commit is contained in:
Jason Ly 2025-12-20 12:44:33 -05:00
parent 4b740719ff
commit 3b0b7359af

View File

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