-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.cpp
85 lines (73 loc) · 1.93 KB
/
query.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "query.h"
Query::Query(Searcher *s) : sorter(string(getenv("HOME"))+"/.bash_history") {
query="";
searcher = s;
}
Query::~Query() {
// DON'T DELETE searcher HERE
}
list_t& Query::getMatches() {
if(history.empty()) {
list_t unsorted_candidates = searcher->searchp(query);
list_t candidates = sorter.getSorted(unsorted_candidates);
if(!candidates.empty())
history.push(candidates);
else {
// TODO: something better here
return empty; // An empty vector output
}
}
return history.top();
}
string Query::getMatch() {
if(history.empty() ||
history.top().empty() ||
!history.top().front().compare(""))
return query;
return history.top().front();
}
bool Query::exactMatch() {
return searcher->search(query);
}
// void Query::addCharRestricted(char c) {
// if(searcher->containsp(query+c))
// query += c;
// history.push(searcher->searchp(query));
// }
void Query::addChar(char c) {
query += c;
list_t unsorted_candidates = searcher->searchp(query);
list_t candidates = sorter.getSorted(unsorted_candidates);
history.push(candidates);
}
bool Query::removeChar() {
if(query.length()>0) {
query=query.substr(0,query.length()-1);
history.pop();
return true;
}
return false;
}
string& Query::getQuery() {return query;}
void Query::rotateForward() {
if(history.top().size()<2) return;
list_t current_matches = history.top();
history.pop();
current_matches.push_back(current_matches.front());
current_matches.erase(current_matches.begin());
history.push(current_matches);
}
void Query::rotateBackward() {
if(history.top().size()<2) return;
list_t current_matches = history.top();
history.pop();
current_matches.insert(current_matches.begin(),current_matches.back());
current_matches.pop_back();
history.push(current_matches);
}
void Query::reset(Searcher *s) {
query="";
while(!history.empty())
history.pop();
searcher=s;
}