Up/down in command panel navigates command history.

This commit is contained in:
Nathan Sobo 2012-03-28 18:08:00 -07:00
parent a4c25dc678
commit a27b54b21b
2 changed files with 40 additions and 3 deletions

View File

@ -68,6 +68,24 @@ describe "CommandPanel", ->
expect(commandPanel).not.toHaveClass 'error'
describe "when move-up and move-down are triggerred on the editor", ->
it "navigates forward and backward through the command history", ->
commandPanel.execute 's/war/peace/g'
commandPanel.execute 's/twinkies/wheatgrass/g'
rootView.trigger 'command-panel:toggle'
commandPanel.editor.trigger 'move-up'
expect(commandPanel.editor.getText()).toBe 's/twinkies/wheatgrass/g'
commandPanel.editor.trigger 'move-up'
expect(commandPanel.editor.getText()).toBe 's/war/peace/g'
commandPanel.editor.trigger 'move-up'
expect(commandPanel.editor.getText()).toBe 's/war/peace/g'
commandPanel.editor.trigger 'move-down'
expect(commandPanel.editor.getText()).toBe 's/twinkies/wheatgrass/g'
commandPanel.editor.trigger 'move-down'
expect(commandPanel.editor.getText()).toBe ''
describe ".execute()", ->
it "executes the command and closes the command panel", ->
rootView.activeEditor().setText("i hate love")

View File

@ -11,6 +11,8 @@ class CommandPanel extends View
@subview 'editor', new Editor
commandInterpreter: null
history: null
historyIndex: 0
initialize: ({@rootView})->
requireStylesheet 'command-panel.css'
@ -21,12 +23,17 @@ class CommandPanel extends View
window.keymap.bindKeys '.editor',
'meta-g': 'command-panel:repeat-relative-address'
@commandInterpreter = new CommandInterpreter()
@history = []
@rootView.on 'command-panel:toggle', => @toggle()
@rootView.on 'command-panel:execute', => @execute()
@rootView.on 'command-panel:repeat-relative-address', => @repeatRelativeAddress()
@editor.addClass 'single-line'
@commandInterpreter = new CommandInterpreter()
@editor.off 'move-up move-down'
@editor.on 'move-up', => @navigateBackwardInHistory()
@editor.on 'move-down', => @navigateForwardInHistory()
toggle: ->
if @parent().length then @hide() else @show()
@ -41,9 +48,9 @@ class CommandPanel extends View
@detach()
@rootView.activeEditor().focus()
execute: ->
execute: (command = @editor.getText()) ->
try
@commandInterpreter.eval(@rootView.activeEditor(), @editor.getText())
@commandInterpreter.eval(@rootView.activeEditor(), command)
catch error
if error instanceof SyntaxError
@addClass 'error'
@ -53,7 +60,19 @@ class CommandPanel extends View
else
throw error
@history.push(command)
@historyIndex = @history.length
@hide()
navigateBackwardInHistory: ->
return if @historyIndex == 0
@historyIndex--
@editor.setText(@history[@historyIndex])
navigateForwardInHistory: ->
return if @historyIndex == @history.length
@historyIndex++
@editor.setText(@history[@historyIndex] or '')
repeatRelativeAddress: ->
@commandInterpreter.repeatRelativeAddress(@rootView.activeEditor())