Refactor some things and format everything

This commit is contained in:
Tulir Asokan 2018-03-21 00:21:16 +02:00
parent 3ad2c11865
commit 7994c289aa
7 changed files with 178 additions and 142 deletions

View File

@ -33,7 +33,7 @@ type Session struct {
NextBatch string NextBatch string
FilterID string FilterID string
Rooms map[string]*rooms.Room Rooms map[string]*rooms.Room
PushRules *gomatrix.PushRuleset PushRules *gomatrix.PushRuleset
} }
func (config *Config) LoadSession(mxid string) error { func (config *Config) LoadSession(mxid string) error {

View File

@ -27,7 +27,7 @@ type Room struct {
*gomatrix.Room *gomatrix.Room
PrevBatch string PrevBatch string
Owner string Owner string
memberCache map[string]*Member memberCache map[string]*Member
firstMemberCache string firstMemberCache string
nameCache string nameCache string
@ -204,7 +204,7 @@ func (room *Room) GetMember(userID string) *Member {
// NewRoom creates a new Room with the given ID // NewRoom creates a new Room with the given ID
func NewRoom(roomID, owner string) *Room { func NewRoom(roomID, owner string) *Room {
return &Room{ return &Room{
Room: gomatrix.NewRoom(roomID), Room: gomatrix.NewRoom(roomID),
Owner: owner, Owner: owner,
} }
} }

View File

@ -17,8 +17,8 @@
package notification package notification
import ( import (
"os/exec"
"fmt" "fmt"
"os/exec"
"strings" "strings"
) )
@ -37,9 +37,9 @@ func Send(title, text string, critical bool) error {
if critical { if critical {
args = append(args, "-timeout", "30") args = append(args, "-timeout", "30")
} }
// if len(iconPath) > 0 { // if len(iconPath) > 0 {
// args = append(args, "-appIcon", iconPath) // args = append(args, "-appIcon", iconPath)
// } // }
return exec.Command("terminal-notifier", args...).Run() return exec.Command("terminal-notifier", args...).Run()
} }
title = strings.Replace(title, `"`, `\"`, -1) title = strings.Replace(title, `"`, `\"`, -1)
@ -47,4 +47,3 @@ func Send(title, text string, critical bool) error {
notification := fmt.Sprintf("display notification \"%s\" with title \"gomuks\" subtitle \"%s\"", text, title) notification := fmt.Sprintf("display notification \"%s\" with title \"gomuks\" subtitle \"%s\"", text, title)
return exec.Command("osascript", "-e", notification).Run() return exec.Command("osascript", "-e", notification).Run()
} }

View File

@ -23,9 +23,9 @@ func Send(title, text string, critical bool) error {
if critical { if critical {
args = append(args, "-p", "critical") args = append(args, "-p", "critical")
} }
// if iconPath { // if iconPath {
// args = append(args, "-i", iconPath) // args = append(args, "-i", iconPath)
// } // }
args = append(args, title, text) args = append(args, title, text)
return exec.Command("notify-send", args...).Run() return exec.Command("notify-send", args...).Run()
} }

View File

@ -20,12 +20,12 @@ import "gopkg.in/toast.v1"
func Send(title, text string, critical bool) error { func Send(title, text string, critical bool) error {
notification := toast.Notification{ notification := toast.Notification{
AppID: "gomuks", AppID: "gomuks",
Title: title, Title: title,
Message: message, Message: message,
Audio: toast.IM, Audio: toast.IM,
Duration: toast.Short, Duration: toast.Short,
// Icon: ..., // Icon: ...,
} }
if critical { if critical {
notification.Duration = toast.Long notification.Duration = toast.Long

View File

@ -23,9 +23,9 @@ import (
) )
type GomuksUI struct { type GomuksUI struct {
gmx ifc.Gomuks gmx ifc.Gomuks
app *tview.Application app *tview.Application
views *tview.Pages views *tview.Pages
mainView *MainView mainView *MainView
loginView *tview.Form loginView *tview.Form
@ -38,9 +38,9 @@ func init() {
func NewGomuksUI(gmx ifc.Gomuks) (ui *GomuksUI) { func NewGomuksUI(gmx ifc.Gomuks) (ui *GomuksUI) {
ui = &GomuksUI{ ui = &GomuksUI{
gmx: gmx, gmx: gmx,
app: gmx.App(), app: gmx.App(),
views: tview.NewPages(), views: tview.NewPages(),
} }
ui.views.SetChangedFunc(ui.Render) ui.views.SetChangedFunc(ui.Render)
return return

View File

@ -322,10 +322,6 @@ func (field *AdvancedInputField) setCursor(screen tcell.Screen) {
y++ y++
rightLimit -= 2 rightLimit -= 2
} }
fieldWidth := runewidth.StringWidth(field.text)
if field.fieldWidth > 0 && fieldWidth > field.fieldWidth-1 {
fieldWidth = field.fieldWidth - 1
}
x = x + tview.StringWidth(field.label) + field.cursorOffset - field.viewOffset x = x + tview.StringWidth(field.label) + field.cursorOffset - field.viewOffset
if x >= rightLimit { if x >= rightLimit {
x = rightLimit - 1 x = rightLimit - 1
@ -344,122 +340,163 @@ func SubstringBefore(s string, w int) string {
return runewidth.Truncate(s, w, "") return runewidth.Truncate(s, w, "")
} }
func (field *AdvancedInputField) TypeRune(ch rune) {
leftPart := SubstringBefore(field.text, field.cursorOffset)
newText := leftPart + string(ch) + field.text[len(leftPart):]
if field.accept != nil {
if !field.accept(newText, ch) {
return
}
}
field.text = newText
field.cursorOffset += runewidth.RuneWidth(ch)
}
func (field *AdvancedInputField) PasteClipboard() {
clip, _ := clipboard.ReadAll("clipboard")
leftPart := SubstringBefore(field.text, field.cursorOffset)
field.text = leftPart + clip + field.text[len(leftPart):]
field.cursorOffset += runewidth.StringWidth(clip)
}
func (field *AdvancedInputField) MoveCursorLeft(moveWord bool) {
before := SubstringBefore(field.text, field.cursorOffset)
if moveWord {
found := lastWord.FindString(before)
field.cursorOffset -= runewidth.StringWidth(found)
} else if len(before) > 0 {
beforeRunes := []rune(before)
char := beforeRunes[len(beforeRunes)-1]
field.cursorOffset -= runewidth.RuneWidth(char)
}
}
func (field *AdvancedInputField) MoveCursorRight(moveWord bool) {
before := SubstringBefore(field.text, field.cursorOffset)
after := field.text[len(before):]
if moveWord {
found := firstWord.FindString(after)
field.cursorOffset += runewidth.StringWidth(found)
} else if len(after) > 0 {
char := []rune(after)[0]
field.cursorOffset += runewidth.RuneWidth(char)
}
}
func (field *AdvancedInputField) RemoveNextCharacter() {
if field.cursorOffset >= runewidth.StringWidth(field.text) {
return
}
leftPart := SubstringBefore(field.text, field.cursorOffset)
// Take everything after the left part minus the first character.
rightPart := string([]rune(field.text[len(leftPart):])[1:])
field.text = leftPart + rightPart
}
func (field *AdvancedInputField) Clear() {
field.text = ""
field.cursorOffset = 0
}
func (field *AdvancedInputField) RemovePreviousWord() {
leftPart := SubstringBefore(field.text, field.cursorOffset)
rightPart := field.text[len(leftPart):]
replacement := lastWord.ReplaceAllString(leftPart, "")
field.text = replacement + rightPart
field.cursorOffset -= runewidth.StringWidth(leftPart) - runewidth.StringWidth(replacement)
}
func (field *AdvancedInputField) RemovePreviousCharacter() {
if field.cursorOffset == 0 {
return
}
leftPart := SubstringBefore(field.text, field.cursorOffset)
rightPart := field.text[len(leftPart):]
// Take everything before the right part minus the last character.
leftPartRunes := []rune(leftPart)
leftPartRunes = leftPartRunes[0 : len(leftPartRunes)-1]
leftPart = string(leftPartRunes)
// Figure out what character was removed to correctly decrease cursorOffset.
removedChar := field.text[len(leftPart) : len(field.text)-len(rightPart)]
field.text = leftPart + rightPart
field.cursorOffset -= runewidth.StringWidth(removedChar)
}
func (field *AdvancedInputField) TriggerTabComplete() bool {
if field.tabComplete != nil {
oldWidth := runewidth.StringWidth(field.text)
field.text = field.tabComplete(field.text, field.cursorOffset)
newWidth := runewidth.StringWidth(field.text)
if oldWidth != newWidth {
field.cursorOffset += newWidth - oldWidth
}
return true
}
return false
}
func (field *AdvancedInputField) handleInputChanges(originalText string) {
// Trigger changed events.
if field.text != originalText && field.changed != nil {
field.changed(field.text)
}
// Make sure cursor offset is valid
if field.cursorOffset < 0 {
field.cursorOffset = 0
}
width := runewidth.StringWidth(field.text)
if field.cursorOffset > width {
field.cursorOffset = width
}
}
// InputHandler returns the handler for this primitive. // InputHandler returns the handler for this primitive.
func (field *AdvancedInputField) InputHandler() func(event *tcell.EventKey, setFocus func(p tview.Primitive)) { func (field *AdvancedInputField) InputHandler() func(event *tcell.EventKey, setFocus func(p tview.Primitive)) {
return field.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p tview.Primitive)) { return field.WrapInputHandler(field.inputHandler)
currentText := field.text }
defer func() {
// Trigger changed events. func (field *AdvancedInputField) inputHandler(event *tcell.EventKey, setFocus func(p tview.Primitive)) {
if field.text != currentText && field.changed != nil { defer field.handleInputChanges(field.text)
field.changed(field.text)
} // Process key event.
// Make sure cursor offset is valid switch key := event.Key(); key {
if field.cursorOffset < 0 { case tcell.KeyRune:
field.cursorOffset = 0 field.TypeRune(event.Rune())
} case tcell.KeyCtrlV:
width := runewidth.StringWidth(field.text) field.PasteClipboard()
if field.cursorOffset > width { case tcell.KeyLeft:
field.cursorOffset = width field.MoveCursorLeft(event.Modifiers() == tcell.ModCtrl)
} case tcell.KeyRight:
}() field.MoveCursorRight(event.Modifiers() == tcell.ModCtrl)
case tcell.KeyDelete:
// Process key event. field.RemoveNextCharacter()
switch key := event.Key(); key { case tcell.KeyCtrlU:
case tcell.KeyRune: // Regular character. if field.vimBindings {
leftPart := SubstringBefore(field.text, field.cursorOffset) field.Clear()
newText := leftPart + string(event.Rune()) + field.text[len(leftPart):] }
if field.accept != nil { case tcell.KeyCtrlW:
if !field.accept(newText, event.Rune()) { if field.vimBindings {
break field.RemovePreviousWord()
} }
} case tcell.KeyBackspace:
field.text = newText field.RemovePreviousWord()
field.cursorOffset += runewidth.RuneWidth(event.Rune()) case tcell.KeyBackspace2:
case tcell.KeyCtrlV: field.RemovePreviousCharacter()
clip, _ := clipboard.ReadAll("clipboard") case tcell.KeyTab:
leftPart := SubstringBefore(field.text, field.cursorOffset) if field.TriggerTabComplete() {
field.text = leftPart + clip + field.text[len(leftPart):] break
field.cursorOffset += runewidth.StringWidth(clip) }
case tcell.KeyLeft: // Move cursor left. fallthrough
before := SubstringBefore(field.text, field.cursorOffset) case tcell.KeyEnter, tcell.KeyEscape, tcell.KeyBacktab:
if event.Modifiers() == tcell.ModCtrl { if field.done != nil {
found := lastWord.FindString(before) field.done(key)
field.cursorOffset -= runewidth.StringWidth(found) }
} else if len(before) > 0 { }
beforeRunes := []rune(before)
char := beforeRunes[len(beforeRunes)-1]
field.cursorOffset -= runewidth.RuneWidth(char)
}
case tcell.KeyRight: // Move cursor right.
before := SubstringBefore(field.text, field.cursorOffset)
after := field.text[len(before):]
if event.Modifiers() == tcell.ModCtrl {
found := firstWord.FindString(after)
field.cursorOffset += runewidth.StringWidth(found)
} else if len(after) > 0 {
char := []rune(after)[0]
field.cursorOffset += runewidth.RuneWidth(char)
}
case tcell.KeyDelete: // Delete next character.
if field.cursorOffset >= runewidth.StringWidth(field.text) {
break
}
leftPart := SubstringBefore(field.text, field.cursorOffset)
// Take everything after the left part minus the first character.
rightPart := string([]rune(field.text[len(leftPart):])[1:])
field.text = leftPart + rightPart
case tcell.KeyCtrlU:
if !field.vimBindings {
break
}
field.text = ""
field.cursorOffset = 0
case tcell.KeyCtrlW:
if !field.vimBindings {
break
}
fallthrough
case tcell.KeyBackspace: // Delete last word
leftPart := SubstringBefore(field.text, field.cursorOffset)
rightPart := field.text[len(leftPart):]
replacement := lastWord.ReplaceAllString(leftPart, "")
field.text = replacement + rightPart
field.cursorOffset -= runewidth.StringWidth(leftPart) - runewidth.StringWidth(replacement)
case tcell.KeyBackspace2: // Delete last character
if field.cursorOffset == 0 {
break
}
leftPart := SubstringBefore(field.text, field.cursorOffset)
rightPart := field.text[len(leftPart):]
// Take everything before the right part minus the last character.
leftPartRunes := []rune(leftPart)
leftPartRunes = leftPartRunes[0 : len(leftPartRunes)-1]
leftPart = string(leftPartRunes)
// Figure out what character was removed to correctly decrease cursorOffset.
removedChar := field.text[len(leftPart) : len(field.text)-len(rightPart)]
field.text = leftPart + rightPart
field.cursorOffset -= runewidth.StringWidth(removedChar)
case tcell.KeyTab: // Tab-completion
if field.tabComplete != nil {
oldWidth := runewidth.StringWidth(field.text)
field.text = field.tabComplete(field.text, field.cursorOffset)
newWidth := runewidth.StringWidth(field.text)
if oldWidth != newWidth {
field.cursorOffset += newWidth - oldWidth
}
break
}
fallthrough
case tcell.KeyEnter, tcell.KeyEscape, tcell.KeyBacktab: // We're done.
if field.done != nil {
field.done(key)
}
}
})
} }