init
This commit is contained in:
73
src/main.cpp
Normal file
73
src/main.cpp
Normal file
@@ -0,0 +1,73 @@
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstdlib>
|
||||
#include <filesystem>
|
||||
#include <format>
|
||||
#include <iostream>
|
||||
#include <ranges>
|
||||
#include <regex>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
namespace ranges = std::ranges;
|
||||
namespace views = std::ranges::views;
|
||||
|
||||
constexpr std::array<std::string_view, 8> exts = {
|
||||
".cc", ".cpp", ".c", ".py", ".rs", ".java", ".cs", ".lua"};
|
||||
|
||||
constexpr std::string_view editor_name = "joe";
|
||||
|
||||
std::vector<fs::path> &get_files(fs::path dir, std::vector<fs::path> &result) {
|
||||
for (auto &entry : fs::directory_iterator(dir)) {
|
||||
if (entry.is_directory()) {
|
||||
get_files(entry.path(), result);
|
||||
continue;
|
||||
}
|
||||
if (ranges::any_of(exts, [&](auto ext) {
|
||||
return ext == entry.path().extension();
|
||||
})) {
|
||||
result.emplace_back(entry.path());
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
fs::path get_filepath(std::string filepath) {
|
||||
fs::path current_path = fs::canonical(fs::current_path());
|
||||
std::vector<fs::path> result;
|
||||
get_files(current_path, result);
|
||||
|
||||
std::regex pattern(filepath, std::regex_constants::icase);
|
||||
|
||||
for (auto &file : result) {
|
||||
std::cout << file.string() << std::endl;
|
||||
if (file.filename().string() == filepath) {
|
||||
return file;
|
||||
}
|
||||
}
|
||||
for (auto &file : result) {
|
||||
if (std::regex_search(file.filename().string(), pattern)) {
|
||||
return file;
|
||||
}
|
||||
}
|
||||
return fs::path();
|
||||
}
|
||||
|
||||
int main(int argc, char **const argv) {
|
||||
if (argc != 2) {
|
||||
std::cerr << "[Info] usage: e filename\n" << argc;
|
||||
return -1;
|
||||
}
|
||||
auto file = get_filepath(argv[1]);
|
||||
std::string really_command;
|
||||
if (file.empty()) {
|
||||
std::format_to(std::back_inserter(really_command), "{} {}", editor_name,
|
||||
argv[1]);
|
||||
} else {
|
||||
std::format_to(std::back_inserter(really_command), "{} {}", editor_name,
|
||||
file.string());
|
||||
}
|
||||
std::cout << really_command << std::endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user