From 1b1cabb360538e03fd72092eb0e2cb25edb633f8 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Thu, 28 Mar 2019 23:28:27 +0200 Subject: Even more changes --- debug/debug.go | 11 +++++----- go.mod | 4 ++-- go.sum | 2 ++ matrix/matrix.go | 39 +++++----------------------------- ui/fuzzy-search-modal.go | 55 ++++++++++++++++++++++++++++++------------------ ui/message-view.go | 1 - ui/view-main.go | 5 +++++ 7 files changed, 55 insertions(+), 62 deletions(-) diff --git a/debug/debug.go b/debug/debug.go index be3d925..b7d97bb 100644 --- a/debug/debug.go +++ b/debug/debug.go @@ -82,13 +82,14 @@ const Oops = ` __________ \ (XX)\_______ (__)\ )\/\ U ||----W | - || ||` + || || + +A fatal error has occurred. + +` func PrettyPanic(panic interface{}) { - fmt.Println(Oops) - fmt.Println("") - fmt.Println("A fatal error has occurred.") - fmt.Println("") + fmt.Print(Oops) traceFile := fmt.Sprintf("/tmp/gomuks-panic-%s.txt", time.Now().Format("2006-01-02--15-04-05")) var buf bytes.Buffer diff --git a/go.mod b/go.mod index 2179618..e68cc5f 100644 --- a/go.mod +++ b/go.mod @@ -11,9 +11,9 @@ require ( github.com/russross/blackfriday/v2 v2.0.1 github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect golang.org/x/image v0.0.0-20190321063152-3fc05d484e9f - golang.org/x/net v0.0.0-20190326090315-15845e8f865b + golang.org/x/net v0.0.0-20190327214358-63eda1eb0650 gopkg.in/yaml.v2 v2.2.2 - maunium.net/go/mautrix v0.1.0-alpha.3.0.20190326200501-48bbec641543 + maunium.net/go/mautrix v0.1.0-alpha.3.0.20190328212757-5794ed367481 maunium.net/go/mauview v0.0.0-20190325223341-4c387be4b686 maunium.net/go/tcell v0.0.0-20190111223412-5e74142cb009 ) diff --git a/go.sum b/go.sum index f23788a..efad238 100644 --- a/go.sum +++ b/go.sum @@ -27,6 +27,8 @@ golang.org/x/image v0.0.0-20190321063152-3fc05d484e9f/go.mod h1:kZ7UVZpmo3dzQBMx golang.org/x/net v0.0.0-20190110200230-915654e7eabc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190326090315-15845e8f865b h1:LlDMQZ0I/u8J45sbt31TecpsFNErRGwDgS4WvT9hKzE= golang.org/x/net v0.0.0-20190326090315-15845e8f865b/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190327214358-63eda1eb0650 h1:XCbwcsP09zrBt1aYht0fASw+ynbEpYr8NnCkIN9nMM0= +golang.org/x/net v0.0.0-20190327214358-63eda1eb0650/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190322080309-f49334f85ddc/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= diff --git a/matrix/matrix.go b/matrix/matrix.go index 6d94e2e..9527451 100644 --- a/matrix/matrix.go +++ b/matrix/matrix.go @@ -29,11 +29,8 @@ import ( "path" "path/filepath" "regexp" - "strings" "time" - "github.com/russross/blackfriday/v2" - "maunium.net/go/mautrix" "maunium.net/go/mautrix/format" @@ -472,21 +469,6 @@ func (c *Container) SendMessage(roomID string, msgtype mautrix.MessageType, text return resp.EventID, nil } -func (c *Container) renderMarkdown(text string) string { - renderer := blackfriday.NewHTMLRenderer(blackfriday.HTMLRendererParameters{ - Flags: blackfriday.UseXHTML, - }) - - return strings.ReplaceAll(string(blackfriday.Run([]byte(text), - blackfriday.WithExtensions(blackfriday.NoIntraEmphasis| - blackfriday.Tables| - blackfriday.FencedCode| - blackfriday.Strikethrough| - blackfriday.SpaceHeadings| - blackfriday.DefinitionLists), - blackfriday.WithRenderer(renderer))), "\n", "") -} - var mentionRegex = regexp.MustCompile("\\[(.+?)]\\(https://matrix.to/#/@.+?:.+?\\)") var roomRegex = regexp.MustCompile("\\[.+?]\\(https://matrix.to/#/(#.+?:[^/]+?)\\)") @@ -497,26 +479,15 @@ var roomRegex = regexp.MustCompile("\\[.+?]\\(https://matrix.to/#/(#.+?:[^/]+?)\ func (c *Container) SendMarkdownMessage(roomID string, msgtype mautrix.MessageType, text string) (string, error) { defer debug.Recover() - html := c.renderMarkdown(text) - if html == text { - return c.SendMessage(roomID, msgtype, text) - } - // Oh god this is horrible - // but at least /rainbow doesn't put HTML into the plaintext :D - text = format.HTMLToText(html) + content := format.RenderMarkdown(text) + content.MsgType = msgtype // Remove markdown link stuff from plaintext mentions and room links - text = mentionRegex.ReplaceAllString(text, "$1") - text = roomRegex.ReplaceAllString(text, "$1") + content.Body = mentionRegex.ReplaceAllString(content.Body, "$1") + content.Body = roomRegex.ReplaceAllString(content.Body, "$1") c.SendTyping(roomID, false) - resp, err := c.client.SendMessageEvent(roomID, mautrix.EventMessage, - mautrix.Content{ - MsgType: msgtype, - Body: text, - Format: mautrix.FormatHTML, - FormattedBody: html, - }) + resp, err := c.client.SendMessageEvent(roomID, mautrix.EventMessage, content) if err != nil { return "", err } diff --git a/ui/fuzzy-search-modal.go b/ui/fuzzy-search-modal.go index 774a804..97a029a 100644 --- a/ui/fuzzy-search-modal.go +++ b/ui/fuzzy-search-modal.go @@ -33,7 +33,9 @@ import ( type FuzzySearchModal struct { mauview.Component - search *mauview.InputField + container *mauview.Box + + search *mauview.InputArea results *mauview.TextView matches fuzzy.Ranks @@ -52,21 +54,19 @@ func NewFuzzySearchModal(mainView *MainView, width int, height int) *FuzzySearch fs.InitList(mainView.rooms) - 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) - fs.results = mauview.NewTextView().SetRegions(true) + fs.search = mauview.NewInputArea(). + SetChangedFunc(fs.changeHandler). + SetTextColor(tcell.ColorWhite). + SetBackgroundColor(tcell.ColorDarkCyan) + fs.search.Focus() - // Flex widget containing input box and results - container := mauview.NewBox(mauview.NewFlex(). + flex := mauview.NewFlex(). SetDirection(mauview.FlexRow). - AddFixedComponent(combinedSearch, 1). - AddProportionalComponent(fs.results, 1)). + AddFixedComponent(fs.search, 1). + AddProportionalComponent(fs.results, 1) + + fs.container = mauview.NewBox(flex). SetBorder(true). SetTitle("Quick Room Switcher"). SetBlurCaptureFunc(func() bool { @@ -74,11 +74,19 @@ func NewFuzzySearchModal(mainView *MainView, width int, height int) *FuzzySearch return true }) - fs.Component = mauview.Center(container, width, height) + fs.Component = mauview.Center(fs.container, width, height).SetAlwaysFocusChild(true) return fs } +func (fs *FuzzySearchModal) Focus() { + fs.container.Focus() +} + +func (fs *FuzzySearchModal) Blur() { + fs.container.Blur() +} + func (fs *FuzzySearchModal) InitList(rooms map[string]*RoomView) { for _, room := range rooms { fs.roomList = append(fs.roomList, room.Room) @@ -95,7 +103,7 @@ func (fs *FuzzySearchModal) changeHandler(str string) { for _, match := range fs.matches { fmt.Fprintf(fs.results, `["%d"]%s[""]%s`, match.OriginalIndex, match.Target, "\n") } - fs.parent.parent.Render() + //fs.parent.parent.Render() fs.results.Highlight(strconv.Itoa(fs.matches[0].OriginalIndex)) fs.results.ScrollToBeginning() } else { @@ -104,13 +112,14 @@ func (fs *FuzzySearchModal) changeHandler(str string) { } } -func (fs *FuzzySearchModal) keyHandler(event mauview.KeyEvent) mauview.KeyEvent { +func (fs *FuzzySearchModal) OnKeyEvent(event mauview.KeyEvent) bool { highlights := fs.results.GetHighlights() + debug.Print("Search key event:", event) switch event.Key() { case tcell.KeyEsc: // Close room finder fs.parent.HideModal() - return nil + return true case tcell.KeyTab: // Cycle highlighted area to next match if len(highlights) > 0 { @@ -118,7 +127,13 @@ func (fs *FuzzySearchModal) keyHandler(event mauview.KeyEvent) mauview.KeyEvent fs.results.Highlight(strconv.Itoa(fs.matches[fs.selected].OriginalIndex)) fs.results.ScrollToHighlight() } - return nil + return true + case tcell.KeyBacktab: + if len(highlights) > 0 { + fs.selected = (fs.selected - 1) % len(fs.matches) + fs.results.Highlight(strconv.Itoa(fs.matches[fs.selected].OriginalIndex)) + fs.results.ScrollToHighlight() + } case tcell.KeyEnter: // Switch room to currently selected room if len(highlights) > 0 { @@ -128,7 +143,7 @@ func (fs *FuzzySearchModal) keyHandler(event mauview.KeyEvent) mauview.KeyEvent fs.parent.HideModal() fs.results.Clear() fs.search.SetText("") - return nil + return true } - return event + return fs.search.OnKeyEvent(event) } diff --git a/ui/message-view.go b/ui/message-view.go index c8a7bfd..d90a3a7 100644 --- a/ui/message-view.go +++ b/ui/message-view.go @@ -559,5 +559,4 @@ func (view *MessageView) Draw(screen mauview.Screen) { text.Draw(screen, messageX, line) } - debug.Print(screen) } diff --git a/ui/view-main.go b/ui/view-main.go index d081d13..d36a510 100644 --- a/ui/view-main.go +++ b/ui/view-main.go @@ -90,6 +90,8 @@ func (view *MainView) ShowModal(modal mauview.Component) { view.focused, ok = modal.(mauview.Focusable) if !ok { view.focused = nil + } else { + view.focused.Focus() } } @@ -281,6 +283,9 @@ defaultHandler: const WheelScrollOffsetDiff = 3 func (view *MainView) OnMouseEvent(event mauview.MouseEvent) bool { + if view.modal != nil { + return view.modal.OnMouseEvent(event) + } if view.config.Preferences.HideRoomList { return view.roomView.OnMouseEvent(event) } -- cgit v1.2.3