gomuks/ui/fuzzy-search-modal.go

159 lines
4.2 KiB
Go
Raw Normal View History

2018-05-22 16:23:54 +02:00
// gomuks - A terminal Matrix client written in Go.
2020-04-19 17:10:14 +02:00
// Copyright (C) 2020 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
"maunium.net/go/mautrix/id"
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-28 22:28:27 +01:00
container *mauview.Box
search *mauview.InputArea
2019-03-25 23:37:35 +01:00
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.results = mauview.NewTextView().SetRegions(true)
2019-03-28 22:28:27 +01:00
fs.search = mauview.NewInputArea().
SetChangedFunc(fs.changeHandler).
SetTextColor(tcell.ColorWhite).
SetBackgroundColor(tcell.ColorDarkCyan)
fs.search.Focus()
2018-05-22 16:23:54 +02:00
2019-03-28 22:28:27 +01:00
flex := mauview.NewFlex().
2019-03-25 23:37:35 +01:00
SetDirection(mauview.FlexRow).
2019-03-28 22:28:27 +01:00
AddFixedComponent(fs.search, 1).
AddProportionalComponent(fs.results, 1)
fs.container = mauview.NewBox(flex).
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-28 22:28:27 +01:00
fs.Component = mauview.Center(fs.container, width, height).SetAlwaysFocusChild(true)
2018-05-22 16:23:54 +02:00
return fs
}
2019-03-28 22:28:27 +01:00
func (fs *FuzzySearchModal) Focus() {
fs.container.Focus()
}
func (fs *FuzzySearchModal) Blur() {
fs.container.Blur()
}
func (fs *FuzzySearchModal) InitList(rooms map[id.RoomID]*RoomView) {
2018-05-22 16:23:54 +02:00
for _, room := range rooms {
2020-02-20 00:10:26 +01:00
if room.Room.IsReplaced() {
//if _, ok := rooms[room.Room.ReplacedBy()]; ok
continue
}
2018-05-22 16:23:54 +02:00
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-28 22:28:27 +01:00
//fs.parent.parent.Render()
2018-05-23 17:20:53 +02:00
fs.results.Highlight(strconv.Itoa(fs.matches[0].OriginalIndex))
2021-02-21 03:25:19 +01:00
fs.selected = 0
2018-05-22 16:23:54 +02:00
fs.results.ScrollToBeginning()
} else {
fs.results.Clear()
fs.results.Highlight()
}
}
2019-03-28 22:28:27 +01:00
func (fs *FuzzySearchModal) OnKeyEvent(event mauview.KeyEvent) bool {
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()
2019-03-28 22:28:27 +01:00
return true
2018-05-22 16:23:54 +02:00
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()
}
2019-03-28 22:28:27 +01:00
return true
case tcell.KeyBacktab:
if len(highlights) > 0 {
fs.selected = (fs.selected - 1) % len(fs.matches)
if fs.selected < 0 {
fs.selected += len(fs.matches)
}
2019-03-28 22:28:27 +01:00
fs.results.Highlight(strconv.Itoa(fs.matches[fs.selected].OriginalIndex))
fs.results.ScrollToHighlight()
}
2020-02-20 00:10:26 +01:00
return true
2018-05-22 16:23:54 +02:00
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("")
2019-03-28 22:28:27 +01:00
return true
2018-05-22 16:23:54 +02:00
}
2019-03-28 22:28:27 +01:00
return fs.search.OnKeyEvent(event)
2018-05-22 16:23:54 +02:00
}