Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : */
8 :
9 : #ifndef COMPLETE_H
10 : #define COMPLETE_H
11 :
12 : #include <functional>
13 : #include <map>
14 :
15 : #include "coloredkdbio.hpp"
16 : #include <command.hpp>
17 : #include <kdb.hpp>
18 :
19 : class CompleteCommand : public Command
20 : {
21 :
22 : public:
23 : CompleteCommand ();
24 : ~CompleteCommand ();
25 :
26 7 : virtual std::string getShortOptions () override
27 : {
28 21 : return "mM0";
29 : }
30 :
31 7 : virtual std::string getSynopsis () override
32 : {
33 21 : return "<path>";
34 : }
35 :
36 85 : virtual std::string getShortHelpText () override
37 : {
38 255 : return "Show suggestions how to complete key names.";
39 : }
40 :
41 7 : virtual std::string getLongHelpText () override
42 : {
43 : return "Suggestions will include existing key names, path segments of existing key names and mountpoints.\n"
44 21 : "Additionally, the output indicates whether the given path is a node or a leaf in the hierarchy of keys.";
45 : }
46 :
47 : virtual int execute (Cmdline const & cmdline) override;
48 :
49 : private:
50 : void complete (std::string const & argument, Cmdline const & cmdLine);
51 : void completeNormal (std::string const & argument, kdb::Key const & parsedArgument, Cmdline const & cmdLine);
52 :
53 : const std::map<kdb::Key, std::pair<int, int>> analyze (const kdb::KeySet & ks, Cmdline const & cmdLine);
54 : void printResults (
55 : kdb::Key const & root, const int minDepth, const int maxDepth, Cmdline const & cmdLine,
56 : std::map<kdb::Key, std::pair<int, int>> const & result,
57 : std::function<bool(std::pair<kdb::Key, std::pair<int, int>> const & current)> const & filterPredicate,
58 : std::function<void(std::pair<kdb::Key, std::pair<int, int>> const & current, const bool verbose)> const & resultFilter);
59 :
60 : // helper functions
61 : int getKeyDepth (kdb::Key const & key);
62 : const kdb::Key getParentKey (kdb::Key const & key);
63 : kdb::KeySet getKeys (kdb::Key root, bool cutAtRoot, Cmdline const & cl);
64 : bool shallShowNextLevel (std::string const & argument);
65 :
66 : void addMountpoints (kdb::KeySet & ks, kdb::Key const & root, Cmdline const & cl);
67 : void addNamespaces (std::map<kdb::Key, std::pair<int, int>> & hierarchy, Cmdline const & cl);
68 : void increaseCount (std::map<kdb::Key, std::pair<int, int>> & hierarchy, kdb::Key const & key,
69 : std::function<int(int)> const & depthIncreaser);
70 :
71 : // print functions
72 : static void printBookmarkResult (std::pair<kdb::Key, std::pair<int, int>> const & current, const bool verbose);
73 : static void printResult (std::pair<kdb::Key, std::pair<int, int>> const & current, const bool verbose);
74 :
75 : // filter functions
76 : static bool filterDepth (const int minDepth, const int maxDepth, std::pair<kdb::Key, std::pair<int, int>> const & current);
77 : static bool filterCascading (std::string const & argument, std::pair<kdb::Key, std::pair<int, int>> const & current);
78 : static bool filterName (std::string const & argument, std::pair<kdb::Key, std::pair<int, int>> const & current);
79 : static bool filterBookmarks (std::string const & bookmarkName, std::pair<kdb::Key, std::pair<int, int>> const & current);
80 : };
81 :
82 : #endif
|