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:
36
pkg/utils/history_buffer.go
Normal file
36
pkg/utils/history_buffer.go
Normal 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
|
||||
}
|
Reference in New Issue
Block a user