Rewrite MessageInput to use TextArea (#316)

This commit is contained in:
ayn2op 2023-07-18 00:21:50 +05:30 • committed by GitHub
parent 592f857b27
commit e363f208e9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 26 deletions

View file

@ -16,17 +16,24 @@ import (
)
type MessageInput struct {
*tview.InputField
*tview.TextArea
}
func newMessageInput() *MessageInput {
mi := &MessageInput{
InputField: tview.NewInputField(),
TextArea: tview.NewTextArea(),
}
mi.SetTextStyle(tcell.StyleDefault.Background(tcell.GetColor(config.Current.Theme.BackgroundColor)))
mi.SetClipboard(func(s string) {
_ = clipboard.WriteAll(s)
}, func() string {
text, _ := clipboard.ReadAll()
return text
})
mi.SetInputCapture(mi.onInputCapture)
mi.SetBackgroundColor(tcell.GetColor(config.Current.Theme.BackgroundColor))
mi.SetFieldBackgroundColor(tcell.GetColor(config.Current.Theme.BackgroundColor))
mi.SetTitleColor(tcell.GetColor(config.Current.Theme.TitleColor))
mi.SetTitleAlign(tview.AlignLeft)
@ -41,7 +48,7 @@ func newMessageInput() *MessageInput {
func (mi *MessageInput) reset() {
mi.SetTitle("")
mi.SetText("")
mi.SetText("", true)
}
func (mi *MessageInput) onInputCapture(event *tcell.EventKey) *tcell.EventKey {
@ -49,9 +56,8 @@ func (mi *MessageInput) onInputCapture(event *tcell.EventKey) *tcell.EventKey {
case config.Current.Keys.MessageInput.Send:
mi.sendAction()
return nil
case config.Current.Keys.MessageInput.Paste:
mi.pasteAction()
return nil
case "Alt+Enter":
return tcell.NewEventKey(tcell.KeyEnter, 0, tcell.ModNone)
case config.Current.Keys.MessageInput.LaunchEditor:
mainFlex.messageInput.launchEditorAction()
return nil
@ -100,17 +106,6 @@ func (mi *MessageInput) sendAction() {
mi.reset()
}
func (mi *MessageInput) pasteAction() {
text, err := clipboard.ReadAll()
if err != nil {
log.Println(err)
return
}
// Append the text to the message input.
mi.SetText(mi.GetText() + text)
}
func (mi *MessageInput) launchEditorAction() {
e := config.Current.Editor
if e == "default" {
@ -129,5 +124,5 @@ func (mi *MessageInput) launchEditorAction() {
}
})
mi.SetText(b.String())
mi.SetText(b.String(), true)
}

View file

@ -26,8 +26,6 @@ type (
Focus string `yaml:"focus"`
Send string `yaml:"send"`
Paste string `yaml:"paste"`
LaunchEditor string `yaml:"launch_editor"`
}
)
@ -69,8 +67,6 @@ func defKeys() Keys {
Focus: "Alt+Rune[i]",
Send: "Enter",
Paste: "Ctrl+V",
LaunchEditor: "Ctrl+E",
},
}

View file

@ -35,5 +35,4 @@ Keybindings are configurable in the [configuration file](#configuration).
| **Message Input** | |
| Focus | Alt + i |
| Send message | Enter |
| Paste from clipboard | Ctrl + v |
| Launch editor | Ctrl + e |