remove slash at the end for path to work in linux

This commit is contained in:
jason 2025-12-22 02:01:31 +00:00
parent 2300a34f5a
commit 1fa2c13e6e

View File

@ -49,6 +49,7 @@ int main(int argc, char* argv[]) {
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;
std::vector<std::string> clean_names; // New vector for logic std::vector<std::string> clean_names; // New vector for logic
std::string current_path = std::filesystem::current_path().string(); std::string current_path = std::filesystem::current_path().string();
std::filesystem::path start_path = std::filesystem::current_path(); std::filesystem::path start_path = std::filesystem::current_path();
@ -56,16 +57,29 @@ int main(int argc, char* argv[]) {
{ {
//directory_path = argv[1]; //directory_path = argv[1];
// Convert relative input (like "../../..") into a full absolute path // Convert relative input (like "../../..") into a full absolute path
std::cout << "argv[1]: " << argv[1] << std::endl; //std::cout << "argv[1]: " << argv[1] << std::endl;
start_path = std::filesystem::absolute(argv[1]).lexically_normal(); start_path = std::filesystem::absolute(argv[1]);
directory_path = start_path.string(); // Update global // Convert to string
std::cout << "directory_path: " << directory_path << std::endl;
current_path = directory_path; // Sync local start_path = start_path.lexically_normal();
std::cout << "current_path: " << current_path << std::endl; 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;
//std::cout << "directory_path: " << directory_path << std::endl;
//std::cout << "current_path: " << current_path << std::endl;
//current_path = directory_path.string(); //current_path = directory_path.string();
//current_path = directory_path; //current_path = directory_path;
} }
std::cout << "Initial current_path after argc: " << current_path << std::endl;
//current_path = start_path.string(); // Sync local
//std::cout << "current path: " << current_path << std::endl;
directory_path = current_path; // Update global
//std::cout << "Initial current_path after argc: " << current_path << std::endl;
// Check if the directory exists // Check if the directory exists
if (!std::filesystem::exists(directory_path) || !std::filesystem::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;
@ -73,9 +87,9 @@ int main(int argc, char* argv[]) {
} }
auto screen = ftxui::ScreenInteractive::TerminalOutput(); auto screen = ftxui::ScreenInteractive::TerminalOutput();
std::cout << "Initial current_path before reload: " << current_path << std::endl; //std::cout << "Initial current_path before reload: " << current_path << std::endl;
ReloadDirectory(screen, start_path, entries, clean_names); ReloadDirectory(screen, start_path, entries, clean_names);
std::cout << "Initial current_path after reload: " << current_path << std::endl; //std::cout << "Initial current_path after reload: " << current_path << std::endl;
int selected = 0; int selected = 0;
auto menu = ftxui::Menu({ auto menu = ftxui::Menu({
.entries = &entries, .entries = &entries,
@ -104,13 +118,16 @@ int main(int argc, char* argv[]) {
// Handle the 'Enter' key press // Handle the 'Enter' key press
if (event == ftxui::Event::Return) { if (event == ftxui::Event::Return) {
std::cout << "Enter pressed. Input buffer: " << input_buffer << std::endl; //std::cout << "Enter pressed. Input buffer: " << input_buffer << std::endl;
if (!input_buffer.empty()) { if (!input_buffer.empty()) {
try
{
// Convert buffer to integer and check bounds // Convert buffer to integer and check bounds
int requested_index = std::stoi(input_buffer);// -1; int requested_index = std::stoi(input_buffer);// -1;
if (requested_index >= 0 && requested_index < (int)clean_names.size()) { if (requested_index >= 0 && requested_index < (int)clean_names.size()) {
selected = requested_index; // Move the highlight to the typed index selected = requested_index; // Move the highlight to the typed index
} }
} catch (...) {}
input_buffer.clear(); // Clear buffer after use input_buffer.clear(); // Clear buffer after use
} }
std::string selected_name; std::string selected_name;
@ -181,7 +198,8 @@ int main(int argc, char* argv[]) {
}); });
screen.Loop(main_renderer); 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 --- // --- OUTPUT AFTER THE LOOP HAS CLEANLY EXITED ---
// ---------------------------------------------------- // ----------------------------------------------------