xaizek / libcursedrl (License: GPLv3+) (since 2019-03-24)
libcursed extension for integration with readline.
Commit 6c705b17209bb776f1ab6a89760615f5f56da74d

Implement context for history
This allows having multiple independent histories and switching between
them easily.
Author: xaizek
Author date (UTC): 2022-06-25 21:27
Committer name: xaizek
Committer date (UTC): 2022-06-25 21:27
Parent(s): d794ea979d1251164c1cae6c5c85b345b5f9cecb
Signing key: 99DC5E4DB05F6BE2
Tree: 6e23e2dbe19d362a2e282d7a337b1f3b0ae94407
File Lines added Lines deleted
PromptRequest.cpp 31 0
PromptRequest.hpp 3 0
File PromptRequest.cpp changed (mode: 100644) (index 7cfc31f..71a1391)
27 27 #include <memory> #include <memory>
28 28 #include <stdexcept> #include <stdexcept>
29 29 #include <string> #include <string>
30 #include <unordered_map>
30 31 #include <utility> #include <utility>
31 32
32 33 #include <readline/history.h> #include <readline/history.h>
 
... ... private:
93 94
94 95 } }
95 96
97 void
98 PromptHistory::switchContext(const std::string &name)
99 {
100 static struct {
101 std::unordered_map<std::string, HISTORY_STATE> contexts;
102 std::string current;
103 } state;
104
105 auto &s = state;
106
107 if (name == s.current) {
108 return;
109 }
110
111 if (HISTORY_STATE *stateSnapshot = history_get_history_state()) {
112 s.contexts[s.current] = *stateSnapshot;
113 std::free(stateSnapshot);
114 }
115
116 auto it = s.contexts.find(name);
117 if (it != s.contexts.end()) {
118 history_set_history_state(&it->second);
119 } else {
120 HISTORY_STATE emptyState = {};
121 history_set_history_state(&emptyState);
122 }
123
124 s.current = name;
125 }
126
96 127 void void
97 128 PromptHistory::limit(int limit) PromptHistory::limit(int limit)
98 129 { stifle_history(limit); } { stifle_history(limit); }
File PromptRequest.hpp changed (mode: 100644) (index 86e37a1..b341650)
... ... namespace cursedrl {
37 37
38 38 // Groups history management functions. // Groups history management functions.
39 39 namespace PromptHistory { namespace PromptHistory {
40 // Saves current context ("" initially) and loads specified one.
41 void switchContext(const std::string &name);
42
40 43 // Puts a limit on maximum number of history entries. // Puts a limit on maximum number of history entries.
41 44 void limit(int limit); void limit(int limit);
42 45 // Loads history from the file. // Loads history from the file.
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/libcursedrl

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

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