Skip to content

Commit 9fa240e

Browse files
committed
Fix the arrow key movement when holding down an arrow key
1 parent 0bbb1a2 commit 9fa240e

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

Diff for: v2/keyhist.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,19 @@ func (kh *KeyHistory) OnlyInAndAllDiffer(keyPresses ...string) bool {
152152
func (kh *KeyHistory) AllWithin(dur time.Duration) bool {
153153
khMut.RLock()
154154
defer khMut.RUnlock()
155-
156155
firstTime := kh.t[0]
157156
lastTime := kh.t[2]
158157
return lastTime.Sub(firstTime) < dur
159158
}
160159

160+
// LastChanged checks if a key was added to the key history for longer since than the given duration, or not
161+
func (kh *KeyHistory) LastChanged(dur time.Duration) bool {
162+
khMut.RLock()
163+
defer khMut.RUnlock()
164+
lastTime := kh.t[2]
165+
return time.Since(lastTime) < dur
166+
}
167+
161168
// PrevWithin checks if the previous keypress happened within the given duration (to check for rapid successions)
162169
func (kh *KeyHistory) PrevWithin(dur time.Duration) bool {
163170
khMut.RLock()

Diff for: v2/keyloop.go

+10-11
Original file line numberDiff line numberDiff line change
@@ -678,8 +678,10 @@ func Loop(tty *vt100.TTY, fnord FilenameOrData, lineNumber LineNumber, colNumber
678678

679679
e.CursorBackward(c, status)
680680

681-
// Check if it has been pressed 2 times the last 200ms, if so, give it an extra boost
682-
if kh.TwoLastAre(leftArrow) && kh.AllWithin(200*time.Millisecond) {
681+
// Move extra if the key is held down
682+
if kh.TwoLastAre(leftArrow) && kh.AllWithin(200*time.Millisecond) && kh.LastChanged(200*time.Millisecond) {
683+
e.CursorBackward(c, status)
684+
e.CursorBackward(c, status)
683685
e.CursorBackward(c, status)
684686
}
685687

@@ -704,12 +706,9 @@ func Loop(tty *vt100.TTY, fnord FilenameOrData, lineNumber LineNumber, colNumber
704706

705707
e.CursorForward(c, status)
706708

707-
if kh.TwoLastAre(leftArrow) && kh.AllWithin(200*time.Millisecond) {
709+
// Move extra if the key is held down
710+
if kh.TwoLastAre(rightArrow) && kh.AllWithin(200*time.Millisecond) && kh.LastChanged(200*time.Millisecond) {
708711
e.CursorForward(c, status)
709-
}
710-
711-
// Check if it has been pressed 2 times the last 200 ms, if so, give it an extra boost
712-
if kh.TwoLastAre(rightArrow) && kh.AllWithin(200*time.Millisecond) {
713712
e.CursorForward(c, status)
714713
e.CursorForward(c, status)
715714
}
@@ -731,8 +730,8 @@ func Loop(tty *vt100.TTY, fnord FilenameOrData, lineNumber LineNumber, colNumber
731730

732731
e.CursorUpward(c, status)
733732

734-
// Check if it has been pressed 2 times the last 200 ms, if so, give it an extra boost
735-
if kh.TwoLastAre(upArrow) && kh.AllWithin(200*time.Millisecond) {
733+
// Move extra if the key is held down
734+
if kh.TwoLastAre(upArrow) && kh.AllWithin(200*time.Millisecond) && kh.LastChanged(200*time.Millisecond) {
736735
e.CursorUpward(c, status)
737736
}
738737

@@ -754,8 +753,8 @@ func Loop(tty *vt100.TTY, fnord FilenameOrData, lineNumber LineNumber, colNumber
754753

755754
e.CursorDownward(c, status)
756755

757-
// Check if it has been pressed 2 times the last 200 ms, if so, give it an extra boost
758-
if kh.TwoLastAre(downArrow) && kh.AllWithin(200*time.Millisecond) {
756+
// Move extra if the key is held down
757+
if kh.TwoLastAre(downArrow) && kh.AllWithin(200*time.Millisecond) && kh.LastChanged(200*time.Millisecond) {
759758
e.CursorDownward(c, status)
760759
}
761760

0 commit comments

Comments
 (0)