diff options
author | jade <lix@jade.fyi> | 2024-03-11 15:12:09 -0600 |
---|---|---|
committer | Gerrit Code Review <gerrit@lix> | 2024-03-11 15:12:09 -0600 |
commit | b06a39211460033305c5264a456161c75ed88425 (patch) | |
tree | 15202d692f95b72c0884df23f3c9840618cd2ab7 /src | |
parent | dd05106d1cb94485cb6c6bd06a09b4ebbcc67435 (diff) | |
parent | 1758a6ef25cb74090642dd5e7a384d210041875e (diff) |
Merge "refactor: repl prompts are now the job of the interacter" into main
Diffstat (limited to 'src')
-rw-r--r-- | src/libcmd/repl-interacter.cc | 15 | ||||
-rw-r--r-- | src/libcmd/repl-interacter.hh | 4 | ||||
-rw-r--r-- | src/libcmd/repl.cc | 2 |
3 files changed, 16 insertions, 5 deletions
diff --git a/src/libcmd/repl-interacter.cc b/src/libcmd/repl-interacter.cc index 93d060e30..18c1cad0f 100644 --- a/src/libcmd/repl-interacter.cc +++ b/src/libcmd/repl-interacter.cc @@ -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; @@ -150,7 +161,7 @@ bool ReadlineLikeInteracter::getLine(std::string & input, const std::string & pr }; setupSignals(); - char * s = readline(prompt.c_str()); + char * s = readline(promptForType(promptType)); Finally doFree([&]() { free(s); }); restoreSignals(); diff --git a/src/libcmd/repl-interacter.hh b/src/libcmd/repl-interacter.hh index e549bab36..cc70efd07 100644 --- a/src/libcmd/repl-interacter.hh +++ b/src/libcmd/repl-interacter.hh @@ -28,7 +28,7 @@ public: 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(){}; }; @@ -41,7 +41,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; }; diff --git a/src/libcmd/repl.cc b/src/libcmd/repl.cc index 243324c61..5809ab285 100644 --- a/src/libcmd/repl.cc +++ b/src/libcmd/repl.cc @@ -196,7 +196,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(""); |