diff --git a/src/js/__tests__/CommandLineStore.spec.js b/src/js/__tests__/CommandLineStore.spec.js index 673aeeb4..365c37d4 100644 --- a/src/js/__tests__/CommandLineStore.spec.js +++ b/src/js/__tests__/CommandLineStore.spec.js @@ -17,6 +17,36 @@ describe('this store', function() { expect(CommandLineStore.getCommandHistory()[0]) .toEqual(command); + var newCommand = 'echo "yo dude";'; + CommandLineActions.submitCommand(newCommand); + + expect(CommandLineStore.getCommandHistoryLength()) + .toEqual(2); + + expect(CommandLineStore.getCommandHistory()[0]) + .toEqual(newCommand); + expect(CommandLineStore.getCommandHistory()[1]) + .toEqual(command); + }); + + it('slices after max length', function() { + var maxLength = CommandLineStore.getMaxHistoryLength(); + var numOver = 10; + for (var i = 0; i < maxLength + numOver; i++) { + CommandLineActions.submitCommand('commandNum' + i); + } + var numNow = 11 + numOver; + expect( + CommandLineStore.getCommandHistoryLength() + ).toEqual(numNow); + + expect( + CommandLineStore.getCommandHistory()[0] + ).toEqual('commandNum109'); + + expect( + CommandLineStore.getCommandHistory()[numNow - 1] + ).toEqual('commandNum89'); }); }); diff --git a/src/js/stores/CommandLineStore.js b/src/js/stores/CommandLineStore.js index 477a5be1..c97e80d6 100644 --- a/src/js/stores/CommandLineStore.js +++ b/src/js/stores/CommandLineStore.js @@ -8,6 +8,8 @@ var assign = require('object-assign'); var ActionTypes = AppConstants.ActionTypes; var COMMAND_HISTORY_KEY = 'lgb_CommandHistory'; +var COMMAND_HISTORY_MAX_LENGTH = 100; +var COMMAND_HISTORY_TO_KEEP = 10; var _commandHistory = []; try { @@ -17,7 +19,16 @@ try { } catch (e) { } -var _saveToLocalStorage = function() { +function _checkForSize() { + // if our command line history is too big... + if (_commandHistory.length > COMMAND_HISTORY_MAX_LENGTH) { + // grab the last 10 + _commandHistory = + _commandHistory.slice(0, COMMAND_HISTORY_TO_KEEP); + } +} + +function _saveToLocalStorage() { try { localStorage.setItem( COMMAND_HISTORY_KEY, @@ -25,7 +36,7 @@ var _saveToLocalStorage = function() { ); } catch (e) { } -}; +} var CommandLineStore = assign( {}, @@ -33,6 +44,10 @@ EventEmitter.prototype, AppConstants.StoreSubscribePrototype, { + getMaxHistoryLength: function() { + return COMMAND_HISTORY_MAX_LENGTH; + }, + getCommandHistoryLength: function() { return _commandHistory.length; }, @@ -47,7 +62,8 @@ AppConstants.StoreSubscribePrototype, switch (action.type) { case ActionTypes.SUBMIT_COMMAND: - _commandHistory.push(action.text); + _commandHistory.unshift(String(action.text)); + _checkForSize(); _saveToLocalStorage(); shouldInform = true; break; diff --git a/src/js/views/commandViews.js b/src/js/views/commandViews.js index 826125b2..af33a45a 100644 --- a/src/js/views/commandViews.js +++ b/src/js/views/commandViews.js @@ -2,10 +2,11 @@ var _ = require('underscore'); // horrible hack to get localStorage Backbone plugin var Backbone = (!require('../util').isBrowser()) ? Backbone = require('backbone') : Backbone = window.Backbone; -var CommandEntryCollection = require('../models/collections').CommandEntryCollection; var Main = require('../app'); var Command = require('../models/commandModel').Command; var CommandEntry = require('../models/commandModel').CommandEntry; +var CommandLineStore = require('../stores/CommandLineStore'); +var CommandLineActions = require('../actions/CommandLineActions'); var Errors = require('../util/errors'); var Warning = Errors.Warning; @@ -18,25 +19,6 @@ var CommandPromptView = Backbone.View.extend({ initialize: function(options) { Main.getEvents().on('commandSubmittedPassive', this.addToCommandHistory, this); - // uses local storage - this.commands = new CommandEntryCollection(); - this.commands.fetch({ - success: _.bind(function() { - // reverse the commands. this is ugly but needs to be done... - var commands = []; - this.commands.each(function(c) { - commands.push(c); - }); - - commands.reverse(); - this.commands.reset(); - - _.each(commands, function(c) { - this.commands.add(c); - }, this); - }, this) - }); - this.index = -1; this.commandParagraph = this.$('#prompt p.command')[0]; this.commandCursor = this.$('#prompt span.cursor')[0]; @@ -164,23 +146,17 @@ var CommandPromptView = Backbone.View.extend({ // if we are over / under, display blank line. yes this eliminates your // partially edited command, but i doubt that is much in this demo - if (this.index >= this.commands.length || this.index < 0) { + if (this.index >= CommandLineStore.getCommandHistoryLength() || this.index < 0) { this.clear(); this.index = -1; return; } // yay! we actually can display something - var commandEntry = this.commands.toArray()[this.index].get('text'); + var commandEntry = CommandLineStore.getCommandHistory()[this.index]; this.setTextField(commandEntry); }, - clearLocalStorage: function() { - this.commands.each(function(c) { - Backbone.sync('delete', c, function() { }); - }, this); - }, - setTextField: function(value) { this.$('#commandTextField').val(value); }, @@ -198,17 +174,15 @@ var CommandPromptView = Backbone.View.extend({ }, rollupCommands: function(numBack) { - var which = this.commands.toArray().slice(1, Number(numBack) + 1); + var which = CommandLineStore.getCommandHistory().slice(1, Number(numBack) + 1); which.reverse(); var str = ''; - _.each(which, function(commandEntry) { - str += commandEntry.get('text') + ';'; + _.each(which, function(text) { + str += text + ';'; }, this); - var rolled = new CommandEntry({text: str}); - this.commands.unshift(rolled); - Backbone.sync('create', rolled, function() { }); + CommandLineActions.submitCommand(str); }, addToCommandHistory: function(value) { @@ -217,22 +191,13 @@ var CommandPromptView = Backbone.View.extend({ // or if we edited the command in place in history var shouldAdd = (value.length && this.index === -1) || ((value.length && this.index !== -1 && - this.commands.toArray()[this.index].get('text') !== value)); + CommandLineStore.getCommandHistory()[this.index] !== value)); if (!shouldAdd) { return; } - var commandEntry = new CommandEntry({text: value}); - this.commands.unshift(commandEntry); - - // store to local storage - Backbone.sync('create', commandEntry, function() { }); - - // if our length is too egregious, reset - if (this.commands.length > 100) { - this.clearLocalStorage(); - } + CommandLineActions.submitCommand(value); log.commandEntered(value); },