From 556c79986923a3a30a6ebac04842a85ab75f5ea8 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Thu, 22 Sep 2016 22:51:48 -0700 Subject: [PATCH] add command line history and navigation to the console --- lib/monitor.js | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/lib/monitor.js b/lib/monitor.js index 67662e5bb..051c26d11 100644 --- a/lib/monitor.js +++ b/lib/monitor.js @@ -3,14 +3,37 @@ var blessed = require("blessed"); -//var formatOutput = require("../utils/format-output.js"); -//var formatModules = require("../utils/format-modules.js"); -//var formatAssets = require("../utils/format-assets.js"); +var CommandHistory = function() { + this.history = []; + this.pointer = -1; +}; + +CommandHistory.prototype.addCommand = function(cmd) { + this.history.push(cmd); + this.pointer = this.history.length; +}; + +CommandHistory.prototype.getPreviousCommand = function(cmd) { + if (this.pointer >= 0) { + this.pointer--; + } + return this.history[this.pointer]; +}; + +CommandHistory.prototype.getNextCommand = function(cmd) { + if (this.pointer >= this.history.length) { + this.pointer = this.history.length - 1; + return ''; + } + this.pointer++; + return this.history[this.pointer]; +}; function Dashboard(options) { var title = options && options.title || "Embark 2.0"; this.env = options.env; this.console = options.console; + this.history = new CommandHistory(); this.color = options && options.color || "green"; this.minimal = options && options.minimal || false; @@ -316,8 +339,21 @@ Dashboard.prototype.layoutCmd = function() { process.exit(0); }); + this.input.key(["up"], function() { + var cmd = self.history.getPreviousCommand(); + self.input.setValue(cmd); + self.input.focus(); + }); + + this.input.key(["down"], function() { + var cmd = self.history.getNextCommand(); + self.input.setValue(cmd); + self.input.focus(); + }); + this.input.on('submit', function(data) { if (data !== '') { + self.history.addCommand(data); self.logText.log('console> ' + data); self.console.executeCmd(data, function(result) { self.logText.log(result);