1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-15 00:15:32 +02:00

Add search history

Add search history for filterable and searchable views.
This commit is contained in:
Karim Khaleel
2023-08-04 14:01:30 +03:00
parent ab5875c78f
commit edec116ceb
12 changed files with 270 additions and 8 deletions

View File

@ -0,0 +1,36 @@
package utils
import "fmt"
type HistoryBuffer[T any] struct {
maxSize int
items []T
}
func NewHistoryBuffer[T any](maxSize int) *HistoryBuffer[T] {
return &HistoryBuffer[T]{
maxSize: maxSize,
items: make([]T, 0, maxSize),
}
}
func (self *HistoryBuffer[T]) Push(item T) {
if len(self.items) == self.maxSize {
self.items = self.items[:len(self.items)-1]
}
self.items = append([]T{item}, self.items...)
}
func (self *HistoryBuffer[T]) PeekAt(index int) (T, error) {
var item T
if len(self.items) == 0 {
return item, fmt.Errorf("Buffer is empty")
}
if len(self.items) <= index || index < -1 {
return item, fmt.Errorf("Index out of range")
}
if index == -1 {
return item, nil
}
return self.items[index], nil
}