inputfields.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. SetDoneFunc(onMessageInputFieldDone(i, s, c)).
  17. SetFieldBackgroundColor(tcell.GetColor(theme.InputFieldBackground)).
  18. SetBackgroundColor(tcell.GetColor(theme.InputFieldBackground)).
  19. SetBorder(true).
  20. SetBorderPadding(0, 0, 1, 1).
  21. SetInputCapture(onMessageInputFieldInputCapture(i))
  22. return i
  23. }
  24. func onMessageInputFieldDone(i *tview.InputField, s *session.Session, c discord.Channel) func(k tcell.Key) {
  25. return func(k tcell.Key) {
  26. if k == tcell.KeyEnter {
  27. t := strings.TrimSpace(i.GetText())
  28. if t == "" {
  29. return
  30. }
  31. i.SetText("")
  32. s.SendMessage(c.ID, t)
  33. }
  34. }
  35. }
  36. func onMessageInputFieldInputCapture(i *tview.InputField) func(event *tcell.EventKey) *tcell.EventKey {
  37. return func(event *tcell.EventKey) *tcell.EventKey {
  38. if event.Key() == tcell.KeyCtrlV {
  39. text, _ := clipboard.ReadAll()
  40. text = i.GetText() + text
  41. i.SetText(text)
  42. }
  43. return event
  44. }
  45. }