erm/app/darktile/hinters/registry.go

63 lines
1.2 KiB
Go
Raw Normal View History

2021-07-30 22:29:20 +00:00
package hinters
import (
"sort"
"sync"
2023-01-16 02:11:07 +00:00
"tuxpa.in/t/erm/app/darktile/termutil"
2021-07-30 22:29:20 +00:00
)
type HinterRegistration struct {
Priority Priority
Hinter Hinter
}
type Priority uint8
const (
PriorityNone = 0
PriorityVeryLow = 48
PriorityLow = 96
PriorityMedium = 128
PriorityHigh = 192
PriorityVeryHigh = 224
PriorityCritical = 255
)
type Hinter interface {
// Match should return the index in the text of the matched occurrence
Match(text string, cursorIndex int) (matched bool, offset int, length int)
// Activate fires when mouseover happens afer a match - takes raw coords
Activate(api HintAPI, match string, start termutil.Position, end termutil.Position) error
Deactivate(api HintAPI) error
Click(api HintAPI) error
}
var hintLock sync.RWMutex
var hinters []HinterRegistration
func register(h Hinter, p Priority) {
hintLock.Lock()
defer hintLock.Unlock()
hinters = append(hinters, HinterRegistration{
Priority: p,
Hinter: h,
})
}
func All() []Hinter {
hintLock.RLock()
defer hintLock.RUnlock()
var output []Hinter
sort.Slice(hinters, func(i, j int) bool {
return hinters[i].Priority > hinters[j].Priority
})
for _, hinter := range hinters {
output = append(output, hinter.Hinter)
}
return output
}