xaizek / pipedial (License: GPLv3 only) (since 2019-01-08)
One more tool for selecting something in console.
Commit a9884d86dfb4b54b28d2fbf85f70ac4e87e52421

Extract PipeDial::filter() method
To shorten PipeDial::buildNormalMode().
Author: xaizek
Author date (UTC): 2020-07-29 16:53
Committer name: xaizek
Committer date (UTC): 2020-07-29 16:53
Parent(s): e2ed8fdac868d082e8ff0fb1552b864af8f9ff61
Signing key: 99DC5E4DB05F6BE2
Tree: 84b41871472f59381291deeb6815668a782eeb5b
File Lines added Lines deleted
src/PipeDial.cpp 53 48
src/PipeDial.hpp 3 0
File src/PipeDial.cpp changed (mode: 100644) (index a63c790..517ed1c)
... ... PipeDial::buildNormalMode()
224 224 normalMode.addShortcut({ vle::Key(L'y').ctrl(), [&]() { normalMode.addShortcut({ vle::Key(L'y').ctrl(), [&]() {
225 225 list.scrollUp(); list.scrollUp();
226 226 }, "scroll one line up" }); }, "scroll one line up" });
227 normalMode.addShortcut({ L"/", [&]() {
228 cursed::List filterList;
229 filterList.setItems({ lines.cbegin(), lines.cend() });
227 normalMode.addShortcut({ L"/", [this]() { filter(); },
228 "start interactive filtering" });
230 229
231 cursed::Prompt prompt;
232 cursed::Track tmpTrack;
233 tmpTrack.addItem(&title);
234 tmpTrack.addItem(&filterList);
235 tmpTrack.addItem(&prompt);
230 return normalMode;
231 }
236 232
237 std::vector<cursed::ColorTree> filtered(lines.cbegin(), lines.cend());
238 auto update = [&](const std::wstring &filter) {
239 if (filter.empty()) {
240 filtered.assign(lines.cbegin(), lines.cend());
241 filterList.setItems(filtered);
242 return;
243 }
233 void
234 PipeDial::filter()
235 {
236 cursed::List filterList;
237 filterList.setItems({ lines.cbegin(), lines.cend() });
238
239 cursed::Prompt prompt;
240 cursed::Track tmpTrack;
241 tmpTrack.addItem(&title);
242 tmpTrack.addItem(&filterList);
243 tmpTrack.addItem(&prompt);
244
245 std::vector<cursed::ColorTree> filtered(lines.cbegin(), lines.cend());
246 auto update = [&](const std::wstring &filter) {
247 if (filter.empty()) {
248 filtered.assign(lines.cbegin(), lines.cend());
249 filterList.setItems(filtered);
250 return;
251 }
244 252
245 filtered.clear();
246 for (int tries = 0; tries < 2; ++tries) {
247 bool caseSensitive = (tries == 0);
248 for (const std::wstring &line : lines) {
249 auto pos = find(line, filter, caseSensitive);
250 if (pos == std::wstring::npos) {
251 continue;
252 }
253
254 cursed::ColorTree item;
255 decltype(pos) from = 0U;
256 while (pos != std::wstring::npos) {
257 item += line.substr(from, pos - from);
258 item += matchHi(line.substr(pos, filter.size()));
259
260 from = pos + filter.size();
261 pos = find(line, filter, caseSensitive, from);
262 }
263 item += line.substr(from);
264 filtered.emplace_back(std::move(item));
253 filtered.clear();
254 for (int tries = 0; tries < 2; ++tries) {
255 bool caseSensitive = (tries == 0);
256 for (const std::wstring &line : lines) {
257 auto pos = find(line, filter, caseSensitive);
258 if (pos == std::wstring::npos) {
259 continue;
265 260 } }
266 261
267 if (!filtered.empty()) {
268 break;
262 cursed::ColorTree item;
263 decltype(pos) from = 0U;
264 while (pos != std::wstring::npos) {
265 item += line.substr(from, pos - from);
266 item += matchHi(line.substr(pos, filter.size()));
267
268 from = pos + filter.size();
269 pos = find(line, filter, caseSensitive, from);
269 270 } }
271 item += line.substr(from);
272 filtered.emplace_back(std::move(item));
270 273 } }
271 filterList.setItems(filtered);
272 };
273 274
274 cursedrl::PromptRequest request(screen, prompt);
275 request.setOnInputChanged(update);
276 screen.replaceTopWidget(&tmpTrack);
277 if (request.prompt(L"&/", L"")) {
278 list.setItems(std::move(filtered));
275 if (!filtered.empty()) {
276 break;
277 }
279 278 } }
280 screen.replaceTopWidget(&track);
281 }, "start interactive filtering" });
282
283 return normalMode;
279 filterList.setItems(filtered);
280 };
281
282 cursedrl::PromptRequest request(screen, prompt);
283 request.setOnInputChanged(update);
284 screen.replaceTopWidget(&tmpTrack);
285 if (request.prompt(L"&/", L"")) {
286 list.setItems(std::move(filtered));
287 }
288 screen.replaceTopWidget(&track);
284 289 } }
285 290
286 291 // A `std::string::find()`-like function that can do case-insensitive searches. // A `std::string::find()`-like function that can do case-insensitive searches.
File src/PipeDial.hpp changed (mode: 100644) (index d796eab..63e1125)
... ... private:
57 57 // Constructs help mode. // Constructs help mode.
58 58 vle::Mode buildHelpMode(); vle::Mode buildHelpMode();
59 59
60 // Handles line filtering.
61 void filter();
62
60 63 private: private:
61 64 std::vector<std::wstring> lines; // Original list of lines. std::vector<std::wstring> lines; // Original list of lines.
62 65
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/pipedial

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/pipedial

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master