Skip to content

Commit

Permalink
sdlconsole: support load from saved command history
Browse files Browse the repository at this point in the history
  • Loading branch information
dhthwy committed Nov 19, 2024
1 parent 06363e2 commit b5015b5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 28 deletions.
21 changes: 20 additions & 1 deletion library/Console-sdl-impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1531,7 +1531,7 @@ class WidgetContext {
auto title = props.get<std::string>(property::WINDOW_MAIN_TITLE, "SDL Console");
SDL_Rect create_rect = props.get<SDL_Rect>(property::WINDOW_MAIN_CREATE_RECT,
SDL_Rect{SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480});
auto flags = SDL_WINDOW_RESIZABLE;
auto flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN;

SDL_Window* handle = sdl_tsd.CreateWindow(title.c_str(), create_rect.x, create_rect.y, create_rect.w, create_rect.h, flags);
if (!handle) {
Expand Down Expand Up @@ -1758,6 +1758,14 @@ class Prompt : public Widget {
}
}

void set_command_history(std::deque<std::u32string> saved_history)
{
std::swap(history, saved_history);
saved_history.clear();
input = &history.emplace_back(U"");
history_index = history.size() - 1;
}

void new_command()
{
emit(InternalEventType::new_command, input);
Expand Down Expand Up @@ -3281,6 +3289,17 @@ int SDLConsole::get_line(std::string& buf)
return -1;
}

void SDLConsole::set_command_history(std::vector<std::string>& entries)
{
std::deque<std::u32string> my_entries;
for (auto& entry : entries) {
my_entries.push_front(text::from_utf8(entry));
}
push_api_task([this, my_entries = std::move(my_entries)] {
impl->outpane().prompt.set_command_history(my_entries);
});
}

SDLConsole& SDLConsole::get_console()
{
static SDLConsole instance;
Expand Down
48 changes: 21 additions & 27 deletions library/Console-sdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <cstdio>
#include <iostream>
#include <string>
#include <SDL_pixels.h>

Expand Down Expand Up @@ -182,9 +181,19 @@ namespace DFHack

int lineedit(const std::string& prompt, std::string& output, CommandHistory & ch)
{
if (!con.state.is_active()) {
fputs("SDLConsole is inactive\n", stderr);
return Console::SHUTDOWN;
static bool did_set_history = false;

// I don't believe these checks are necessary.
// unless, for some reason, fiothread is inited before the console.
if (con.state.is_inactive() && !con.state.is_shutdown()) {
return Console::RETRY;
}
// kludge. This is the only place to set it?
if (!did_set_history) {
std::vector<std::string> hist;
ch.getEntries(hist);
con.set_command_history(hist);
did_set_history = true;
}

if (prompt != this->prompt) {
Expand All @@ -195,21 +204,12 @@ namespace DFHack
int ret = con.get_line(output);
if (ret == 0)
return Console::RETRY;
else if (ret == -1)
return Console::SHUTDOWN;

if (ret == Console::FAILURE) {
fputs("SDLConsole is inactive (shutting down)\n", stderr);
}
return ret;
}

void begin_batch()
{
}

void end_batch()
{
}

void flush()
{
}
Expand Down Expand Up @@ -243,9 +243,6 @@ namespace DFHack
{
}

/// Waits given number of milliseconds before continuing.
void msleep(unsigned int msec);

int get_columns(void)
{
return con.get_columns();
Expand All @@ -257,7 +254,6 @@ namespace DFHack
}

SDLConsole& con;
bool in_batch{false};
std::string prompt; // current prompt string
};
}
Expand Down Expand Up @@ -310,22 +306,21 @@ bool Console::shutdown(void)
return true;
}

/*
* This should be for guarding against interleaving prints to the console.
* The begin_batch() and end_batch() pair does the job on its own.
*/
void Console::begin_batch()
{
wlock->lock();

if(inited.load())
d->begin_batch();
}

void Console::end_batch()
{
if(inited.load())
d->end_batch();

wlock->unlock();
}
/* XXX: Not implemented */

/* Don't think we need this? */
void Console::flush_proxy()
{
return;
Expand Down Expand Up @@ -441,6 +436,5 @@ void Console::cleanup()
INTERPOSE_HOOK(con_render_hook,render).apply(0);
// destroy() will change its state to inactive
d->con.destroy();
std::cerr << "SDLConsole destroyed" << std::endl;
inited.store(false);
}
3 changes: 3 additions & 0 deletions library/include/SDL_console.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <memory>
#include <atomic>
#include <optional>
#include <vector>

struct SDL_Color;
union SDL_Event;
Expand Down Expand Up @@ -88,6 +89,8 @@ class SDLConsole {
*/
[[nodiscard]] int get_line(std::string& buf);

void set_command_history(std::vector<std::string>& entries);

/*
* Retrieves an instance of the console.
* Currently supports only one instance.
Expand Down

0 comments on commit b5015b5

Please sign in to comment.