| 1 |
// gomuks - A terminal Matrix client written in Go. |
| 2 |
// Copyright (C) 2020 Tulir Asokan |
| 3 |
// |
| 4 |
// This program is free software: you can redistribute it and/or modify |
| 5 |
// it under the terms of the GNU Affero General Public License as published by |
| 6 |
// the Free Software Foundation, either version 3 of the License, or |
| 7 |
// (at your option) any later version. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU Affero General Public License for more details. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU Affero General Public License |
| 15 |
// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
package ui |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"sort" |
| 22 |
"strconv" |
| 23 |
|
| 24 |
"github.com/lithammer/fuzzysearch/fuzzy" |
| 25 |
|
| 26 |
"go.mau.fi/mauview" |
| 27 |
"go.mau.fi/tcell" |
| 28 |
|
| 29 |
"maunium.net/go/mautrix/id" |
| 30 |
|
| 31 |
"maunium.net/go/gomuks/config" |
| 32 |
"maunium.net/go/gomuks/debug" |
| 33 |
"maunium.net/go/gomuks/matrix/rooms" |
| 34 |
) |
| 35 |
|
| 36 |
type FuzzySearchModal struct { |
| 37 |
mauview.Component |
| 38 |
|
| 39 |
container *mauview.Box |
| 40 |
|
| 41 |
search *mauview.InputArea |
| 42 |
results *mauview.TextView |
| 43 |
|
| 44 |
matches fuzzy.Ranks |
| 45 |
selected int |
| 46 |
|
| 47 |
roomList []*rooms.Room |
| 48 |
roomTitles []string |
| 49 |
|
| 50 |
parent *MainView |
| 51 |
} |
| 52 |
|
| 53 |
func NewFuzzySearchModal(mainView *MainView, width int, height int) *FuzzySearchModal { |
| 54 |
fs := &FuzzySearchModal{ |
| 55 |
parent: mainView, |
| 56 |
} |
| 57 |
|
| 58 |
fs.InitList(mainView.rooms) |
| 59 |
|
| 60 |
fs.results = mauview.NewTextView().SetRegions(true) |
| 61 |
fs.search = mauview.NewInputArea(). |
| 62 |
SetChangedFunc(fs.changeHandler). |
| 63 |
SetTextColor(tcell.ColorWhite). |
| 64 |
SetBackgroundColor(tcell.ColorDarkCyan) |
| 65 |
fs.search.Focus() |
| 66 |
|
| 67 |
flex := mauview.NewFlex(). |
| 68 |
SetDirection(mauview.FlexRow). |
| 69 |
AddFixedComponent(fs.search, 1). |
| 70 |
AddProportionalComponent(fs.results, 1) |
| 71 |
|
| 72 |
fs.container = mauview.NewBox(flex). |
| 73 |
SetBorder(true). |
| 74 |
SetTitle("Quick Room Switcher"). |
| 75 |
SetBlurCaptureFunc(func() bool { |
| 76 |
fs.parent.HideModal() |
| 77 |
return true |
| 78 |
}) |
| 79 |
|
| 80 |
fs.Component = mauview.Center(fs.container, width, height).SetAlwaysFocusChild(true) |
| 81 |
|
| 82 |
return fs |
| 83 |
} |
| 84 |
|
| 85 |
func (fs *FuzzySearchModal) Focus() { |
| 86 |
fs.container.Focus() |
| 87 |
} |
| 88 |
|
| 89 |
func (fs *FuzzySearchModal) Blur() { |
| 90 |
fs.container.Blur() |
| 91 |
} |
| 92 |
|
| 93 |
func (fs *FuzzySearchModal) InitList(rooms map[id.RoomID]*RoomView) { |
| 94 |
for _, room := range rooms { |
| 95 |
if room.Room.IsReplaced() { |
| 96 |
//if _, ok := rooms[room.Room.ReplacedBy()]; ok |
| 97 |
continue |
| 98 |
} |
| 99 |
fs.roomList = append(fs.roomList, room.Room) |
| 100 |
fs.roomTitles = append(fs.roomTitles, room.Room.GetTitle()) |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
func (fs *FuzzySearchModal) changeHandler(str string) { |
| 105 |
// Get matches and display in result box |
| 106 |
fs.matches = fuzzy.RankFindFold(str, fs.roomTitles) |
| 107 |
if len(str) > 0 && len(fs.matches) > 0 { |
| 108 |
sort.Sort(fs.matches) |
| 109 |
fs.results.Clear() |
| 110 |
for _, match := range fs.matches { |
| 111 |
fmt.Fprintf(fs.results, `["%d"]%s[""]%s`, match.OriginalIndex, match.Target, "\n") |
| 112 |
} |
| 113 |
//fs.parent.parent.Render() |
| 114 |
fs.results.Highlight(strconv.Itoa(fs.matches[0].OriginalIndex)) |
| 115 |
fs.selected = 0 |
| 116 |
fs.results.ScrollToBeginning() |
| 117 |
} else { |
| 118 |
fs.results.Clear() |
| 119 |
fs.results.Highlight() |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
func (fs *FuzzySearchModal) OnKeyEvent(event mauview.KeyEvent) bool { |
| 124 |
highlights := fs.results.GetHighlights() |
| 125 |
kb := config.Keybind{ |
| 126 |
Key: event.Key(), |
| 127 |
Ch: event.Rune(), |
| 128 |
Mod: event.Modifiers(), |
| 129 |
} |
| 130 |
switch fs.parent.config.Keybindings.Modal[kb] { |
| 131 |
case "cancel": |
| 132 |
// Close room finder |
| 133 |
fs.parent.HideModal() |
| 134 |
return true |
| 135 |
case "select_next": |
| 136 |
// Cycle highlighted area to next match |
| 137 |
if len(highlights) > 0 { |
| 138 |
fs.selected = (fs.selected + 1) % len(fs.matches) |
| 139 |
fs.results.Highlight(strconv.Itoa(fs.matches[fs.selected].OriginalIndex)) |
| 140 |
fs.results.ScrollToHighlight() |
| 141 |
} |
| 142 |
return true |
| 143 |
case "select_prev": |
| 144 |
if len(highlights) > 0 { |
| 145 |
fs.selected = (fs.selected - 1) % len(fs.matches) |
| 146 |
if fs.selected < 0 { |
| 147 |
fs.selected += len(fs.matches) |
| 148 |
} |
| 149 |
fs.results.Highlight(strconv.Itoa(fs.matches[fs.selected].OriginalIndex)) |
| 150 |
fs.results.ScrollToHighlight() |
| 151 |
} |
| 152 |
return true |
| 153 |
case "confirm": |
| 154 |
// Switch room to currently selected room |
| 155 |
if len(highlights) > 0 { |
| 156 |
debug.Print("Fuzzy Selected Room:", fs.roomList[fs.matches[fs.selected].OriginalIndex].GetTitle()) |
| 157 |
fs.parent.SwitchRoom(fs.roomList[fs.matches[fs.selected].OriginalIndex].Tags()[0].Tag, fs.roomList[fs.matches[fs.selected].OriginalIndex]) |
| 158 |
} |
| 159 |
fs.parent.HideModal() |
| 160 |
fs.results.Clear() |
| 161 |
fs.search.SetText("") |
| 162 |
return true |
| 163 |
} |
| 164 |
return fs.search.OnKeyEvent(event) |
| 165 |
} |