#include "mologram/ui/Suggest.hpp"
#include <algorithm>
#include <cctype>
#include <map>
namespace mologram::ui {
static bool wordByte(unsigned char c) { return std::isalnum(c) || c == '_' || c >= 0x80; } // letters of any script travel as UTF-8 bytes >= 0x80
static std::string lower(std::string s) {
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return (char)std::tolower(c); });
return s;
}
std::optional<SuggestToken> Suggest::tokenAt(const std::string& line, size_t caret) {
caret = std::min(caret, line.size());
size_t i = caret;
while (i > 0 && wordByte((unsigned char)line[i - 1])) i--;
if (i == 0) return std::nullopt;
char trigger = line[i - 1];
if (trigger != '@' && trigger != '#' && trigger != '/') return std::nullopt;
size_t at = i - 1;
if (at > 0 && line[at - 1] != ' ' && line[at - 1] != '\t') return std::nullopt; // an e-mail address or a URL is not a mention
if (trigger == '/' && at != 0) return std::nullopt;
SuggestToken t;
t.trigger = trigger;
t.prefix = line.substr(i, caret - i);
t.start = at;
return t;
}
std::vector<Suggestion> Suggest::mentions(const std::vector<PeerInfo>& people, const std::string& prefix) {
std::vector<Suggestion> out;
std::string p = lower(prefix);
for (const auto& m : people) {
if (m.username.empty()) continue; // a mention needs a username
if (!p.empty() && lower(m.username).rfind(p, 0) != 0 && lower(m.name).find(p) == std::string::npos) continue;
out.push_back({m.name + " @" + m.username, "@" + m.username});
}
return out;
}
std::vector<Suggestion> Suggest::hashtags(const std::vector<std::string>& texts, const std::string& prefix) {
std::map<std::string, int> count;
for (const auto& t : texts) {
for (size_t i = 0; i < t.size(); i++) {
if (t[i] != '#' || (i > 0 && t[i - 1] != ' ' && t[i - 1] != '\n')) continue;
size_t j = i + 1;
while (j < t.size() && wordByte((unsigned char)t[j])) j++;
if (j > i + 1) count[t.substr(i, j - i)]++;
i = j;
}
}
std::vector<std::pair<std::string, int>> v(count.begin(), count.end());
std::stable_sort(v.begin(), v.end(), [](const auto& a, const auto& b) { return a.second > b.second; });
std::vector<Suggestion> out;
std::string p = lower(prefix);
for (const auto& kv : v) {
if (!p.empty() && lower(kv.first).rfind("#" + p, 0) != 0) continue;
out.push_back({kv.first + " (" + std::to_string(kv.second) + "x)", kv.first});
}
return out;
}
std::vector<Suggestion> Suggest::commands(const std::vector<std::pair<std::string, std::string>>& cmds, const std::string& prefix) {
std::vector<Suggestion> out;
std::string p = lower(prefix);
for (const auto& c : cmds) {
std::string name = c.first.rfind("/", 0) == 0 ? c.first.substr(1) : c.first;
if (!p.empty() && lower(name).rfind(p, 0) != 0) continue;
out.push_back({"/" + name + " " + c.second, "/" + name});
}
return out;
}
std::string Suggest::apply(const std::string& line, const SuggestToken& t, size_t tokenEnd, const Suggestion& s, size_t& caret) {
tokenEnd = std::min(std::max(tokenEnd, t.start), line.size());
std::string rest = line.substr(tokenEnd);
if (!rest.empty() && rest[0] == ' ') rest.erase(0, 1); // one space follows the inserted word, never two
std::string out = line.substr(0, t.start) + s.insert + " " + rest;
caret = t.start + s.insert.size() + 1;
return out;
}
} // namespace mologram::ui