picker.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package picker
  2. import (
  3. "github.com/ayn2op/tview"
  4. "github.com/ayn2op/tview/keybind"
  5. "github.com/gdamore/tcell/v3"
  6. "github.com/sahilm/fuzzy"
  7. )
  8. type (
  9. SelectedFunc func(item Item)
  10. CancelFunc func()
  11. )
  12. type Picker struct {
  13. *tview.Flex
  14. input *tview.InputField
  15. list *tview.List
  16. onSelected SelectedFunc
  17. onCancel CancelFunc
  18. keyMap *KeyMap
  19. items Items
  20. filtered Items
  21. }
  22. func New() *Picker {
  23. p := &Picker{
  24. Flex: tview.NewFlex(),
  25. input: tview.NewInputField(),
  26. list: tview.NewList(),
  27. }
  28. // Show a horizontal bottom border to visually separate input from list.
  29. var borderSet tview.BorderSet
  30. borderSet.Bottom = tview.BoxDrawingsLightHorizontal
  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. p.
  40. SetDirection(tview.FlexRow).
  41. // bottom border + value
  42. AddItem(p.input, 2, 0, true).
  43. AddItem(p.list, 0, 1, false)
  44. p.Update()
  45. return p
  46. }
  47. func (p *Picker) setFilteredItems(filtered Items) {
  48. p.filtered = filtered
  49. p.list.SetBuilder(func(index int, cursor int) tview.ListItem {
  50. if index < 0 || index >= len(p.filtered) {
  51. return nil
  52. }
  53. style := tcell.StyleDefault
  54. if index == cursor {
  55. style = style.Reverse(true)
  56. }
  57. return tview.NewTextView().
  58. SetScrollable(false).
  59. SetWrap(false).
  60. SetWordWrap(false).
  61. SetTextStyle(style).
  62. SetLines([]tview.Line{{{Text: p.filtered[index].Text, Style: style}}})
  63. })
  64. if len(filtered) == 0 {
  65. p.list.SetCursor(-1)
  66. } else {
  67. p.list.SetCursor(0)
  68. }
  69. }
  70. func (p *Picker) SetKeyMap(keyMap *KeyMap) {
  71. p.keyMap = keyMap
  72. }
  73. // SetScrollBarVisibility sets when the picker's list scrollBar is rendered.
  74. func (p *Picker) SetScrollBarVisibility(visibility tview.ScrollBarVisibility) {
  75. p.list.SetScrollBarVisibility(visibility)
  76. }
  77. // SetScrollBar sets the scrollBar primitive used by the picker's list.
  78. func (p *Picker) SetScrollBar(scrollBar *tview.ScrollBar) {
  79. p.list.SetScrollBar(scrollBar)
  80. }
  81. func (p *Picker) SetSelectedFunc(onSelected SelectedFunc) {
  82. p.onSelected = onSelected
  83. }
  84. func (p *Picker) SetCancelFunc(onCancel CancelFunc) {
  85. p.onCancel = onCancel
  86. }
  87. func (p *Picker) ClearInput() {
  88. p.input.SetText("")
  89. }
  90. func (p *Picker) ClearList() {
  91. p.filtered = nil
  92. p.list.Clear()
  93. }
  94. func (p *Picker) ClearItems() {
  95. p.items = nil
  96. p.filtered = nil
  97. }
  98. func (p *Picker) AddItem(item Item) {
  99. p.items = append(p.items, item)
  100. }
  101. func (p *Picker) Update() {
  102. p.ClearInput()
  103. p.onInputChanged("")
  104. }
  105. func (p *Picker) onListSelected(index int) {
  106. if p.onSelected != nil {
  107. if index >= 0 && index < len(p.filtered) {
  108. item := p.filtered[index]
  109. p.onSelected(item)
  110. }
  111. }
  112. }
  113. func (p *Picker) onInputChanged(text string) {
  114. var fuzzied Items
  115. if text == "" {
  116. fuzzied = append(fuzzied, p.items...)
  117. } else {
  118. matches := fuzzy.FindFrom(text, p.items)
  119. for _, match := range matches {
  120. fuzzied = append(fuzzied, p.items[match.Index])
  121. }
  122. }
  123. p.setFilteredItems(fuzzied)
  124. }
  125. func (p *Picker) HandleEvent(event tcell.Event) tview.Command {
  126. switch event := event.(type) {
  127. case *tview.KeyEvent:
  128. consume := tview.BatchCommand{tview.RedrawCommand{}, tview.ConsumeEventCommand{}}
  129. if p.keyMap != nil {
  130. switch {
  131. case keybind.Matches(event, p.keyMap.Up):
  132. p.list.HandleEvent(tcell.NewEventKey(tcell.KeyUp, "", tcell.ModNone))
  133. return consume
  134. case keybind.Matches(event, p.keyMap.Down):
  135. p.list.HandleEvent(tcell.NewEventKey(tcell.KeyDown, "", tcell.ModNone))
  136. return consume
  137. case keybind.Matches(event, p.keyMap.Top):
  138. p.list.HandleEvent(tcell.NewEventKey(tcell.KeyHome, "", tcell.ModNone))
  139. return consume
  140. case keybind.Matches(event, p.keyMap.Bottom):
  141. p.list.HandleEvent(tcell.NewEventKey(tcell.KeyEnd, "", tcell.ModNone))
  142. return consume
  143. case keybind.Matches(event, p.keyMap.Select):
  144. p.onListSelected(p.list.Cursor())
  145. return consume
  146. case keybind.Matches(event, p.keyMap.Cancel):
  147. if p.onCancel != nil {
  148. p.onCancel()
  149. }
  150. return consume
  151. }
  152. }
  153. return p.Flex.HandleEvent(event)
  154. }
  155. return p.Flex.HandleEvent(event)
  156. }