Implementation of bash command line history stack in 20 lines of coffeescript. Some tricky corner cases are covered.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CommandHistory | |
constructor: -> | |
@stack = [] | |
@curPos = -1 | |
@pushShouldReplaceLast = false | |
put = (index, cmd) => | |
if @pushShouldReplaceLast then @stack[index..index] = cmd | |
else @stack.push cmd | |
@curPos = index | |
@pushShouldReplaceLast = false | |
move = (detectEnd, changePos, cur) => | |
put @curPos, cur | |
if not detectEnd @curPos then changePos() | |
@pushShouldReplaceLast = @stack.length > 1 or @curPos < -1 | |
@stack[@curPos..@curPos] | |
@back = _.partial move, ((pos) => pos is -@stack.length), => @curPos-- | |
@forward = _.partial move, ((pos) => pos is -1), => @curPos++ | |
@push = _.partial put, -1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment