embark/cmd/dashboard/command_history.js

44 lines
945 B
JavaScript

let fs = require('../../lib/core/fs');
class CommandHistory {
constructor(options = {}) {
this.cmdHistoryFile = options.cmdHistoryFile || fs.dappPath('.embark', 'cmd_history');
this.history = [];
this.pointer = -1;
this.loadHistory();
}
addCommand(cmd) {
this.history.push(cmd);
this.pointer = this.history.length;
}
getPreviousCommand() {
if (this.pointer >= 0) {
this.pointer--;
}
return this.history[this.pointer];
}
getNextCommand() {
if (this.pointer >= this.history.length) {
this.pointer = this.history.length - 1;
return '';
}
this.pointer++;
return this.history[this.pointer];
}
loadHistory() {
if (fs.existsSync(this.cmdHistoryFile)) {
fs.readFileSync(this.cmdHistoryFile)
.toString()
.split('\n')
.reverse()
.forEach((cmd) => { this.addCommand(cmd); })
}
}
}
module.exports = CommandHistory;