Skip to content

Commit

Permalink
refactor: repl prompts are now the job of the interacter
Browse files Browse the repository at this point in the history
  • Loading branch information
lf- committed Feb 26, 2024
1 parent bf5c79e commit 7abbeed
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
19 changes: 15 additions & 4 deletions src/libcmd/repl-interacter.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include "file-system.hh"
#include "libcmd/repl.hh"
#include <cstdio>

#ifdef USE_READLINE
Expand All @@ -19,6 +17,8 @@ extern "C" {
#include "signals.hh"
#include "finally.hh"
#include "repl-interacter.hh"
#include "file-system.hh"
#include "libcmd/repl.hh"

namespace nix {

Expand Down Expand Up @@ -124,7 +124,18 @@ ReadlineLikeInteracter::Guard ReadlineLikeInteracter::init(detail::ReplCompleter
return restoreRepl;
}

bool ReadlineLikeInteracter::getLine(std::string & input, const std::string & prompt)
static constexpr const char * promptForType(ReplPromptType promptType)
{
switch (promptType) {
case ReplPromptType::ReplPrompt:
return "nix-repl> ";
case ReplPromptType::ContinuationPrompt:
return " ";
}
assert(false);
}

bool ReadlineLikeInteracter::getLine(std::string & input, ReplPromptType promptType)
{
struct sigaction act, old;
sigset_t savedSignalMask, set;
Expand All @@ -151,7 +162,7 @@ bool ReadlineLikeInteracter::getLine(std::string & input, const std::string & pr

setupSignals();
Finally resetTerminal([&]() { rl_deprep_terminal(); });
char * s = readline(prompt.c_str());
char * s = readline(promptForType(promptType));
Finally doFree([&]() { free(s); });
restoreSignals();

Expand Down
9 changes: 7 additions & 2 deletions src/libcmd/repl-interacter.hh
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@
#include <string>

namespace nix {
enum class ReplPromptType {
ReplPrompt,
ContinuationPrompt,
};

class ReplInteracter
{
public:
using Guard = Finally<std::function<void()>>;

virtual Guard init(detail::ReplCompleterMixin * repl) = 0;
/** Returns a boolean of whether the interacter got EOF */
virtual bool getLine(std::string & input, const std::string & prompt) = 0;
virtual bool getLine(std::string & input, ReplPromptType promptType) = 0;
virtual ~ReplInteracter(){};
};

Expand All @@ -27,7 +32,7 @@ public:
{
}
virtual Guard init(detail::ReplCompleterMixin * repl) override;
virtual bool getLine(std::string & input, const std::string & prompt) override;
virtual bool getLine(std::string & input, ReplPromptType promptType) override;
virtual ~ReadlineLikeInteracter() override;
};

Expand Down
2 changes: 1 addition & 1 deletion src/libcmd/repl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ ReplExitStatus NixRepl::mainLoop()
logger->pause();
// When continuing input from previous lines, don't print a prompt, just align to the same
// number of chars as the prompt.
if (!interacter->getLine(input, input.empty() ? "nix-repl> " : " ")) {
if (!interacter->getLine(input, input.empty() ? ReplPromptType::ReplPrompt : ReplPromptType::ContinuationPrompt)) {
// Ctrl-D should exit the debugger.
state->debugStop = false;
logger->cout("");
Expand Down

0 comments on commit 7abbeed

Please sign in to comment.