From cf4c29d90a1b0ec7f8cd7f0c5258be63a2e02058 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Fri, 6 Sep 2013 15:20:06 +0200 Subject: [PATCH] Load files specified on the command line MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For example: $ nix-repl '' '' Welcome to Nix version 1.6pre3215_2c1ecf8. Type :? for help. Loading ‘’... Added 3337 variables. Loading ‘’... Added 7 variables. nix-repl> --- nix-repl.cc | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/nix-repl.cc b/nix-repl.cc index 838928b35..2ef23faa5 100644 --- a/nix-repl.cc +++ b/nix-repl.cc @@ -31,8 +31,9 @@ struct NixRepl int displ; NixRepl(); - void mainLoop(); + void mainLoop(const Strings & args); void processLine(string line); + void loadFile(const Path & path); void addAttrsToScope(Value & attrs); void addVarToScope(const Symbol & name, Value * v); Expr * parseString(string s); @@ -110,9 +111,15 @@ NixRepl::NixRepl() } -void NixRepl::mainLoop() +void NixRepl::mainLoop(const Strings & args) { - std::cerr << "Welcome to Nix version " << NIX_VERSION << ". Type :? for help." << std::endl << std::endl; + std::cout << "Welcome to Nix version " << NIX_VERSION << ". Type :? for help." << std::endl << std::endl; + + foreach (Strings::const_iterator, i, args) { + std::cout << format("Loading ‘%1%’...") % *i << std::endl; + loadFile(*i); + std::cout << std::endl; + } using_history(); read_history(0); @@ -150,12 +157,7 @@ void NixRepl::processLine(string line) else if (command == ":l") { state.resetFileCache(); - Path path = lookupFileArg(state, removeWhitespace(string(line, 2))); - Value v, v2; - state.evalFile(path, v); - Bindings bindings; - state.autoCallFunction(bindings, v, v2); - addAttrsToScope(v2); + loadFile(removeWhitespace(string(line, 2))); } else if (command == ":t") { @@ -202,12 +204,22 @@ void NixRepl::processLine(string line) } +void NixRepl::loadFile(const Path & path) +{ + Value v, v2; + state.evalFile(lookupFileArg(state, path), v); + Bindings bindings; + state.autoCallFunction(bindings, v, v2); + addAttrsToScope(v2); +} + + void NixRepl::addAttrsToScope(Value & attrs) { state.forceAttrs(attrs); foreach (Bindings::iterator, i, *attrs.attrs) addVarToScope(i->name, i->value); - std::cout << format("added %1% variables") % attrs.attrs->size() << std::endl; + std::cout << format("Added %1% variables.") % attrs.attrs->size() << std::endl; } @@ -233,8 +245,8 @@ void NixRepl::evalString(string s, Value & v) } -void run(nix::Strings args) +void run(Strings args) { NixRepl repl; - repl.mainLoop(); + repl.mainLoop(args); }