diff options
| author | Maxime Coste <mawww@kakoune.org> | 2019-06-05 23:19:27 +1000 |
|---|---|---|
| committer | Maxime Coste <mawww@kakoune.org> | 2019-06-23 12:05:09 +1000 |
| commit | e613292568e9f66628135b5a1f468cc9ae85106d (patch) | |
| tree | 17d5074e6cb17528baaf38ff3acaf817e221a1a2 /src/register_manager.hh | |
| parent | a9e778fcc7f06a27f2d06da225f08110c9e2dca9 (diff) | |
Use register to store prompt history
Diffstat (limited to 'src/register_manager.hh')
| -rw-r--r-- | src/register_manager.hh | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/register_manager.hh b/src/register_manager.hh index 4337f257..c463ad3a 100644 --- a/src/register_manager.hh +++ b/src/register_manager.hh @@ -20,6 +20,15 @@ public: virtual void set(Context& context, ConstArrayView<String> values) = 0; virtual ConstArrayView<String> get(const Context& context) = 0; + virtual const String& get_main(const Context& context, size_t main_index) = 0; + + struct RestoreInfo + { + std::vector<String> data; + size_t size; + }; + virtual RestoreInfo save(const Context&) = 0; + virtual void restore(Context&, const RestoreInfo&) = 0; }; // static value register, which can be modified @@ -39,6 +48,25 @@ public: else return ConstArrayView<String>(m_content); } + + const String& get_main(const Context& context, size_t main_index) override + { + return get(context)[std::min(main_index, m_content.size() - 1)]; + } + + RestoreInfo save(const Context& context) override + { + //std::unique_ptr<String[]> data{new String[m_content.size()]}; + //std::copy_n(m_content.data(), m_content.size(), data.get()); + auto content = get(context); + std::vector<String> data{content.begin(), content.end()}; + return {std::move(data), content.size()}; + } + + void restore(Context&, const RestoreInfo& info) override + { + m_content.assign(info.data.begin(), info.data.begin() + info.size); + } protected: Vector<String, MemoryDomain::Registers> m_content; }; @@ -63,11 +91,47 @@ public: return StaticRegister::get(context); } + void restore(Context& context, const RestoreInfo& info) override + { + set(context, info.data); + } + private: Getter m_getter; Setter m_setter; }; +// Register that is used to store some kind prompt history +class HistoryRegister : public StaticRegister +{ +public: + void set(Context&, ConstArrayView<String> values) override + { + for (auto& entry : values) + { + m_content.erase(std::remove(m_content.begin(), m_content.end(), entry), + m_content.end()); + m_content.push_back(entry); + } + } + + const String& get_main(const Context&, size_t) override + { + return m_content.empty() ? String::ms_empty : m_content.back(); + } + + RestoreInfo save(const Context&) override + { + return {{}, m_content.size()}; + } + + void restore(Context&, const RestoreInfo& info) override + { + if (info.size < m_content.size()) + m_content.resize(info.size); + } +}; + template<typename Func> std::unique_ptr<Register> make_dyn_reg(Func func) { @@ -93,6 +157,14 @@ public: { return ConstArrayView<String>(String::ms_empty); } + + const String& get_main(const Context&, size_t) override + { + return String::ms_empty; + } + + RestoreInfo save(const Context&) override { return {}; } + void restore(Context&, const RestoreInfo& info) override {} }; class RegisterManager : public Singleton<RegisterManager> |
