~rafael/gembro

4ac6ab6ce936f3c700b2768b523dc827d0d41e17 — RafaĆ«l L. Bekkema 3 years ago cbe9b9e
Make message buttons clickable
3 files changed, 37 insertions(+), 8 deletions(-)

M input.go
M message.go
M tab.go
M input.go => input.go +2 -2
@@ 45,7 45,7 @@ func (inp Input) Update(msg tea.Msg) (Input, tea.Cmd) {
		case "enter":
			inp.input.Blur()
			cmds = append(cmds, fireEvent(InputEvent{Value: inp.input.Value(), Type: inp.Type, Payload: inp.Payload}))
		case "ctrl+q":
		case "esc":
			inp.input.Blur()
			cmds = append(cmds, fireEvent(CloseInputEvent{}))
		}


@@ 57,5 57,5 @@ func (inp Input) Update(msg tea.Msg) (Input, tea.Cmd) {
}

func (inp Input) View() string {
	return fmt.Sprintf("%s %s\n\nPress ENTER to continue or ctrl+q to cancel", inp.Message, inp.input.View())
	return fmt.Sprintf("%s %s\n\nPress ENTER to continue or Escape to cancel", inp.Message, inp.input.View())
}

M message.go => message.go +34 -4
@@ 2,6 2,7 @@ package main

import (
	"fmt"
	"strings"

	"git.sr.ht/~rafael/gembro/text"
	tea "github.com/charmbracelet/bubbletea"


@@ 12,6 13,13 @@ type Message struct {
	Type        int
	WithConfirm bool
	Payload     string
	actionY     int
}

func NewMessage(message string, typ int, withConfirm bool, payload string) Message {
	msg := text.Wrap(message, 80)
	actionY := strings.Count(msg, "\n") + headerHeight + 1
	return Message{msg, typ, withConfirm, payload, actionY}
}

func (m Message) Update(msg tea.Msg) (Message, tea.Cmd) {


@@ 19,17 27,39 @@ func (m Message) Update(msg tea.Msg) (Message, tea.Cmd) {
	switch msg := msg.(type) {
	case tea.KeyMsg:
		switch skey := msg.String(); skey {
		case "y", "n", "enter", "q":
		case "y", "n", "enter", "q", "esc":
			cmds = append(cmds, fireEvent(MessageEvent{Response: skey == "y", Type: m.Type, Payload: m.Payload}))
		}
	case tea.MouseMsg:
		return m, m.handleClick(msg)
	}
	return m, tea.Batch(cmds...)
}

func (m Message) handleClick(msg tea.MouseMsg) tea.Cmd {
	if msg.Y != m.actionY {
		return nil
	}
	if m.WithConfirm {
		yes := 1 <= msg.X && msg.X < 4
		no := 10 <= msg.X && msg.X < 12
		if yes || no {
			return fireEvent(MessageEvent{Response: yes, Type: m.Type, Payload: m.Payload})
		}
		return nil
	}
	if 1 <= msg.X && msg.X < 3 {
		return fireEvent(MessageEvent{Response: false, Type: m.Type, Payload: m.Payload})
	}
	return nil
}

func (m Message) View() string {
	msg := text.Wrap(m.Message, 80)
	if m.WithConfirm {
		return fmt.Sprintf("%s\n\n(Y)es or (N)o", msg)
		return fmt.Sprintf("%s\n\n[%s] or [%s]",
			m.Message,
			text.Color("Yes", text.Clink),
			text.Color("No", text.Clink))
	}
	return fmt.Sprintf("%s\n\nPress ENTER or q to continue", msg)
	return fmt.Sprintf("%s\n\n[%s]", m.Message, text.Color("OK", text.Clink))
}

M tab.go => tab.go +1 -2
@@ 179,8 179,7 @@ func (tab Tab) View() string {
}

func (tab Tab) showMessage(msg, payload string, typ int, withConfirm bool) (Tab, tea.Cmd) {
	tab.message = Message{Message: msg,
		WithConfirm: withConfirm, Type: typ, Payload: payload}
	tab.message = NewMessage(msg, typ, withConfirm, payload)
	tab.mode = modeMessage
	return tab, nil
}