Skip to content

Commit

Permalink
Merge pull request #1018 from kuzudb/multi-line-CLI
Browse files Browse the repository at this point in the history
Added multi-line query functionality
  • Loading branch information
aziz-mu committed Nov 15, 2022
2 parents a6b3009 + b83cf5b commit 78d938a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 15 deletions.
32 changes: 26 additions & 6 deletions tools/shell/embedded_shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace main {

// prompt for user input
const char* PROMPT = "kuzu> ";
const char* ALTPROMPT = "..> ";
// file to read/write shell history
const char* HISTORY_PATH = "history.txt";

Expand Down Expand Up @@ -50,6 +51,9 @@ const regex specialChars{R"([-[\]{}()*+?.,\^$|#\s])"};
vector<string> nodeTableNames;
vector<string> relTableNames;

bool continueLine = false;
string currLine;

void EmbeddedShell::updateTableNames() {
nodeTableNames.clear();
relTableNames.clear();
Expand Down Expand Up @@ -172,8 +176,15 @@ EmbeddedShell::EmbeddedShell(

void EmbeddedShell::run() {
char* line;
while ((line = linenoise(PROMPT)) != nullptr) {
string query;
stringstream ss;
while ((line = linenoise(continueLine ? ALTPROMPT : PROMPT)) != nullptr) {
auto lineStr = string(line);
if (continueLine) {
lineStr = currLine + lineStr;
currLine = "";
continueLine = false;
}
if (line == shellCommand.HELP) {
printHelp();
} else if (line == shellCommand.CLEAR) {
Expand All @@ -197,11 +208,20 @@ void EmbeddedShell::run() {
} else if (lineStr.rfind(shellCommand.SHOW_REL) == 0) {
printRelSchema(lineStr.substr(shellCommand.SHOW_REL.length()));
} else if (!lineStr.empty()) {
auto queryResult = conn->query(lineStr);
if (queryResult->isSuccess()) {
printExecutionResult(*queryResult);
} else {
printf("Error: %s\n", queryResult->getErrorMessage().c_str());
ss.clear();
ss.str(lineStr);
while (getline(ss, query, ';')) {
if (ss.eof() && ss.peek() == -1) {
continueLine = true;
currLine += query + " ";
} else {
auto queryResult = conn->query(query);
if (queryResult->isSuccess()) {
printExecutionResult(*queryResult);
} else {
printf("Error: %s\n", queryResult->getErrorMessage().c_str());
}
}
}
}
updateTableNames();
Expand Down
42 changes: 33 additions & 9 deletions tools/shell/linenoise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
#include <unistd.h>

#include <cstddef>
#include <string>

#include <sys/ioctl.h>

Expand Down Expand Up @@ -181,6 +182,9 @@ static void linenoiseAtExit(void);
int linenoiseHistoryAdd(const char* line);
static void refreshLine(struct linenoiseState* l);

std::string oldInput = "";
bool inputLeft;

/* Debugging macro. */
#if 0
FILE *lndebug_fp = NULL;
Expand Down Expand Up @@ -680,6 +684,14 @@ static void refreshLine(struct linenoiseState* l) {
refreshSingleLine(l);
}

bool pastedInput(int ifd) {
struct pollfd fd {
ifd, POLLIN, 0
};
int isPasted = poll(&fd, 1, 0);
return (isPasted != 0);
}

/* Insert the character 'c' at cursor current position.
*
* On error writing to the terminal -1 is returned, otherwise 0. */
Expand All @@ -695,11 +707,7 @@ int linenoiseEditInsert(struct linenoiseState* l, char c) {
if (write(l->ofd, &d, 1) == -1)
return -1;
}
struct pollfd fd {
l->ifd, POLLIN, 0
};
int pastedInput = poll(&fd, 1, 0);
if (pastedInput == 0) {
if (!pastedInput(l->ifd)) {
refreshLine(l);
}
} else {
Expand Down Expand Up @@ -878,9 +886,18 @@ static int linenoiseEdit(
int nread;
char seq[3];

nread = read(l.ifd, &c, 1);
if (nread <= 0)
return l.len;
if (inputLeft) {
c = oldInput[0];
oldInput.erase(0, 1);
if (!c) {
inputLeft = false;
}
}
if (!inputLeft) {
nread = read(l.ifd, &c, 1);
if (nread <= 0)
return l.len;
}

/* Only autocomplete when the callback is set. It returns < 0 when
* there was an error reading from fd. Otherwise it will return the
Expand All @@ -894,9 +911,16 @@ static int linenoiseEdit(
if (c == 0)
continue;
}

switch (c) {
case ENTER: /* enter */
if (pastedInput(l.ifd)) {
linenoiseEditInsert(&l, ' ');
inputLeft = true;
while (pastedInput(l.ifd)) {
read(l.ifd, &c, 1);
oldInput += c;
}
}
history_len--;
free(history[history_len]);
if (mlmode)
Expand Down

0 comments on commit 78d938a

Please sign in to comment.