picker.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package picker
  2. import (
  3. "github.com/ayn2op/tview"
  4. "github.com/gdamore/tcell/v3"
  5. "github.com/sahilm/fuzzy"
  6. )
  7. type (
  8. SelectedFunc func(item Item)
  9. CancelFunc func()
  10. )
  11. type Picker struct {
  12. *tview.Flex
  13. input *tview.InputField
  14. list *tview.List
  15. onSelected SelectedFunc
  16. onCancel CancelFunc
  17. keyMap *KeyMap
  18. items Items
  19. filtered Items
  20. }
  21. func New() *Picker {
  22. p := &Picker{
  23. Flex: tview.NewFlex(),
  24. input: tview.NewInputField(),
  25. list: tview.NewList(),
  26. }
  27. // Show a horizontal bottom border to visually separate input from list.
  28. borderSet := tview.BorderSet{
  29. Bottom: tview.BoxDrawingsLightHorizontal,
  30. }
  31. borderSet.BottomLeft = borderSet.Bottom
  32. borderSet.BottomRight = borderSet.Bottom
  33. p.input.
  34. SetChangedFunc(p.onInputChanged).
  35. SetLabel("> ").
  36. SetBorders(tview.BordersBottom).
  37. SetBorderSet(borderSet).
  38. SetBorderStyle(tcell.StyleDefault.Dim(true)).
  39. SetInputCapture(p.onInputCapture)
  40. p.list.SetSnapToItems(true)
  41. p.
  42. SetDirection(tview.FlexRow).
  43. // bottom border + value
  44. AddItem(p.input, 2, 0, true).
  45. AddItem(p.list, 0, 1, false)
  46. p.Update()
  47. return p
  48. }
  49. func (p *Picker) setFilteredItems(filtered Items) {
  50. p.filtered = filtered
  51. p.list.SetBuilder(func(index int, cursor int) tview.ListItem {
  52. if index < 0 || index >= len(p.filtered) {
  53. return nil
  54. }
  55. style := tcell.StyleDefault
  56. if index == cursor {
  57. style = style.Foreground(tview.Styles.PrimitiveBackgroundColor).Background(tview.Styles.PrimaryTextColor)
  58. }
  59. return tview.NewTextView().
  60. SetScrollable(false).
  61. SetWrap(false).
  62. SetWordWrap(false).
  63. SetTextStyle(style).
  64. SetLines([]tview.Line{{{Text: p.filtered[index].Text, Style: style}}})
  65. })
  66. if len(filtered) == 0 {
  67. p.list.SetCursor(-1)
  68. } else {
  69. p.list.SetCursor(0)
  70. }
  71. }
  72. func (p *Picker) SetKeyMap(keyMap *KeyMap) {
  73. p.keyMap = keyMap
  74. }
  75. func (p *Picker) SetSelectedFunc(onSelected SelectedFunc) {
  76. p.onSelected = onSelected
  77. }
  78. func (p *Picker) SetCancelFunc(onCancel CancelFunc) {
  79. p.onCancel = onCancel
  80. }
  81. func (p *Picker) ClearInput() {
  82. p.input.SetText("")
  83. }
  84. func (p *Picker) ClearList() {
  85. p.filtered = nil
  86. p.list.Clear()
  87. }
  88. func (p *Picker) ClearItems() {
  89. p.items = nil
  90. p.filtered = nil
  91. }
  92. func (p *Picker) AddItem(item Item) {
  93. p.items = append(p.items, item)
  94. }
  95. func (p *Picker) Update() {
  96. p.ClearInput()
  97. p.onInputChanged("")
  98. }
  99. func (p *Picker) onListSelected(index int) {
  100. if p.onSelected != nil {
  101. if index >= 0 && index < len(p.filtered) {
  102. item := p.filtered[index]
  103. p.onSelected(item)
  104. }
  105. }
  106. }
  107. func (p *Picker) onInputChanged(text string) {
  108. var fuzzied Items
  109. if text == "" {
  110. fuzzied = append(fuzzied, p.items...)
  111. } else {
  112. matches := fuzzy.FindFrom(text, p.items)
  113. for _, match := range matches {
  114. fuzzied = append(fuzzied, p.items[match.Index])
  115. }
  116. }
  117. p.setFilteredItems(fuzzied)
  118. }
  119. func (p *Picker) onInputCapture(event *tcell.EventKey) *tcell.EventKey {
  120. if p.keyMap == nil {
  121. return nil
  122. }
  123. handler := p.list.InputHandler()
  124. switch event.Name() {
  125. case p.keyMap.Up:
  126. handler(tcell.NewEventKey(tcell.KeyUp, "", tcell.ModNone), nil)
  127. return nil
  128. case p.keyMap.Down:
  129. handler(tcell.NewEventKey(tcell.KeyDown, "", tcell.ModNone), nil)
  130. return nil
  131. case p.keyMap.Top:
  132. handler(tcell.NewEventKey(tcell.KeyHome, "", tcell.ModNone), nil)
  133. return nil
  134. case p.keyMap.Bottom:
  135. handler(tcell.NewEventKey(tcell.KeyEnd, "", tcell.ModNone), nil)
  136. case p.keyMap.Select:
  137. p.onListSelected(p.list.Cursor())
  138. return nil
  139. case p.keyMap.Cancel:
  140. if p.onCancel != nil {
  141. p.onCancel()
  142. }
  143. return nil
  144. }
  145. return event
  146. }