From 8c61772c668e04f7c4bb5f5ba0d47a9992a28eeb Mon Sep 17 00:00:00 2001 From: chatlanin Date: Sat, 18 Mar 2023 14:03:39 +0300 Subject: [PATCH] add create file and dir --- src/rrr/content/content.cpp | 32 ++++++++++++++++++- src/rrr/content/content.hpp | 1 + src/rrr/content/file/file.hpp | 8 +++++ src/rrr/content/navigation/navigation.cpp | 12 ++++--- src/rrr/content/navigation/navigation.hpp | 2 +- .../gui/browser/navigation/navigation.cpp | 11 +++---- src/rrr/layers/gui/dialogs/dialogs.cpp | 1 + src/rrr/utils/types.hpp | 4 +-- 8 files changed, 56 insertions(+), 15 deletions(-) diff --git a/src/rrr/content/content.cpp b/src/rrr/content/content.cpp index dcf2b1f..6d43d29 100644 --- a/src/rrr/content/content.cpp +++ b/src/rrr/content/content.cpp @@ -1,6 +1,8 @@ #include "content.hpp" #include +#include "logger/logger.hpp" + #include "buffer/buffer.hpp" namespace rrr @@ -71,6 +73,10 @@ namespace rrr int content::get_preview_cursor_position() { int preview_cursor_position = 0; + + if (std::filesystem::is_empty(PWD)) + return preview_cursor_position; + auto pwd = nav.store[PWD].at(cursor_position).path; if (buffer::state.contains(pwd)) @@ -103,6 +109,7 @@ namespace rrr { // ставим новый pwd и заполняем навигацию и историю PWD = PWD / nav.store[PWD].at(cursor_position).path.filename(); + nav.fill(PWD); his.fill(PWD); @@ -125,7 +132,11 @@ namespace rrr } // и исходя из позиции курсора заполняем окно предварительного просмотра - prev.fill(nav.store[PWD].at(cursor_position).path); + // в зависимости от того пустаая ли она или неt + if (std::filesystem::is_empty(PWD)) + prev.data.clear(); + else + prev.fill(nav.store[PWD].at(cursor_position).path); } void content::navigation_left() @@ -151,4 +162,23 @@ namespace rrr cursor_position = i; }); } + + void content::create_file(std::string filename) + { + std::string cmd; + + if (filename.find("/") != std::string::npos) + { + if (filename.at(filename.size() - 1) == '/') cmd = "mkdir -p " + std::string(PWD / filename); + else cmd = "mkdir -p " + std::string(PWD / std::filesystem::path(filename).parent_path()) + + " && touch " + std::string(PWD / filename); + } + else + { + cmd = "touch " + std::string(PWD / filename); + } + + hack::utils::unix_cmd(cmd); + nav.fill(PWD); + } } diff --git a/src/rrr/content/content.hpp b/src/rrr/content/content.hpp index 087fe61..f86a934 100644 --- a/src/rrr/content/content.hpp +++ b/src/rrr/content/content.hpp @@ -32,6 +32,7 @@ namespace rrr void increment_position(int); void navigation_right(); void navigation_left(); + void create_file(std::string); private: content_type::navigation nav; diff --git a/src/rrr/content/file/file.hpp b/src/rrr/content/file/file.hpp index 66efc22..0f2d0e1 100644 --- a/src/rrr/content/file/file.hpp +++ b/src/rrr/content/file/file.hpp @@ -5,6 +5,8 @@ #include #include +#include "logger/logger.hpp" + namespace rrr { // NONE - просто выводится текст. когда пупка пуста и нет контента вообще @@ -98,6 +100,12 @@ namespace rrr::file_utils inline files fill(std::filesystem::path pwd) { files current_files = get_files_struct(pwd); + + if (current_files.size() == 1 && current_files.at(0).path == pwd / "no permission") + { + return current_files; + } + files tmp; tmp.reserve(current_files.size()); std::sort(current_files.begin(), current_files.end()); diff --git a/src/rrr/content/navigation/navigation.cpp b/src/rrr/content/navigation/navigation.cpp index 4b3b91f..02d9493 100644 --- a/src/rrr/content/navigation/navigation.cpp +++ b/src/rrr/content/navigation/navigation.cpp @@ -4,12 +4,14 @@ namespace rrr::content_type { void navigation::fill(std::filesystem::path PWD) { - if (!store.contains(PWD)) - store[PWD] = file_utils::fill(PWD); - } + // if (!store.contains(PWD)) + // store[PWD] = file_utils::fill(PWD); - void navigation::update(std::filesystem::path PWD) - { store[PWD] = file_utils::fill(PWD); } + + // void navigation::update(std::filesystem::path PWD) + // { + // store[PWD] = file_utils::fill(PWD); + // } } diff --git a/src/rrr/content/navigation/navigation.hpp b/src/rrr/content/navigation/navigation.hpp index 134f485..8898fe8 100644 --- a/src/rrr/content/navigation/navigation.hpp +++ b/src/rrr/content/navigation/navigation.hpp @@ -14,7 +14,7 @@ namespace rrr::content_type ~navigation() = default; void fill(std::filesystem::path); - void update(std::filesystem::path); + // void update(std::filesystem::path); std::unordered_map store; }; diff --git a/src/rrr/layers/gui/browser/navigation/navigation.cpp b/src/rrr/layers/gui/browser/navigation/navigation.cpp index 7363743..b38a4ba 100644 --- a/src/rrr/layers/gui/browser/navigation/navigation.cpp +++ b/src/rrr/layers/gui/browser/navigation/navigation.cpp @@ -133,16 +133,13 @@ namespace rrr::layers::gui case types::event_type::DELETE_CURRENT_FILE: freeze = false; hack::log()("DELETE FILE"); - // HERE - // реализовать удаление после создания break; case types::event_type::CREATE_FILE: { freeze = false; auto filename = std::any_cast(value); - // HERE начинам тут - // реализовать через контент создание файла или директории - // а затем его уделаение см. HERE то что выше + cnt->create_file(filename); + data = cnt->get(TYPE_WIN::NAVIGATION); } break; default: @@ -197,13 +194,15 @@ namespace rrr::layers::gui em->execute(types::event_type::NAVIGATION_RIGHT, nullptr); } + // удаление if (shift && key.get_keycode() == try_engine::key::D) { em->execute(types::event_type::SHOW_DELETE_ONE_FILE_DIALOG, current_file); freeze = true; } - if (shift && key.get_keycode() == try_engine::key::C) + // создание файла/директории + if (shift && key.get_keycode() == try_engine::key::A) { em->execute(types::event_type::SHOW_CREATE_FILE_DIALOG, nullptr); freeze = true; diff --git a/src/rrr/layers/gui/dialogs/dialogs.cpp b/src/rrr/layers/gui/dialogs/dialogs.cpp index 963c4fc..70ad1fb 100644 --- a/src/rrr/layers/gui/dialogs/dialogs.cpp +++ b/src/rrr/layers/gui/dialogs/dialogs.cpp @@ -221,6 +221,7 @@ namespace rrr::layers::gui void dialogs::create_file() { + if (title.empty()) return; show = false; create_dialog = false; em->execute(types::event_type::CREATE_FILE, title); diff --git a/src/rrr/utils/types.hpp b/src/rrr/utils/types.hpp index ac3e244..f29eefe 100644 --- a/src/rrr/utils/types.hpp +++ b/src/rrr/utils/types.hpp @@ -10,8 +10,8 @@ namespace rrr::types NAVIGATION_LEFT, NAVIGATION_RIGHT, SHOW_DELETE_ONE_FILE_DIALOG, - FREEZE_BROWSER_ACTION, - UNFREEZE_BROWSER_ACTION, + FREEZE_BROWSER_ACTION, // замораживает все действия + UNFREEZE_BROWSER_ACTION, // размораживает все действия DELETE_CURRENT_FILE, SHOW_CREATE_FILE_DIALOG, CREATE_FILE