From 43d0ca3fd66a32f4d2db5c9116b4a11318c56018 Mon Sep 17 00:00:00 2001 From: jason Date: Sat, 22 Nov 2025 07:55:38 +0000 Subject: [PATCH] dir --- joomer-ftxui-file-browser.cpp | 133 ++++++++++++++++++++++++++++++++++ makefile | 91 +++++++++++++++++++++++ 2 files changed, 224 insertions(+) create mode 100644 joomer-ftxui-file-browser.cpp create mode 100644 makefile diff --git a/joomer-ftxui-file-browser.cpp b/joomer-ftxui-file-browser.cpp new file mode 100644 index 0000000..c8cb3e9 --- /dev/null +++ b/joomer-ftxui-file-browser.cpp @@ -0,0 +1,133 @@ +#include +#include +#include +#include +#include +#include // Required 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; + +// Note: You must pass the ScreenInteractive object to trigger a redraw. +void ReloadDirectory(ftxui::ScreenInteractive& screen, const fs::path& new_path, std::vector& entries) { + test = "ReloadDirectory"; + namespace fs = std::filesystem; + + std::string directory_path = new_path; // Replace with your directory path + // Check if the path is a valid directory + if (!fs::exists(new_path) || !fs::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; + test = "path not a directory: " + string(new_path.c_str()); + return; + } + + // 2. Read the new directory entries + int i = 1; + test = "list dir"; + entries.clear(); + for (const auto& entry : fs::directory_iterator(new_path)) { + test = "got in for loop, new path is " + string(new_path.c_str()); + if (fs::is_directory(entry.path()) || fs::is_regular_file(entry.path())) { + i++; + std::string name = entry.path().filename().string(); + //if (fs::is_directory(entry.path())) { + // name += "/"; + //} + entries.push_back(name); + //entries.push_back(std::to_string(i) + ". " + name); + //actual_paths.push_back(entry.path()); + } + } +} + +int main(int argc, char* argv[]) { + 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; + + if (argc > 1) + directory_path = argv[1]; + + // Check if the directory exists + if (!fs::exists(directory_path) || !fs::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(); + + + fs::directory_iterator entryIt(directory_path); + int i = 0; + // Iterate through the directory entries + for (const auto& entry : fs::directory_iterator(directory_path)) + //for (entryIt = entries.begin(), i = 0; entryIt != entries.end(); ++ entryIt, i++) + { + i++; + string str_num = to_string(i); + entries.push_back( entry.path().filename().string()); + } + /* = { + "entry 1", + "entry 2", + "entry 3", + };*/ + int selected = 0; + auto menu = Menu({ + .entries = &entries, + .selected = &selected, + }); + + // --- The key part: Applying CatchEvent() --- + auto menu_with_event_handler = menu | CatchEvent([&](Event event) { + // Handle the 'Enter' key press + if (event == Event::Return) { + // 1. Store the selected index + final_selected_index = selected; + std::cout << "Selected Entry: " << entries[selected] << std::endl; + directory_path = directory_path + "/" + entries[selected]; + // Call the function to switch and reload + ReloadDirectory(screen, directory_path, entries); + //screen.Exit(); // Exit the application + return true; // Event handled + } + + // Handle the 'q' key press to quit anytime + if (event == Event::Character('q')) { + screen.Exit(); + return true; // Event handled + } + + // If the event is not one we want to catch, return false + // so the Menu can handle it (like arrow keys) + return false; + }); + + screen.Loop(menu_with_event_handler); + + // ---------------------------------------------------- + // --- OUTPUT AFTER THE LOOP HAS CLEANLY EXITED --- + // ---------------------------------------------------- + cout<< "directory path: " << directory_path << 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; + } 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 diff --git a/makefile b/makefile new file mode 100644 index 0000000..549041a --- /dev/null +++ b/makefile @@ -0,0 +1,91 @@ + +# Project configuration +EXECUTABLE_NAME = joomer-ftxui-file-browser +PLATFORM = $(shell uname) +BUILD_TYPE ?= release# Default to release build if not specified + +#STB_PATH = $(HOME)/workdir/stb +LINUX_LIB_EXT = so + +# Version configuration (can be overridden) +#DPP_VERSION ?= $(shell find ../DPP/build/library -name "libdpp.so.*.*.*" -type f | head -1 | sed 's/.*libdpp\.so\.//') +FTXUI_PATH = $(HOME)/learndir/FTXUI +#BELLA_BUILD_DIR = $(BELLA_PATH)/build/library +LIB_PATHS = -L$(FTXUI_PATH)/build +INC_DIRS = -Iinclude -I$(FTXUI_PATH)/include +#endif + +#DPP_VERSION := $(or $(DPP_VERSION),10.1.4)# Fallback version if auto-detection fails +#DPP_LINUX_LIB_VERSION_FILE = libdpp.$(LINUX_LIB_EXT).$(DPP_VERSION) +#BELLA_LIB_NAME = libdpp.$(LINUX_LIB_EXT) + +# Common paths +OBJ_DIR = obj/$(PLATFORM)/$(BUILD_TYPE) +BIN_DIR = bin/$(PLATFORM)/$(BUILD_TYPE) +OUTPUT_FILE = $(BIN_DIR)/$(EXECUTABLE_NAME) + +CXX = g++ +CXX_FLAGS = $(COMMON_FLAGS) -std=c++17 -Wall -g $(INC_DIRS) + +# Build type specific flags +ifeq ($(BUILD_TYPE), debug) + CPP_DEFINES = -D_DEBUG -DDL_USE_SHARED + COMMON_FLAGS = $(ARCH_FLAGS) $(INCLUDE_PATHS) + COMP_FLAGS = -g -O0 -std=c++17 -Wall + #LINK_FLAGS = -fvisibility=hidden +else + CPP_DEFINES = -DNDEBUG=1 -DDL_USE_SHARED + COMMON_FLAGS = $(ARCH_FLAGS) $(INCLUDE_PATHS) + COMP_FLAGS = -O3 -std=c++17 -Wall + #LINK_FLAGS = -fvisibility=hidden +endif + +# Language-specific flags +#C_FLAGS = $(COMMON_FLAGS) -std=c17 +#CXX_FLAGS = $(COMMON_FLAGS) -std=c++17 -Wall -g $(INC_DIRS) +CXX = g++ +CXX_FLAGS = $(COMMON_FLAGS) $(INC_DIRS) +LINK_FLAGS = $(LIB_PATHS) #-fvisibility=hidden +FTXUI_LIB_FLAGS = -lftxui-component -lftxui-dom -lftxui-screen +#BELLA_LIB_FLAGS = -L$(LIB_PATHS) -Wl,-rpath,$(LIB_PATHS) -lbella_engine_sdk + +# Linker directive flags (L for library search path, l for library) +# Need to link against the D++ library and pthread (common for C++ applications with threading) +LDFLAGS = $(FTXUI_LIB_FLAGS) #$(BELLA_LIB_FLAGS) #-L$(LIB_PATHS) -ldpp -lpthread -Wl,-rpath='$$ORIGIN' # search for lib in the same place as the executable file + +# List of object files for your executable +# We've changed this back to use joomer-ftxui-file-browser.o as the source of the executable +OBJECTS = $(OBJ_DIR)/joomer-ftxui-file-browser.o + +$(OBJ_DIR)/%.o: %.cpp + @echo "Compiling $< -> $@" + @mkdir -p $(@D) # Ensure the output binary directory exists (e.g., bin/Linux/release/) + $(CXX) -c $(CPP_DEFINES) $(COMP_FLAGS) $(INC_DIRS) $< -o $@ + +# --- Main Target --- +# 'all' is the default target that builds your executable +all: $(OUTPUT_FILE) + @echo "FTXUI_PATH: $(FTXUI_PATH)" + @echo "Build complete." + + + +# Rule to link the executable: +# It depends on the object files and uses CXX to link them with specified libraries. +$(OUTPUT_FILE): $(OBJECTS) + @echo "Linking $(OUTPUT_FILE)..." + @mkdir -p $(@D) + $(CXX) -o $@ $(OBJECTS) $(LINK_FLAGS) $(LDFLAGS) + @echo "FTXUI_PATH: "$(FTXUI_PATH) + @echo "Build complete." + + +# --- Clean Target --- +# Removes all generated build files and directories +clean: + @echo "Cleaning build directory..." + $(RM) -r $(BIN_DIR) + $(RM) -r $(OBJ_DIR) + +# .PHONY specifies targets that are not actual files to prevent conflicts with file names +.PHONY: all clean \ No newline at end of file