inputfields.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package ui
  2. import (
  3. "strings"
  4. "github.com/atotto/clipboard"
  5. "github.com/diamondburned/arikawa/v3/discord"
  6. "github.com/diamondburned/arikawa/v3/session"
  7. "github.com/gdamore/tcell/v2"
  8. "github.com/rigormorrtiss/discordo/util"
  9. "github.com/rivo/tview"
  10. )
  11. func NewMessageInputField(s *session.Session, c discord.Channel, theme *util.Theme) *tview.InputField {
  12. i := tview.NewInputField()
  13. i.
  14. SetPlaceholder("Message...").
  15. SetFieldWidth(0).
  16. SetFieldBackgroundColor(tcell.GetColor(theme.InputFieldBackground)).
  17. SetBackgroundColor(tcell.GetColor(theme.InputFieldBackground)).
  18. SetBorder(true).
  19. SetBorderPadding(0, 0, 1, 1).
  20. SetInputCapture(onMessageInputFieldInputCapture(i, s, c))
  21. return i
  22. }
  23. func onMessageInputFieldInputCapture(i *tview.InputField, s *session.Session, c discord.Channel) func(event *tcell.EventKey) *tcell.EventKey {
  24. return func(event *tcell.EventKey) *tcell.EventKey {
  25. switch event.Key() {
  26. case tcell.KeyEnter:
  27. t := strings.TrimSpace(i.GetText())
  28. if t == "" {
  29. return nil
  30. }
  31. i.SetText("")
  32. s.SendMessage(c.ID, t)
  33. case tcell.KeyCtrlV:
  34. text, _ := clipboard.ReadAll()
  35. text = i.GetText() + text
  36. i.SetText(text)
  37. }
  38. return event
  39. }
  40. }