Mercurial > xbps-search-tv
comparison main.cpp @ 3:ae3a6d7bcca6 default tip
cache the package list for better performance
| author | Yasuri <yasuri@charliesmp.net> |
|---|---|
| date | Sun, 13 Sep 2026 19:48:34 +0000 |
| parents | 00b62a080354 |
| children |
comparison
equal
deleted
inserted
replaced
| 2:00b62a080354 | 3:ae3a6d7bcca6 |
|---|---|
| 1 #include <iostream> | 1 #include <iostream> |
| 2 #include <sstream> | 2 #include <sstream> |
| 3 #include "process.h" | 3 #include "process.h" |
| 4 #include <filesystem> | |
| 5 #include <fstream> | |
| 6 #include <chrono> | |
| 7 #include <cstdlib> | |
| 8 | |
| 9 namespace fs = std::filesystem; | |
| 10 | |
| 11 std::string get_package_list() { | |
| 12 const char* home = std::getenv("HOME"); | |
| 13 fs::path cache_dir = fs::path(home) / ".cache" / "xt"; | |
| 14 fs::path cache_file = cache_dir / "packages.txt"; | |
| 15 | |
| 16 const auto max_age = std::chrono::minutes(30); | |
| 17 | |
| 18 if (fs::exists(cache_file)) { | |
| 19 auto last_write = fs::last_write_time(cache_file); | |
| 20 auto now = decltype(last_write)::clock::now(); | |
| 21 auto age = now - last_write; | |
| 22 if (age < max_age) { | |
| 23 std::ifstream in(cache_file); | |
| 24 std::string contents((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>()); | |
| 25 return contents; | |
| 26 } | |
| 27 } | |
| 28 | |
| 29 // cache missing or stale, re-query | |
| 30 char* const query_argv[] = { const_cast<char*>("xbps-query"), const_cast<char*>("-Rs"), const_cast<char*>(""), nullptr }; | |
| 31 std::string package_list = run_piped(query_argv, ""); | |
| 32 | |
| 33 fs::create_directories(cache_dir); | |
| 34 std::ofstream out(cache_file); | |
| 35 out << package_list; | |
| 36 | |
| 37 return package_list; | |
| 38 } | |
| 4 std::string extract_pkgname(const std::string& fzf_line) { | 39 std::string extract_pkgname(const std::string& fzf_line) { |
| 5 std::istringstream iss(fzf_line); | 40 std::istringstream iss(fzf_line); |
| 6 std::string marker, pkgver; | 41 std::string marker, pkgver; |
| 7 iss >> marker >> pkgver; | 42 iss >> marker >> pkgver; |
| 8 return pkgver; | 43 return pkgver; |
| 9 } | 44 } |
| 10 int main() { | 45 int main() { |
| 11 char* const query_argv[] = { const_cast<char*>("xbps-query"), const_cast<char*>("-Rs"), const_cast<char*>(""), nullptr }; | 46 std::string package_list = get_package_list(); |
| 12 std::string package_list = run_piped(query_argv, ""); | |
| 13 char* const fzf_argv[] = { const_cast<char*>("fzf"), nullptr }; | 47 char* const fzf_argv[] = { const_cast<char*>("fzf"), nullptr }; |
| 14 std::string result = run_piped(fzf_argv, package_list); | 48 std::string result = run_piped(fzf_argv, package_list); |
| 15 | 49 |
| 16 // strip trailing newline fzf adds | 50 // strip trailing newline fzf adds |
| 17 if (!result.empty() && result.back() == '\n') { | 51 if (!result.empty() && result.back() == '\n') { |
