index.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. var readline = require('readline');
  2. var log = require('hlogger').createLogger("shell");
  3. var path = require('path');
  4. var shellActivator = {
  5. start(context) {
  6. this.context = context;
  7. this.manager = context.manager;
  8. this.stdout = process.stdout;
  9. this.registerCommands(context);
  10. this.cli = readline.createInterface(process.stdin,this.stdout,(str) => this.completer(str));
  11. this.cli.setPrompt("HCI> ");
  12. this.cli.on('line',(cmd) => {
  13. if(cmd.length == "") {this.cli.prompt(); return;}
  14. var args = cmd.split(/\s/);
  15. context.emit("cmd:" + args[0],args).done(()=> {
  16. this.cli.prompt();
  17. });
  18. });
  19. this.cli.prompt();
  20. },
  21. stop(context) {
  22. log.info("Closing cli");
  23. this.cli.close();
  24. },
  25. completer(str) {
  26. var hits = this.manager.listeners
  27. .filter((v)=> {
  28. var re = new RegExp("^"+ this.context.name + ":cmd:" +str + ".*");
  29. if(re.test(v.name)) {
  30. return v;
  31. }
  32. }).map((v) => v.name.split(":")[2] + " ");
  33. return [hits,str];
  34. },
  35. output(str) {
  36. this.stdout.write(str + "\n");
  37. },
  38. registerCommands(context) {
  39. }
  40. }
  41. module.exports.bundleActivator = shellActivator;