Add :quit command
This commit is contained in:
parent
a5dffb3d3d
commit
3567bdb514
1 changed files with 36 additions and 19 deletions
55
nix-repl.cc
55
nix-repl.cc
|
@ -39,7 +39,7 @@ struct NixRepl
|
||||||
void mainLoop(const Strings & args);
|
void mainLoop(const Strings & args);
|
||||||
void completePrefix(string prefix);
|
void completePrefix(string prefix);
|
||||||
bool getLine(string & line);
|
bool getLine(string & line);
|
||||||
void processLine(string line);
|
bool processLine(string line);
|
||||||
void loadFile(const Path & path);
|
void loadFile(const Path & path);
|
||||||
void addAttrsToScope(Value & attrs);
|
void addAttrsToScope(Value & attrs);
|
||||||
void addVarToScope(const Symbol & name, Value & v);
|
void addVarToScope(const Symbol & name, Value & v);
|
||||||
|
@ -95,10 +95,13 @@ void NixRepl::mainLoop(const Strings & args)
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
string line;
|
string line;
|
||||||
if (!getLine(line)) break;
|
if (!getLine(line)) {
|
||||||
|
std::cout << std::endl;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
processLine(removeWhitespace(line));
|
if (!processLine(removeWhitespace(line))) return;
|
||||||
} catch (Error & e) {
|
} catch (Error & e) {
|
||||||
printMsg(lvlError, "error: " + e.msg());
|
printMsg(lvlError, "error: " + e.msg());
|
||||||
} catch (Interrupted & e) {
|
} catch (Interrupted & e) {
|
||||||
|
@ -108,7 +111,6 @@ void NixRepl::mainLoop(const Strings & args)
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -255,11 +257,19 @@ bool isVarName(const string & s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void NixRepl::processLine(string line)
|
bool NixRepl::processLine(string line)
|
||||||
{
|
{
|
||||||
if (line == "") return;
|
if (line == "") return true;
|
||||||
|
|
||||||
string command = string(line, 0, 2);
|
string command, arg;
|
||||||
|
|
||||||
|
if (line[0] == ':') {
|
||||||
|
size_t p = line.find(' ');
|
||||||
|
command = string(line, 0, p);
|
||||||
|
if (p != string::npos) arg = removeWhitespace(string(line, p));
|
||||||
|
} else {
|
||||||
|
arg = line;
|
||||||
|
}
|
||||||
|
|
||||||
if (command == ":?") {
|
if (command == ":?") {
|
||||||
cout << "The following commands are available:\n"
|
cout << "The following commands are available:\n"
|
||||||
|
@ -270,30 +280,31 @@ void NixRepl::processLine(string line)
|
||||||
<< " :b <expr> Build derivation\n"
|
<< " :b <expr> Build derivation\n"
|
||||||
<< " :l <path> Load Nix expression and add it to scope\n"
|
<< " :l <path> Load Nix expression and add it to scope\n"
|
||||||
<< " :p <expr> Evaluate and print expression recursively\n"
|
<< " :p <expr> Evaluate and print expression recursively\n"
|
||||||
|
<< " :q Exit nix-repl\n"
|
||||||
<< " :s <expr> Build dependencies of derivation, then start nix-shell\n"
|
<< " :s <expr> Build dependencies of derivation, then start nix-shell\n"
|
||||||
<< " :t <expr> Describe result of evaluation\n";
|
<< " :t <expr> Describe result of evaluation\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (command == ":a") {
|
else if (command == ":a") {
|
||||||
Value v;
|
Value v;
|
||||||
evalString(string(line, 2), v);
|
evalString(arg, v);
|
||||||
addAttrsToScope(v);
|
addAttrsToScope(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (command == ":l") {
|
else if (command == ":l") {
|
||||||
state.resetFileCache();
|
state.resetFileCache();
|
||||||
loadFile(removeWhitespace(string(line, 2)));
|
loadFile(arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (command == ":t") {
|
else if (command == ":t") {
|
||||||
Value v;
|
Value v;
|
||||||
evalString(string(line, 2), v);
|
evalString(arg, v);
|
||||||
std::cout << showType(v) << std::endl;
|
std::cout << showType(v) << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (command == ":b" || command == ":s") {
|
else if (command == ":b" || command == ":s") {
|
||||||
Value v;
|
Value v;
|
||||||
evalString(string(line, 2), v);
|
evalString(arg, v);
|
||||||
DrvInfo drvInfo;
|
DrvInfo drvInfo;
|
||||||
if (!getDerivation(state, v, drvInfo, false))
|
if (!getDerivation(state, v, drvInfo, false))
|
||||||
throw Error("expression does not evaluation to a derivation, so I can't build it");
|
throw Error("expression does not evaluation to a derivation, so I can't build it");
|
||||||
|
@ -305,23 +316,27 @@ void NixRepl::processLine(string line)
|
||||||
/* We could do the build in this process using buildPaths(),
|
/* We could do the build in this process using buildPaths(),
|
||||||
but doing it in a child makes it easier to recover from
|
but doing it in a child makes it easier to recover from
|
||||||
problems / SIGINT. */
|
problems / SIGINT. */
|
||||||
if (runProgram("nix-store", Strings{"-r", drvPath}) != 0) return;
|
if (runProgram("nix-store", Strings{"-r", drvPath}) == 0) {
|
||||||
Derivation drv = parseDerivation(readFile(drvPath));
|
Derivation drv = parseDerivation(readFile(drvPath));
|
||||||
std::cout << std::endl << "this derivation produced the following outputs:" << std::endl;
|
std::cout << std::endl << "this derivation produced the following outputs:" << std::endl;
|
||||||
foreach (DerivationOutputs::iterator, i, drv.outputs)
|
foreach (DerivationOutputs::iterator, i, drv.outputs)
|
||||||
std::cout << format(" %1% -> %2%") % i->first % i->second.path << std::endl;
|
std::cout << format(" %1% -> %2%") % i->first % i->second.path << std::endl;
|
||||||
|
}
|
||||||
} else
|
} else
|
||||||
runProgram("nix-shell", Strings{drvPath});
|
runProgram("nix-shell", Strings{drvPath});
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (command == ":p") {
|
else if (command == ":p") {
|
||||||
Value v;
|
Value v;
|
||||||
evalString(string(line, 2), v);
|
evalString(arg, v);
|
||||||
printValue(std::cout, v, 1000000000) << std::endl;
|
printValue(std::cout, v, 1000000000) << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (string(line, 0, 1) == ":")
|
else if (command == ":q" || command == ":quit")
|
||||||
throw Error(format("unknown command ‘%1%’") % string(line, 0, 2));
|
return false;
|
||||||
|
|
||||||
|
else if (command != "")
|
||||||
|
throw Error(format("unknown command ‘%1%’") % command);
|
||||||
|
|
||||||
else {
|
else {
|
||||||
size_t p = line.find('=');
|
size_t p = line.find('=');
|
||||||
|
@ -341,6 +356,8 @@ void NixRepl::processLine(string line)
|
||||||
printValue(std::cout, v, 1) << std::endl;
|
printValue(std::cout, v, 1) << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue