gomuks/ui/fuzzy-search-modal.go

135 lines
3.8 KiB
Go
Raw Normal View History

2018-05-22 16:23:54 +02:00
// gomuks - A terminal Matrix client written in Go.
2019-01-17 13:13:25 +01:00
// Copyright (C) 2019 Tulir Asokan
2018-05-22 16:23:54 +02:00
//
// This program is free software: you can redistribute it and/or modify
2019-01-17 13:13:25 +01:00
// it under the terms of the GNU Affero General Public License as published by
2018-05-22 16:23:54 +02:00
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2019-01-17 13:13:25 +01:00
// GNU Affero General Public License for more details.
2018-05-22 16:23:54 +02:00
//
2019-01-17 13:13:25 +01:00
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-05-22 16:23:54 +02:00
package ui
import (
"fmt"
"sort"
"strconv"
"github.com/lithammer/fuzzysearch/fuzzy"
2019-01-17 13:13:25 +01:00
2019-03-25 23:37:35 +01:00
"maunium.net/go/mauview"
2019-01-17 13:13:25 +01:00
"maunium.net/go/tcell"
2018-05-22 16:24:47 +02:00
"maunium.net/go/gomuks/debug"
2018-05-22 16:23:54 +02:00
"maunium.net/go/gomuks/matrix/rooms"
)
type FuzzySearchModal struct {
2019-03-25 23:37:35 +01:00
mauview.Component
2018-05-22 16:23:54 +02:00
2019-03-25 23:37:35 +01:00
search *mauview.InputField
results *mauview.TextView
2018-05-22 16:23:54 +02:00
matches fuzzy.Ranks
selected int
roomList []*rooms.Room
roomTitles []string
2019-03-25 23:37:35 +01:00
parent *MainView
2018-05-22 16:23:54 +02:00
}
func NewFuzzySearchModal(mainView *MainView, width int, height int) *FuzzySearchModal {
fs := &FuzzySearchModal{
2019-03-25 23:37:35 +01:00
parent: mainView,
2018-05-22 16:23:54 +02:00
}
fs.InitList(mainView.rooms)
2019-03-25 23:37:35 +01:00
fs.search = mauview.NewInputField().SetChangedFunc(fs.changeHandler)
wrappedSearch := mauview.NewBox(fs.search).SetKeyCaptureFunc(fs.keyHandler)
searchLabel := mauview.NewTextField().SetText("Room")
combinedSearch := mauview.NewFlex().
SetDirection(mauview.FlexColumn).
AddFixedComponent(searchLabel, 5).
AddProportionalComponent(wrappedSearch, 1)
2018-05-22 16:23:54 +02:00
2019-03-25 23:37:35 +01:00
fs.results = mauview.NewTextView().SetRegions(true)
2018-05-22 16:23:54 +02:00
// Flex widget containing input box and results
2019-03-25 23:37:35 +01:00
container := mauview.NewBox(mauview.NewFlex().
SetDirection(mauview.FlexRow).
AddFixedComponent(combinedSearch, 1).
AddProportionalComponent(fs.results, 1)).
2018-05-22 16:23:54 +02:00
SetBorder(true).
2019-03-25 23:37:35 +01:00
SetTitle("Quick Room Switcher").
SetBlurCaptureFunc(func() bool {
fs.parent.HideModal()
return true
})
2018-05-22 16:23:54 +02:00
2019-03-25 23:37:35 +01:00
fs.Component = mauview.Center(container, width, height)
2018-05-22 16:23:54 +02:00
return fs
}
func (fs *FuzzySearchModal) InitList(rooms map[string]*RoomView) {
for _, room := range rooms {
fs.roomList = append(fs.roomList, room.Room)
fs.roomTitles = append(fs.roomTitles, room.Room.GetTitle())
}
}
func (fs *FuzzySearchModal) changeHandler(str string) {
// Get matches and display in result box
fs.matches = fuzzy.RankFindFold(str, fs.roomTitles)
if len(str) > 0 && len(fs.matches) > 0 {
sort.Sort(fs.matches)
fs.results.Clear()
for _, match := range fs.matches {
2018-05-23 17:20:53 +02:00
fmt.Fprintf(fs.results, `["%d"]%s[""]%s`, match.OriginalIndex, match.Target, "\n")
2018-05-22 16:23:54 +02:00
}
2019-03-25 23:37:35 +01:00
fs.parent.parent.Render()
2018-05-23 17:20:53 +02:00
fs.results.Highlight(strconv.Itoa(fs.matches[0].OriginalIndex))
2018-05-22 16:23:54 +02:00
fs.results.ScrollToBeginning()
} else {
fs.results.Clear()
fs.results.Highlight()
}
}
2019-03-25 23:37:35 +01:00
func (fs *FuzzySearchModal) keyHandler(event mauview.KeyEvent) mauview.KeyEvent {
2018-05-22 16:23:54 +02:00
highlights := fs.results.GetHighlights()
switch event.Key() {
case tcell.KeyEsc:
// Close room finder
2019-03-25 23:37:35 +01:00
fs.parent.HideModal()
2018-05-22 16:23:54 +02:00
return nil
case tcell.KeyTab:
// Cycle highlighted area to next match
if len(highlights) > 0 {
fs.selected = (fs.selected + 1) % len(fs.matches)
2018-05-23 17:20:53 +02:00
fs.results.Highlight(strconv.Itoa(fs.matches[fs.selected].OriginalIndex))
2018-05-22 16:23:54 +02:00
fs.results.ScrollToHighlight()
}
return nil
case tcell.KeyEnter:
// Switch room to currently selected room
if len(highlights) > 0 {
2018-05-23 17:20:53 +02:00
debug.Print("Fuzzy Selected Room:", fs.roomList[fs.matches[fs.selected].OriginalIndex].GetTitle())
2019-03-25 23:37:35 +01:00
fs.parent.SwitchRoom(fs.roomList[fs.matches[fs.selected].OriginalIndex].Tags()[0].Tag, fs.roomList[fs.matches[fs.selected].OriginalIndex])
2018-05-22 16:23:54 +02:00
}
2019-03-25 23:37:35 +01:00
fs.parent.HideModal()
2018-05-22 16:23:54 +02:00
fs.results.Clear()
fs.search.SetText("")
return nil
}
return event
}