Revision 8a16b7a0
Added by david.sorber over 2 years ago
| src/Catalog.cc | ||
|---|---|---|
|
|
||
|
void Catalog::filter(const std::string& criteria)
|
||
|
{
|
||
|
// TODO: would be nice if there were a more efficient way to do this
|
||
|
for (auto& item : m_catalog)
|
||
|
// Tokenize the filter criteria
|
||
|
std::vector<std::string_view> tokens;
|
||
|
tokenize(criteria, " ", tokens);
|
||
|
|
||
|
// Iterate over the filter criteria tokens
|
||
|
for (auto& token : tokens)
|
||
|
{
|
||
|
if (item.match(criteria))
|
||
|
{
|
||
|
item.setVisibilty(true);
|
||
|
}
|
||
|
else
|
||
|
// Attempt to match each catalog item
|
||
|
for (auto& item : m_catalog)
|
||
|
{
|
||
|
item.setVisibilty(false);
|
||
|
if (item.match(token))
|
||
|
{
|
||
|
item.setVisibilty(true);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
item.setVisibilty(false);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
| src/Catalog.h | ||
|---|---|---|
|
#include <climits>
|
||
|
#include <cstdint>
|
||
|
#include <string>
|
||
|
#include <string_view>
|
||
|
#include <vector>
|
||
|
|
||
|
#include "CatalogItem.h"
|
||
|
|
||
|
|
||
|
// I mean this should go into some sort of utils
|
||
|
template<class T>
|
||
|
static inline void tokenize(
|
||
|
T input,
|
||
|
std::string delim,
|
||
|
std::vector<std::string_view>& tokens)
|
||
|
{
|
||
|
uint32_t prevOffset = 0;
|
||
|
auto pos = std::string_view::npos;
|
||
|
while ((pos = input.find(delim, prevOffset)) != std::string_view::npos)
|
||
|
{
|
||
|
tokens.emplace_back(input.data() + prevOffset, (pos - prevOffset));
|
||
|
prevOffset = pos + delim.size();
|
||
|
}
|
||
|
|
||
|
// Final (or only one) parameter
|
||
|
tokens.emplace_back(input.data() + prevOffset,
|
||
|
(input.size() - prevOffset));
|
||
|
}
|
||
|
|
||
|
|
||
|
class Catalog
|
||
|
{
|
||
|
public:
|
||
| src/CatalogItem.cc | ||
|---|---|---|
|
}
|
||
|
|
||
|
bool CatalogItem::match(const std::string& criteria)
|
||
|
{
|
||
|
return m_keyLower.find(criteria) != std::string::npos;
|
||
|
}
|
||
|
|
||
|
bool CatalogItem::match(const std::string_view& criteria)
|
||
|
{
|
||
|
return m_keyLower.find(criteria) != std::string::npos;
|
||
|
}
|
||
| src/CatalogItem.h | ||
|---|---|---|
|
|
||
|
#include <cstdint>
|
||
|
#include <string>
|
||
|
#include <string_view>
|
||
|
|
||
|
|
||
|
class CatalogItem
|
||
| ... | ... | |
|
* @return
|
||
|
*/
|
||
|
bool match(const std::string& criteria);
|
||
|
|
||
|
bool match(const std::string_view& criteria);
|
||
|
|
||
|
private:
|
||
|
|
||
| src/main.cc | ||
|---|---|---|
|
#endif
|
||
|
|
||
|
const std::string APP_NAME("pwmgr");
|
||
|
const std::string VERSION("v0.1.2");
|
||
|
const std::string VERSION("v0.1.3");
|
||
|
const std::string DEFAULT_FILEPATH("/home/dsorber/Documents/chicken_soup/chicken_soup.txt.enc");
|
||
|
|
||
|
const std::string BOLD{"\033[1m"};
|
||
| ... | ... | |
|
Catalog catalog;
|
||
|
|
||
|
const char* STR_EMPTY = "";
|
||
|
const char* STR_PWD = "enter password and click \"Load\"";
|
||
|
const char* STR_LOADING = "Loading...";
|
||
|
|
||
|
static ImGuiInputTextFlags MULTILINE_TEXT_FLAGS =
|
||
| ... | ... | |
|
char filterBuffer[128]{};
|
||
|
char filePathBuffer[BUFFER_SIZE]{};
|
||
|
std::memcpy(filePathBuffer, DEFAULT_FILEPATH.data(), DEFAULT_FILEPATH.size());
|
||
|
const char* statusText = STR_EMPTY;
|
||
|
const char* statusText = STR_PWD;
|
||
|
|
||
|
char passwdBuffer[BUFFER_SIZE]{};
|
||
|
while (! done)
|
||
| ... | ... | |
|
}
|
||
|
ImGui::SameLine();
|
||
|
|
||
|
ImGui::Text(statusText);
|
||
|
ImGui::Text("Status: %s", statusText);
|
||
|
|
||
|
if (loading)
|
||
|
{
|
||
| ... | ... | |
|
iter.getValueBuffer(),
|
||
|
iter.getValueBufferSize(),
|
||
|
ImVec2(-FLT_MIN,
|
||
|
ImGui::GetTextLineHeight() * 4),
|
||
|
ImGui::GetTextLineHeight() * 5),
|
||
|
MULTILINE_TEXT_FLAGS);
|
||
|
}
|
||
|
}
|
||
Tokenize a filter criteria and then search the catalog for feach filter
criteria token.