DEVELOPMENT ENVIRONMENT

~liljamo/tamma

ref: 0.3.1 tamma/components/confirm/confirm.go -rw-r--r-- 2.1 KiB
7115a178Jonni Liljamo fix: nix build 13 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
 * Copyright (C) 2025 Jonni Liljamo <jonni@liljamo.com>
 *
 * This file is licensed under GPL-3.0-only, see NOTICE and LICENSE for
 * more information.
 */

// Package confirm contains a confirm dialog component for Bubble Tea.
package confirm

import (
	"strings"

	"github.com/charmbracelet/bubbles/help"
	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss"
)

// Result is is a message containing the result of the confirm dialog.
type Result struct {
	Choice bool
}

// Model is the Bubble Tea model for this confirm dialog component.
type Model struct {
	keymap           *KeyMap
	help             help.Model
	dialogStyle      lipgloss.Style
	selectedYesStyle lipgloss.Style
	selectedNoStyle  lipgloss.Style
	choice           bool
}

// New returns a new Model.
func New() Model {
	return Model{
		keymap: NewKeyMap(),
		help:   help.New(),
		dialogStyle: lipgloss.NewStyle().
			AlignHorizontal(lipgloss.Center).
			PaddingLeft(4),
		selectedYesStyle: lipgloss.NewStyle().
			Foreground(lipgloss.Color("113")),
		selectedNoStyle: lipgloss.NewStyle().
			Foreground(lipgloss.Color("167")),
		choice: false,
	}
}

// Init is part of the tea.Model interface.
func (m Model) Init() tea.Cmd {
	return nil
}

// Update is part of the tea.Model interface.
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
	switch msg := msg.(type) {
	case tea.KeyMsg:
		switch msg.String() {
		case m.keymap.Submit.Keys()[0]:
			return m, func() tea.Msg {
				return Result{Choice: m.choice}
			}
		case m.keymap.Back.Keys()[0]:
			return m, func() tea.Msg {
				return Result{Choice: false}
			}
		case "left", "h", "right", "l":
			m.choice = !m.choice
		}
	}

	return m, nil
}

// View is part of the tea.Model interface.
func (m Model) View() string {
	s := strings.Builder{}
	dialog := strings.Builder{}
	dialog.WriteString("Confirm: ")
	if m.choice {
		dialog.WriteString(" no")
		dialog.WriteString(m.selectedYesStyle.Render(" >yes"))
	} else {
		dialog.WriteString(m.selectedNoStyle.Render(">no"))
		dialog.WriteString("  yes")
	}
	dialog.WriteString("\n")
	s.WriteString(m.dialogStyle.Render(dialog.String()))
	s.WriteString(m.help.View(m.keymap))
	return s.String()
}