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

@ -17,8 +17,8 @@
package notification package notification
import ( import (
"os/exec"
"fmt" "fmt"
"os/exec"
"strings" "strings"
) )
@ -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

@ -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,45 +340,28 @@ func SubstringBefore(s string, w int) string {
return runewidth.Truncate(s, w, "") return runewidth.Truncate(s, w, "")
} }
// InputHandler returns the handler for this primitive. func (field *AdvancedInputField) TypeRune(ch rune) {
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)) {
currentText := field.text
defer func() {
// Trigger changed events.
if field.text != currentText && 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
}
}()
// Process key event.
switch key := event.Key(); key {
case tcell.KeyRune: // Regular character.
leftPart := SubstringBefore(field.text, field.cursorOffset) leftPart := SubstringBefore(field.text, field.cursorOffset)
newText := leftPart + string(event.Rune()) + field.text[len(leftPart):] newText := leftPart + string(ch) + field.text[len(leftPart):]
if field.accept != nil { if field.accept != nil {
if !field.accept(newText, event.Rune()) { if !field.accept(newText, ch) {
break return
} }
} }
field.text = newText field.text = newText
field.cursorOffset += runewidth.RuneWidth(event.Rune()) field.cursorOffset += runewidth.RuneWidth(ch)
case tcell.KeyCtrlV: }
func (field *AdvancedInputField) PasteClipboard() {
clip, _ := clipboard.ReadAll("clipboard") clip, _ := clipboard.ReadAll("clipboard")
leftPart := SubstringBefore(field.text, field.cursorOffset) leftPart := SubstringBefore(field.text, field.cursorOffset)
field.text = leftPart + clip + field.text[len(leftPart):] field.text = leftPart + clip + field.text[len(leftPart):]
field.cursorOffset += runewidth.StringWidth(clip) field.cursorOffset += runewidth.StringWidth(clip)
case tcell.KeyLeft: // Move cursor left. }
func (field *AdvancedInputField) MoveCursorLeft(moveWord bool) {
before := SubstringBefore(field.text, field.cursorOffset) before := SubstringBefore(field.text, field.cursorOffset)
if event.Modifiers() == tcell.ModCtrl { if moveWord {
found := lastWord.FindString(before) found := lastWord.FindString(before)
field.cursorOffset -= runewidth.StringWidth(found) field.cursorOffset -= runewidth.StringWidth(found)
} else if len(before) > 0 { } else if len(before) > 0 {
@ -390,46 +369,48 @@ func (field *AdvancedInputField) InputHandler() func(event *tcell.EventKey, setF
char := beforeRunes[len(beforeRunes)-1] char := beforeRunes[len(beforeRunes)-1]
field.cursorOffset -= runewidth.RuneWidth(char) field.cursorOffset -= runewidth.RuneWidth(char)
} }
case tcell.KeyRight: // Move cursor right. }
func (field *AdvancedInputField) MoveCursorRight(moveWord bool) {
before := SubstringBefore(field.text, field.cursorOffset) before := SubstringBefore(field.text, field.cursorOffset)
after := field.text[len(before):] after := field.text[len(before):]
if event.Modifiers() == tcell.ModCtrl { if moveWord {
found := firstWord.FindString(after) found := firstWord.FindString(after)
field.cursorOffset += runewidth.StringWidth(found) field.cursorOffset += runewidth.StringWidth(found)
} else if len(after) > 0 { } else if len(after) > 0 {
char := []rune(after)[0] char := []rune(after)[0]
field.cursorOffset += runewidth.RuneWidth(char) field.cursorOffset += runewidth.RuneWidth(char)
} }
case tcell.KeyDelete: // Delete next character. }
func (field *AdvancedInputField) RemoveNextCharacter() {
if field.cursorOffset >= runewidth.StringWidth(field.text) { if field.cursorOffset >= runewidth.StringWidth(field.text) {
break return
} }
leftPart := SubstringBefore(field.text, field.cursorOffset) leftPart := SubstringBefore(field.text, field.cursorOffset)
// Take everything after the left part minus the first character. // Take everything after the left part minus the first character.
rightPart := string([]rune(field.text[len(leftPart):])[1:]) rightPart := string([]rune(field.text[len(leftPart):])[1:])
field.text = leftPart + rightPart field.text = leftPart + rightPart
case tcell.KeyCtrlU:
if !field.vimBindings {
break
} }
func (field *AdvancedInputField) Clear() {
field.text = "" field.text = ""
field.cursorOffset = 0 field.cursorOffset = 0
case tcell.KeyCtrlW:
if !field.vimBindings {
break
} }
fallthrough
case tcell.KeyBackspace: // Delete last word func (field *AdvancedInputField) RemovePreviousWord() {
leftPart := SubstringBefore(field.text, field.cursorOffset) leftPart := SubstringBefore(field.text, field.cursorOffset)
rightPart := field.text[len(leftPart):] rightPart := field.text[len(leftPart):]
replacement := lastWord.ReplaceAllString(leftPart, "") replacement := lastWord.ReplaceAllString(leftPart, "")
field.text = replacement + rightPart field.text = replacement + rightPart
field.cursorOffset -= runewidth.StringWidth(leftPart) - runewidth.StringWidth(replacement) field.cursorOffset -= runewidth.StringWidth(leftPart) - runewidth.StringWidth(replacement)
case tcell.KeyBackspace2: // Delete last character }
func (field *AdvancedInputField) RemovePreviousCharacter() {
if field.cursorOffset == 0 { if field.cursorOffset == 0 {
break return
} }
leftPart := SubstringBefore(field.text, field.cursorOffset) leftPart := SubstringBefore(field.text, field.cursorOffset)
rightPart := field.text[len(leftPart):] rightPart := field.text[len(leftPart):]
@ -445,7 +426,9 @@ func (field *AdvancedInputField) InputHandler() func(event *tcell.EventKey, setF
field.text = leftPart + rightPart field.text = leftPart + rightPart
field.cursorOffset -= runewidth.StringWidth(removedChar) field.cursorOffset -= runewidth.StringWidth(removedChar)
case tcell.KeyTab: // Tab-completion }
func (field *AdvancedInputField) TriggerTabComplete() bool {
if field.tabComplete != nil { if field.tabComplete != nil {
oldWidth := runewidth.StringWidth(field.text) oldWidth := runewidth.StringWidth(field.text)
field.text = field.tabComplete(field.text, field.cursorOffset) field.text = field.tabComplete(field.text, field.cursorOffset)
@ -453,13 +436,67 @@ func (field *AdvancedInputField) InputHandler() func(event *tcell.EventKey, setF
if oldWidth != newWidth { if oldWidth != newWidth {
field.cursorOffset += newWidth - oldWidth 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.
func (field *AdvancedInputField) InputHandler() func(event *tcell.EventKey, setFocus func(p tview.Primitive)) {
return field.WrapInputHandler(field.inputHandler)
}
func (field *AdvancedInputField) inputHandler(event *tcell.EventKey, setFocus func(p tview.Primitive)) {
defer field.handleInputChanges(field.text)
// Process key event.
switch key := event.Key(); key {
case tcell.KeyRune:
field.TypeRune(event.Rune())
case tcell.KeyCtrlV:
field.PasteClipboard()
case tcell.KeyLeft:
field.MoveCursorLeft(event.Modifiers() == tcell.ModCtrl)
case tcell.KeyRight:
field.MoveCursorRight(event.Modifiers() == tcell.ModCtrl)
case tcell.KeyDelete:
field.RemoveNextCharacter()
case tcell.KeyCtrlU:
if field.vimBindings {
field.Clear()
}
case tcell.KeyCtrlW:
if field.vimBindings {
field.RemovePreviousWord()
}
case tcell.KeyBackspace:
field.RemovePreviousWord()
case tcell.KeyBackspace2:
field.RemovePreviousCharacter()
case tcell.KeyTab:
if field.TriggerTabComplete() {
break break
} }
fallthrough fallthrough
case tcell.KeyEnter, tcell.KeyEscape, tcell.KeyBacktab: // We're done. case tcell.KeyEnter, tcell.KeyEscape, tcell.KeyBacktab:
if field.done != nil { if field.done != nil {
field.done(key) field.done(key)
} }
} }
})
} }