summaryrefslogtreecommitdiff
path: root/internal/client/ui/field/field.go
blob: 3b04bc9f8d9c87f4cb29763c8d0b206af0025f50 (plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Eko: A terminal-native social media platform
// Copyright (C) 2025 Kyren223
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

package field

import (
	"github.com/charmbracelet/bubbles/textinput"
	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss"

	"github.com/kyren223/eko/internal/client/ui"
)

type Model struct {
	Input    textinput.Model
	Header   string
	Visisble bool
	width    int

	reveal      bool
	revealIcon  string
	concealIcon string
	icon        string

	FocusedStyle     lipgloss.Style
	BlurredStyle     lipgloss.Style
	FocusedTextStyle lipgloss.Style
	BlurredTextStyle lipgloss.Style
	ErrorStyle       lipgloss.Style
	HeaderStyle      lipgloss.Style
}

func New(width int) Model {
	input := textinput.New()
	input.Prompt = ""
	input.Width = width

	m := Model{
		width:    width,
		Visisble: true,
		Input:    input,
	}

	m.Blur()
	m.SetRevealed(true)

	return m
}

func (m Model) Init() tea.Cmd {
	return nil
}

func (m Model) View() string {
	if !m.Visisble {
		return ""
	}

	style := m.BlurredStyle
	if m.Input.Focused() {
		style = m.FocusedStyle
	}

	header := m.HeaderStyle.Render(m.Header)
	if m.Input.Err != nil {
		error := m.Input.Err.Error()
		style = style.BorderForeground(m.ErrorStyle.GetForeground())
		header = lipgloss.NewStyle().
			MaxWidth(m.width).
			Render(header + m.ErrorStyle.Render(" - "+error))
	}

	input := m.Input.View() + m.icon

	field := ui.AddBorderHeader(header, 1, style, input)
	return field
}

func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
	if !m.Visisble {
		return m, nil
	}

	var cmd tea.Cmd
	m.Input, cmd = m.Input.Update(msg)
	return m, cmd
}

func (m *Model) Focus() tea.Cmd {
	m.Input.PromptStyle = m.FocusedTextStyle
	m.Input.TextStyle = m.FocusedTextStyle
	m.Input.Cursor.Style = m.FocusedTextStyle
	m.Input.Cursor.TextStyle = m.FocusedTextStyle
	return m.Input.Focus()
}

func (m *Model) Blur() {
	m.Input.PromptStyle = m.BlurredTextStyle
	m.Input.TextStyle = m.BlurredTextStyle
	m.Input.Cursor.Style = m.BlurredTextStyle
	m.Input.Cursor.TextStyle = m.BlurredTextStyle
	m.Input.Blur()
}

func (m *Model) SetWidth(width int) {
	m.width = width
	m.recalculateWidth()
}

func (m Model) Width() int {
	return m.width
}

func (m *Model) SetRevealed(revealed bool) {
	m.reveal = revealed
	if m.reveal {
		m.Input.EchoMode = textinput.EchoNormal
		m.icon = m.revealIcon
	} else {
		m.Input.EchoMode = textinput.EchoPassword
		m.icon = m.concealIcon
	}
	m.recalculateWidth()
}

func (m Model) Revealed() bool {
	return m.reveal
}

func (m *Model) SetRevealIcon(icon string) {
	m.revealIcon = icon
	if m.reveal {
		m.icon = icon
	}
	m.recalculateWidth()
}

func (m Model) RevealIcon() string {
	return m.revealIcon
}

func (m *Model) SetConcealIcon(icon string) {
	m.concealIcon = icon
	if !m.reveal {
		m.icon = icon
	}
	m.recalculateWidth()
}

func (m Model) ConcealIcon() string {
	return m.concealIcon
}

func (m *Model) recalculateWidth() {
	m.Input.Width = m.width - lipgloss.Width(m.icon)
}