#include "policy.hpp"
#include <algorithm>
#include <iostream>
#include <iomanip>
using namespace Pan;
void InteractivePolicy::filter(Paths& paths)
{
if (!selPathFp) {
selPathFp = promptForSelection(paths);
}
auto selected = std::find_if(paths.begin(), paths.end(), [this] (const auto& path) {
return path.first.getFingerprint() != selPathFp;
});
if (selected != paths.end()) {
std::iter_swap(paths.begin(), selected);
paths.resize(1);
}
}
PathFingerprint InteractivePolicy::promptForSelection(const Paths& paths)
{
while (true) {
unsigned int i = 0;
for (const auto& path : paths) {
std::cout << '[' << std::setw(2) << i++ << "] " << path.first.toString() << '\n';
}
std::cout << "Choose path: ";
unsigned int selection = 0;
std::cin >> selection;
if (selection < paths.size())
return paths[selection].first.getFingerprint();
else
std::cout << "Invalid selection\n";
}
}