@@ -468,6 +468,20 @@ func visualLength(runes []rune) int {
468
468
return length
469
469
}
470
470
471
+ // histroryAt unlocks the terminal and relocks it while calling History.At.
472
+ func (t * Terminal ) historyAt (idx int ) (string , bool ) {
473
+ t .lock .Unlock ()
474
+ defer t .lock .Lock ()
475
+ return t .History .At (idx )
476
+ }
477
+
478
+ // historyAdd unlocks the terminal and relocks it while calling History.Add.
479
+ func (t * Terminal ) historyAdd (entry string ) {
480
+ t .lock .Unlock () // Unlock to avoid deadlock if History methods use the output writer.
481
+ defer t .lock .Lock () // panic in Add protection.
482
+ t .History .Add (entry )
483
+ }
484
+
471
485
// handleKey processes the given key and, optionally, returns a line of text
472
486
// that the user has entered.
473
487
func (t * Terminal ) handleKey (key rune ) (line string , ok bool ) {
@@ -515,7 +529,7 @@ func (t *Terminal) handleKey(key rune) (line string, ok bool) {
515
529
t .pos = len (t .line )
516
530
t .moveCursorToPos (t .pos )
517
531
case keyUp :
518
- entry , ok := t .History . At (t .historyIndex + 1 )
532
+ entry , ok := t .historyAt (t .historyIndex + 1 )
519
533
if ! ok {
520
534
return "" , false
521
535
}
@@ -534,7 +548,7 @@ func (t *Terminal) handleKey(key rune) (line string, ok bool) {
534
548
t .setLine (runes , len (runes ))
535
549
t .historyIndex --
536
550
default :
537
- entry , ok := t .History . At (t .historyIndex - 1 )
551
+ entry , ok := t .historyAt (t .historyIndex - 1 )
538
552
if ok {
539
553
t .historyIndex --
540
554
runes := []rune (entry )
@@ -799,9 +813,7 @@ func (t *Terminal) readLine() (line string, err error) {
799
813
if lineOk {
800
814
if t .echo {
801
815
t .historyIndex = - 1
802
- t .lock .Unlock ()
803
- t .History .Add (line ) // so this can output without deadlock.
804
- t .lock .Lock ()
816
+ t .historyAdd (line ) // so this can output without deadlock.
805
817
}
806
818
if lineIsPasted {
807
819
err = ErrPasteIndicator
0 commit comments