Skip to content

Commit

Permalink
History is now a singleton.
Browse files Browse the repository at this point in the history
This simplifies different components accessing the history database.
  • Loading branch information
chrisant996 committed May 31, 2022
1 parent 377a738 commit 191807b
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 52 deletions.
41 changes: 15 additions & 26 deletions clink/app/src/host/host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,6 @@ host::~host()
delete m_prompt_filter;
delete m_suggester;
delete m_lua;
delete m_history;
delete m_printer;
terminal_destroy(m_terminal);
}
Expand Down Expand Up @@ -464,18 +463,6 @@ void host::pop_queued_line()
m_char_cursor = 0;
}

//------------------------------------------------------------------------------
int host::add_history(const char* line)
{
return !!m_history->add(line);
}

//------------------------------------------------------------------------------
int host::remove_history(int rl_history_index, const char* line)
{
return !!m_history->remove(rl_history_index, line);
}

//------------------------------------------------------------------------------
void host::filter_prompt()
{
Expand Down Expand Up @@ -861,28 +848,29 @@ bool host::edit_line(const char* prompt, const char* rprompt, str_base& out, boo

// Initialize history before filtering the prompt, so that the Lua history
// APIs can work.
history_db* history = history_database::get();
if (init_history)
{
if (m_history &&
((g_save_history.get() != m_history->has_bank(bank_master)) ||
m_history->is_stale_name()))
if (history &&
((g_save_history.get() != history->has_bank(bank_master)) ||
history->is_stale_name()))
{
delete m_history;
m_history = 0;
delete history;
history = nullptr;
}

if (!m_history)
if (!history)
{
dbg_ignore_scope(snapshot, "History");
str<> history_path;
app->get_history_path(history_path);
m_history = new history_db(history_path.c_str(), app->get_id(), g_save_history.get());
history = new history_database(history_path.c_str(), app->get_id(), g_save_history.get());
}

if (m_history)
if (history)
{
m_history->initialise();
m_history->load_rl_history();
history->initialise();
history->load_rl_history();
}
}

Expand Down Expand Up @@ -994,7 +982,7 @@ bool host::edit_line(const char* prompt, const char* rprompt, str_base& out, boo

// Handle history event expansion. expand() is a static method,
// so can call it even when m_history is nullptr.
if (m_history->expand(out.c_str(), out) == history_db::expand_print)
if (history->expand(out.c_str(), out) == history_db::expand_print)
{
puts(out.c_str());
out.clear();
Expand Down Expand Up @@ -1030,8 +1018,9 @@ bool host::edit_line(const char* prompt, const char* rprompt, str_base& out, boo
}

// Add the line to the history.
if (add_history)
m_history->add(out.c_str());
assert(history);
if (add_history && history)
history->add(out.c_str());
break;
}

Expand Down
3 changes: 0 additions & 3 deletions clink/app/src/host/host.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ class host : public host_callbacks
void cleanup_after_signal();

// host_callbacks:
int add_history(const char* line) override;
int remove_history(int rl_history_index, const char* line) override;
void filter_prompt() override;
void filter_transient_prompt(bool final) override;
bool can_suggest(const line_state& line) override;
Expand Down Expand Up @@ -90,7 +88,6 @@ class host : public host_callbacks
doskey m_doskey;
terminal m_terminal;
printer* m_printer;
history_db* m_history = nullptr;
host_lua* m_lua = nullptr;
prompt_filter* m_prompt_filter = nullptr;
suggester* m_suggester = nullptr;
Expand Down
8 changes: 8 additions & 0 deletions clink/lib/include/lib/history_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#pragma once

#include <core/str_iter.h>
#include <core/singleton.h>

#include <vector>

Expand Down Expand Up @@ -167,4 +168,11 @@ template <int S> history_db::iter history_db::read_lines(char (&buffer)[S])
return read_lines(buffer, S);
}

//------------------------------------------------------------------------------
class history_database : public history_db, public singleton<history_database>
{
public:
history_database(const char* path, int id, bool use_master_bank);
};

#define DIAG(fmt, ...) do { if (m_diagnostic) fprintf(stderr, fmt, ##__VA_ARGS__); } while (false)
2 changes: 0 additions & 2 deletions clink/lib/include/lib/host_callbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ enum class recognition : char;
class host_callbacks
{
public:
virtual int add_history(const char* line) = 0;
virtual int remove_history(int rl_history_index, const char* line) = 0;
virtual void filter_prompt() = 0;
virtual void filter_transient_prompt(bool final) = 0;
virtual bool can_suggest(const line_state& line) = 0;
Expand Down
8 changes: 8 additions & 0 deletions clink/lib/src/history_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1828,6 +1828,14 @@ bool history_db::is_stale_name() const



//------------------------------------------------------------------------------
history_database::history_database(const char* path, int id, bool use_master_bank)
: history_db(path, id, use_master_bank)
{
}



//------------------------------------------------------------------------------
bool expand_history(const char* in, str_base& out)
{
Expand Down
18 changes: 0 additions & 18 deletions clink/lib/src/line_editor_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,24 +151,6 @@ void set_prompt(const char* prompt, const char* rprompt, bool redisplay)



//------------------------------------------------------------------------------
int host_add_history(int, const char* line)
{
if (!s_callbacks)
return 0;

return s_callbacks->add_history(line);
}

//------------------------------------------------------------------------------
int host_remove_history(int rl_history_index, const char* line)
{
if (!s_callbacks)
return 0;

return s_callbacks->remove_history(rl_history_index, line);
}

//------------------------------------------------------------------------------
void host_filter_prompt()
{
Expand Down
18 changes: 17 additions & 1 deletion clink/lib/src/rl/rl_commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "rl_commands.h"
#include "doskey.h"
#include "textlist_impl.h"
#include "history_db.h"

#include "rl_suggestions.h"

Expand Down Expand Up @@ -89,7 +90,6 @@ extern line_buffer* g_rl_buffer;
extern word_collector* g_word_collector;
extern editor_module::result* g_result;
extern void host_cmd_enqueue_lines(std::list<str_moveable>& lines, bool hide_prompt, bool show_line);
extern int host_add_history(int, const char* line);
extern void host_get_app_context(int& id, str_base& binaries, str_base& profile, str_base& scripts);
extern "C" int show_cursor(int visible);
extern int ellipsify(const char* in, int limit, str_base& out, bool expand_ctrl);
Expand Down Expand Up @@ -231,6 +231,22 @@ static void get_word_bounds(const line_buffer& buffer, int* left, int* right)



//------------------------------------------------------------------------------
int host_add_history(int, const char* line)
{
history_database* h = history_database::get();
return h && h->add(line);
}

//------------------------------------------------------------------------------
int host_remove_history(int rl_history_index, const char* line)
{
history_database* h = history_database::get();
return h && h->remove(rl_history_index, line);
}



//------------------------------------------------------------------------------
static int s_cua_anchor = -1;

Expand Down
4 changes: 4 additions & 0 deletions clink/lib/src/rl/rl_commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ bool is_force_reload_scripts();
void clear_force_reload_scripts();
int force_reload_scripts();

//------------------------------------------------------------------------------
int host_add_history(int, const char* line);
int host_remove_history(int rl_history_index, const char* line);

//------------------------------------------------------------------------------
int show_rl_help(int, int);
int show_rl_help_raw(int, int);
Expand Down
2 changes: 0 additions & 2 deletions clink/lib/src/rl/rl_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ extern int _rl_last_v_pos;

extern int clink_diagnostics(int, int);

extern int host_add_history(int rl_history_index, const char* line);
extern int host_remove_history(int rl_history_index, const char* line);
extern void host_send_event(const char* event_name);
extern void host_cleanup_after_signal();
extern int macro_hook_func(const char* macro);
Expand Down

0 comments on commit 191807b

Please sign in to comment.