Skip to content

Commit

Permalink
Merge pull request #25 from RossComputerGuy/feat/drop-editline
Browse files Browse the repository at this point in the history
feat(libcmd): drop editline
  • Loading branch information
RossComputerGuy authored Feb 11, 2025
2 parents 59b0942 + 4b9c304 commit c5621b9
Show file tree
Hide file tree
Showing 6 changed files with 1 addition and 141 deletions.
1 change: 0 additions & 1 deletion packaging/hydra.nix
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ in
self: super: {
nix-cmd = super.nix-cmd.override {
enableMarkdown = false;
readlineFlavor = "readline";
};
}
)
Expand Down
14 changes: 0 additions & 14 deletions src/libcmd/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,6 @@ configdata.set('HAVE_LOWDOWN', lowdown.found().to_int())
# The API changed slightly around terminal initialization.
configdata.set('HAVE_LOWDOWN_1_4', lowdown.version().version_compare('>= 1.4.0').to_int())

readline_flavor = get_option('readline-flavor')
if readline_flavor == 'editline'
editline = dependency('libeditline', 'editline', version : '>=1.14')
deps_private += editline
elif readline_flavor == 'zig'
configdata.set(
'USE_ZIG_REPL',
1,
description: 'Use zig implementation for repl',
)
else
error('illegal editline flavor', readline_flavor)
endif

config_h = configure_file(
configuration : configdata,
output : 'config-cmd.hh',
Expand Down
8 changes: 0 additions & 8 deletions src/libcmd/meson.options
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,3 @@ option(
type: 'feature',
description: 'Enable Markdown rendering in the Nix binary (requires lowdown)',
)

option(
'readline-flavor',
type : 'combo',
choices : ['editline', 'zig'],
value : 'zig',
description : 'Which library to use for nice line editing with the Nix language REPL',
)
20 changes: 1 addition & 19 deletions src/libcmd/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
nix-expr,
nix-flake,
nix-main,
editline,
lowdown,
nlohmann_json,

Expand All @@ -20,14 +19,6 @@

# Whether to enable Markdown rendering in the Nix binary.
enableMarkdown ? !stdenv.hostPlatform.isWindows,

# Which interactive line editor library to use for Nix's repl.
#
# Currently supported choices are:
#
# - editline (default)
# - readline
readlineFlavor ? if stdenv.hostPlatform.isLinux then "zig" else "editline",
}:

let
Expand All @@ -53,15 +44,7 @@ mkMesonLibrary (finalAttrs: {
(fileset.fileFilter (file: file.hasExt "zig") ./.)
];

buildInputs = [
(
{
inherit editline;
zig = null;
}
.${readlineFlavor}
)
] ++ lib.optional enableMarkdown lowdown;
buildInputs = lib.optional enableMarkdown lowdown;

propagatedBuildInputs = [
nix-util
Expand All @@ -86,7 +69,6 @@ mkMesonLibrary (finalAttrs: {

mesonFlags = [
(lib.mesonEnable "markdown" enableMarkdown)
(lib.mesonOption "readline-flavor" readlineFlavor)
];

meta = {
Expand Down
98 changes: 0 additions & 98 deletions src/libcmd/repl-interacter.cc
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
#include <cstdio>

#ifdef USE_ZIG_REPL
extern "C" char* readline(const char* prompt);
#else
// editline < 1.15.2 don't wrap their API for C++ usage
// (added in https://github.com/troglobit/editline/commit/91398ceb3427b730995357e9d120539fb9bb7461).
// This results in linker errors due to to name-mangling of editline C symbols.
// For compatibility with these versions, we wrap the API here
// (wrapping multiple times on newer versions is no problem).
extern "C" {
#include <editline.h>
}
#endif

#include "signals.hh"
#include "finally.hh"
Expand All @@ -34,97 +23,17 @@ void sigintHandler(int signo)

static detail::ReplCompleterMixin * curRepl; // ugly

static char * completionCallback(char * s, int * match)
{
auto possible = curRepl->completePrefix(s);
if (possible.size() == 1) {
*match = 1;
auto * res = strdup(possible.begin()->c_str() + strlen(s));
if (!res)
throw Error("allocation failure");
return res;
} else if (possible.size() > 1) {
auto checkAllHaveSameAt = [&](size_t pos) {
auto & first = *possible.begin();
for (auto & p : possible) {
if (p.size() <= pos || p[pos] != first[pos])
return false;
}
return true;
};
size_t start = strlen(s);
size_t len = 0;
while (checkAllHaveSameAt(start + len))
++len;
if (len > 0) {
*match = 1;
auto * res = strdup(std::string(*possible.begin(), start, len).c_str());
if (!res)
throw Error("allocation failure");
return res;
}
}

*match = 0;
return nullptr;
}

static int listPossibleCallback(char * s, char *** avp)
{
auto possible = curRepl->completePrefix(s);

if (possible.size() > (std::numeric_limits<int>::max() / sizeof(char *)))
throw Error("too many completions");

int ac = 0;
char ** vp = nullptr;

auto check = [&](auto * p) {
if (!p) {
if (vp) {
while (--ac >= 0)
free(vp[ac]);
free(vp);
}
throw Error("allocation failure");
}
return p;
};

vp = check((char **) malloc(possible.size() * sizeof(char *)));

for (auto & p : possible)
vp[ac++] = check(strdup(p.c_str()));

*avp = vp;

return ac;
}

ReadlineLikeInteracter::Guard ReadlineLikeInteracter::init(detail::ReplCompleterMixin * repl)
{
// Allow nix-repl specific settings in .inputrc
#ifndef USE_ZIG_REPL
rl_readline_name = "nix-repl";
#endif
try {
createDirs(dirOf(historyFile));
} catch (SystemError & e) {
logWarning(e.info());
}
#ifndef USE_ZIG_REPL
el_hist_size = 1000;
#endif
#ifndef USE_ZIG_REPL
read_history(historyFile.c_str());
#endif
auto oldRepl = curRepl;
curRepl = repl;
Guard restoreRepl([oldRepl] { curRepl = oldRepl; });
#ifndef USE_ZIG_REPL
rl_set_complete_func(completionCallback);
rl_set_list_possib_func(listPossibleCallback);
#endif
return restoreRepl;
}

Expand Down Expand Up @@ -197,11 +106,4 @@ bool ReadlineLikeInteracter::getLine(std::string & input, ReplPromptType promptT
return true;
}

ReadlineLikeInteracter::~ReadlineLikeInteracter()
{
#ifndef USE_ZIG_REPL
write_history(historyFile.c_str());
#endif
}

};
1 change: 0 additions & 1 deletion src/libcmd/repl-interacter.hh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public:
}
virtual Guard init(detail::ReplCompleterMixin * repl) override;
virtual bool getLine(std::string & input, ReplPromptType promptType) override;
virtual ~ReadlineLikeInteracter() override;
};

};

0 comments on commit c5621b9

Please sign in to comment.