add try_engine

This commit is contained in:
chatlanin
2023-03-02 15:25:58 +03:00
parent 5a829fad05
commit f81f4e5e78
54 changed files with 129 additions and 1905 deletions

View File

@@ -0,0 +1,120 @@
#pragma once
#include "utils/utils.hpp"
#include <filesystem>
#include <vector>
#include <algorithm>
namespace rrr
{
// FILE - файл
// DIR - дирректория
// TEXT - просто выводится текст
enum class file_type
{
FILE, DIR, TEXT
};
// класс-тип контента
// т.е. контентом является все, что может поместиться в терминал
// и это все называется файлом. ддиректория - это файл, файл - это файл и т.п.
// просто каждая сущность имеет свой тип см. выше
class file : public hack::utils::counter<int>
{
public:
file() = default;
file(std::filesystem::path, file_type, bool);
file(file&&);
file(const file&);
public:
file& operator=(const file&);
bool operator<(const file&) const;
bool operator==(const file&) const;
friend std::ostream& operator<<(std::ostream&, const file&);
public:
int id;
std::filesystem::path path;
file_type type;
bool is_link = false;
bool is_mark = false;
};
}
namespace rrr::file_utils
{
using files = std::vector<file>;
struct filesystem_convert
{
file operator()(const std::filesystem::directory_entry& entry) const
{
file_type type = file_type::FILE;
if (std::filesystem::is_directory(entry))
type = file_type::DIR;
auto is_link = std::filesystem::is_symlink(entry);
return { entry.path(), type, is_link };
}
};
inline files get_files_struct(const std::filesystem::path& path)
{
files f;
try
{
std::filesystem::directory_iterator start(path);
std::filesystem::directory_iterator end;
std::transform(start, end, std::back_inserter(f), filesystem_convert());
}
catch(...)
{
f.push_back({ path / "no permission", file_type::TEXT, false });
}
return f;
}
// не нужно тут передавать pwd, как ссылку
// т.к. один фиг копирование в content::fill произойдет из-за
// parent_path() метода, которым мы пользуемся при передачи сюда параметра
// НО пока эта информация уже устарела, т.к. много в коде изменилось
// по этому потом поэкспериментируй с запусками эстов конечно же
// т.е. передай по ссылке и посмотри на поведение
inline files fill(std::filesystem::path pwd)
{
files current_files = get_files_struct(pwd);
files tmp;
tmp.reserve(current_files.size());
std::sort(current_files.begin(), current_files.end());
files tmp_files;
tmp_files.reserve(current_files.size());
std::copy_if(current_files.begin(), current_files.end(), std::back_inserter(tmp), [](const file& f) -> bool {
if (f.type == file_type::DIR) return true;
return false;
});
std::copy_if(current_files.begin(), current_files.end(), std::back_inserter(tmp_files), [](const file& f) -> bool {
if (f.type != file_type::DIR) return true;
return false;
});
tmp.insert(tmp.end(), tmp_files.begin(), tmp_files.end());
return tmp;
}
inline bool is_hidden(const std::filesystem::path &p)
{
std::filesystem::path::string_type name = p.filename();
if(name != ".." && name != "." && name[0] == '.')
return true;
return false;
}
}