embark-area-51/cmd/dashboard/command_history.js

45 lines
950 B
JavaScript
Raw Permalink Normal View History

let fs = require('../../lib/core/fs');
2017-03-30 11:12:39 +00:00
class CommandHistory {
constructor(options = {}) {
this.cmdHistoryFile = options.cmdHistoryFile
|| process.env.DEFAULT_CMD_HISTORY_PATH;
2017-03-30 11:12:39 +00:00
this.history = [];
this.pointer = -1;
this.loadHistory();
2017-03-30 11:12:39 +00:00
}
2017-02-19 17:59:02 +00:00
2017-03-30 11:12:39 +00:00
addCommand(cmd) {
this.history.push(cmd);
this.pointer = this.history.length;
}
2017-02-19 17:59:02 +00:00
2017-12-05 23:14:46 +00:00
getPreviousCommand() {
2017-03-30 11:12:39 +00:00
if (this.pointer >= 0) {
this.pointer--;
}
return this.history[this.pointer];
2017-02-19 17:59:02 +00:00
}
2017-12-05 23:14:46 +00:00
getNextCommand() {
2017-03-30 11:12:39 +00:00
if (this.pointer >= this.history.length) {
this.pointer = this.history.length - 1;
return '';
}
this.pointer++;
return this.history[this.pointer];
2017-02-19 17:59:02 +00:00
}
loadHistory() {
if (fs.existsSync(this.cmdHistoryFile)) {
fs.readFileSync(this.cmdHistoryFile)
.toString()
.split('\n')
.reverse()
.forEach((cmd) => { this.addCommand(cmd); })
}
}
}
2017-02-19 17:59:02 +00:00
module.exports = CommandHistory;