Mercurial > xbps-search-tv
changeset 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 | |
| files | Makefile main.cpp |
| diffstat | 2 files changed, 41 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/Makefile Sun Sep 13 19:40:11 2026 +0000 +++ b/Makefile Sun Sep 13 19:48:34 2026 +0000 @@ -3,8 +3,9 @@ TARGET := xt SRCS := main.cpp spawn.cpp pipes.cpp OBJS := $(SRCS:.cpp=.o) - -.PHONY: all clean +PREFIX = /usr/local +BINDIR = /bin +.PHONY: all clean install all: $(TARGET) @@ -14,5 +15,7 @@ %.o: %.cpp process.h $(CXX) $(CXXFLAGS) -c $< -o $@ +install: $(TARGET) + install -Dm755 $(TARGET) $(PREFIX)$(BINDIR)/$(TARGET) clean: rm -f $(OBJS) $(TARGET)
--- a/main.cpp Sun Sep 13 19:40:11 2026 +0000 +++ b/main.cpp Sun Sep 13 19:48:34 2026 +0000 @@ -1,6 +1,41 @@ #include <iostream> #include <sstream> #include "process.h" +#include <filesystem> +#include <fstream> +#include <chrono> +#include <cstdlib> + +namespace fs = std::filesystem; + +std::string get_package_list() { + const char* home = std::getenv("HOME"); + fs::path cache_dir = fs::path(home) / ".cache" / "xt"; + fs::path cache_file = cache_dir / "packages.txt"; + + const auto max_age = std::chrono::minutes(30); + + if (fs::exists(cache_file)) { + auto last_write = fs::last_write_time(cache_file); + auto now = decltype(last_write)::clock::now(); + auto age = now - last_write; + if (age < max_age) { + std::ifstream in(cache_file); + std::string contents((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>()); + return contents; + } + } + + // cache missing or stale, re-query + char* const query_argv[] = { const_cast<char*>("xbps-query"), const_cast<char*>("-Rs"), const_cast<char*>(""), nullptr }; + std::string package_list = run_piped(query_argv, ""); + + fs::create_directories(cache_dir); + std::ofstream out(cache_file); + out << package_list; + + return package_list; +} std::string extract_pkgname(const std::string& fzf_line) { std::istringstream iss(fzf_line); std::string marker, pkgver; @@ -8,8 +43,7 @@ return pkgver; } int main() { - char* const query_argv[] = { const_cast<char*>("xbps-query"), const_cast<char*>("-Rs"), const_cast<char*>(""), nullptr }; - std::string package_list = run_piped(query_argv, ""); + std::string package_list = get_package_list(); char* const fzf_argv[] = { const_cast<char*>("fzf"), nullptr }; std::string result = run_piped(fzf_argv, package_list);
