github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/utils/readline/undo.go (about) 1 package readline 2 3 import ( 4 "strings" 5 ) 6 7 func (rl *Instance) undoAppendHistory() { 8 if rl.viUndoSkipAppend { 9 rl.viUndoSkipAppend = false 10 return 11 } 12 13 rl.viUndoHistory = append(rl.viUndoHistory, rl.line.Duplicate()) 14 } 15 16 func (rl *Instance) undoLastStr() string { 17 var undo *UnicodeT 18 for { 19 if len(rl.viUndoHistory) == 0 { 20 return "" 21 } 22 undo = rl.viUndoHistory[len(rl.viUndoHistory)-1] 23 rl.viUndoHistory = rl.viUndoHistory[:len(rl.viUndoHistory)-1] 24 if undo.String() != rl.line.String() { 25 break 26 } 27 } 28 29 output := rl.clearHelpersStr() 30 31 output += moveCursorBackwardsStr(rl.line.CellPos()) 32 output += strings.Repeat(" ", rl.line.CellLen()) 33 output += moveCursorBackwardsStr(rl.line.CellLen()) 34 output += moveCursorForwardsStr(undo.CellPos()) 35 36 rl.line = undo.Duplicate() 37 38 output += rl.echoStr() 39 40 // TODO: check me 41 if rl.modeViMode != vimInsert && rl.line.RuneLen() > 0 && rl.line.RunePos() == rl.line.RuneLen() { 42 rl.line.SetRunePos(rl.line.RuneLen() - 1) 43 output += moveCursorBackwardsStr(1) 44 } 45 46 return output 47 }