Skip to content

Commit b2dd452

Browse files
added poups for errors enabled dynamic table scaling
1 parent f41d866 commit b2dd452

4 files changed

Lines changed: 48 additions & 56 deletions

File tree

ui/tui/models/views/popup/message_model.go

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,18 @@ const (
2222
type MessageSeverity int
2323

2424
type MessageModel struct {
25-
form form.Form[struct{}]
26-
innerSize util.Size
27-
size util.Size
25+
form form.Form[struct{}]
26+
size util.Size
2827
}
2928

30-
func OpenMessage(severity MessageSeverity, message string, cmd tea.Cmd, width, height int) tea.Cmd {
31-
return popup.Open(util.ModelPointer(newMessage(severity, message, cmd, width, height)))
29+
func OpenMessage(severity MessageSeverity, message string, cmd tea.Cmd) tea.Cmd {
30+
return popup.Open(util.ModelPointer(newMessage(severity, message, cmd)))
3231
}
3332

3433
func newMessage(
3534
severity MessageSeverity,
3635
message string,
3736
cmd tea.Cmd,
38-
width int,
39-
height int,
4037
) *MessageModel {
4138
switch severity {
4239
case MessageInfo:
@@ -56,10 +53,6 @@ func newMessage(
5653
return tea.Sequence(popup.Close(), cmd)
5754
}),
5855
),
59-
innerSize: util.Size{
60-
Width: width,
61-
Height: height,
62-
},
6356
}
6457
}
6558

@@ -70,26 +63,24 @@ func (m MessageModel) Init() tea.Cmd {
7063
func (m *MessageModel) Update(msg tea.Msg) tea.Cmd {
7164
if m.size.UpdateFromMsg(msg) {
7265
size := util.Size{
73-
Width: min(m.innerSize.Width, m.size.Width),
74-
Height: min(m.innerSize.Height, m.size.Height),
66+
Width: util.Clamp(6, m.size.Width/2, m.size.Width),
67+
Height: util.Clamp(7, m.size.Height/2, m.size.Height),
7568
}
7669
return m.form.Update(size.ToMsg())
7770
}
7871
return m.form.Update(msg)
7972
}
8073

8174
func (m MessageModel) View() string {
82-
// TODO only for testing... size of form needs to be made non greedy
83-
return lipgloss.NewStyle().MaxWidth(40).Render(m.form.View())
84-
// return m.form.View()
75+
return lipgloss.NewStyle().
76+
MaxWidth(m.size.Width).
77+
MaxHeight(m.size.Height).
78+
Render(m.form.View())
8579
}
8680

87-
func (m *MessageModel) Focus(parentKeyMap help.KeyMap) tea.Cmd {
88-
return m.form.Focus(parentKeyMap)
89-
}
90-
func (m *MessageModel) Blur() {
91-
m.form.Blur()
92-
}
81+
func (m *MessageModel) Focus(parentKeyMap help.KeyMap) tea.Cmd { return m.form.Focus(parentKeyMap) }
82+
83+
func (m *MessageModel) Blur() { m.form.Blur() }
9384

9485
// *[MessageModel] implements [util.Model]
9586
var _ util.Model = (*MessageModel)(nil)

ui/tui/models/views/publickey/create_model.go

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,22 @@ func (m *CreateModel) Init() tea.Cmd {
7171
form.WithOnCancel[createFormImport](func() tea.Cmd { return popup.Close() }),
7272
form.WithOnSubmit(func(result createFormImport, err error) tea.Cmd {
7373
if err != nil {
74-
return popupviews.OpenMessage(popupviews.MessageError, err.Error(), nil, 50, 20)
75-
}
76-
77-
// TODO parse result.key
78-
parts := strings.Split(result.Key, " ")
79-
80-
if len(parts) < 2 || len(parts) > 3 {
81-
return popupviews.OpenMessage(popupviews.MessageError, "unable to parse public key", nil, 50, 20)
74+
return popupviews.OpenMessage(popupviews.MessageError, err.Error(), nil)
8275
}
8376

8477
var data, algorithm, comment string
85-
data, algorithm = parts[0], parts[1]
86-
if len(parts) == 3 {
87-
comment = parts[2]
78+
// TODO parse result.key... using this mock for now:
79+
{
80+
parts := strings.Split(result.Key, " ")
81+
82+
if len(parts) < 2 || len(parts) > 3 {
83+
return popupviews.OpenMessage(popupviews.MessageError, "unable to parse public key", nil)
84+
}
85+
86+
data, algorithm = parts[0], parts[1]
87+
if len(parts) == 3 {
88+
comment = parts[2]
89+
}
8890
}
8991

9092
return tea.Sequence(popup.Close(), func() tea.Msg { return createMsgImportResult{data, algorithm, comment} })
@@ -130,13 +132,7 @@ func (m *CreateModel) Init() tea.Cmd {
130132
form.WithInitialData(util.DerefOrNullValue(m.preset)),
131133
))
132134

133-
initCmd := m.form.Init()
134-
135-
// if m.preset != nil {
136-
// _ = m.form.Set(*m.preset)
137-
// }
138-
139-
return initCmd
135+
return m.form.Init()
140136
}
141137

142138
// Update implements util.Model.
@@ -163,8 +159,7 @@ func (m *CreateModel) Update(msg tea.Msg) tea.Cmd {
163159
case createMsgCreateResult:
164160
m.locked = nil
165161
if msg.err != nil {
166-
// TODO open popup displaying error
167-
return nil
162+
return popupviews.OpenMessage(popupviews.MessageError, "Error creating Public Key:\n"+msg.err.Error(), nil)
168163
}
169164
return tea.Sequence(m.rc.Pop(1), func() tea.Msg { return CreateMsgCreated{msg.publicKeyId} })
170165

@@ -175,7 +170,7 @@ func (m *CreateModel) Update(msg tea.Msg) tea.Cmd {
175170
return nil
176171
}
177172

178-
// pass key msg to form
173+
// pass remaining msgs to form
179174
return m.form.Update(msg)
180175
}
181176

ui/tui/models/views/publickey/edit_model.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/toeirei/keymaster/ui/tui/models/components/router"
1313
"github.com/toeirei/keymaster/ui/tui/models/helpers/form"
1414
formelement "github.com/toeirei/keymaster/ui/tui/models/helpers/form/element"
15+
popupviews "github.com/toeirei/keymaster/ui/tui/models/views/popup"
1516
"github.com/toeirei/keymaster/ui/tui/util"
1617
"github.com/toeirei/keymaster/ui/tui/util/keys"
1718
)
@@ -105,17 +106,15 @@ func (m *EditModel) Update(msg tea.Msg) tea.Cmd {
105106
m.publicKey = msg.publicKey
106107
_ = m.refreshForm()
107108
if msg.err != nil {
108-
// TODO open popup displaying error
109-
return nil
109+
return popupviews.OpenMessage(popupviews.MessageError, "Error loading Public Key:\n"+msg.err.Error(), nil)
110110
}
111111
return nil
112112

113113
case editMsgUpdateResult:
114114
m.locked = nil
115115
if msg.err != nil {
116116
if msg.err != nil {
117-
// TODO open popup displaying error
118-
return nil
117+
return popupviews.OpenMessage(popupviews.MessageError, "Error updating Public Key:\n"+msg.err.Error(), nil)
119118
}
120119
return nil
121120
}

ui/tui/models/views/publickey/list_model.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
popupviews "github.com/toeirei/keymaster/ui/tui/models/views/popup"
1919
"github.com/toeirei/keymaster/ui/tui/util"
2020
"github.com/toeirei/keymaster/ui/tui/util/keys"
21+
"github.com/toeirei/keymaster/util/slicest"
2122
)
2223

2324
type ListModel struct {
@@ -78,12 +79,10 @@ func (m *ListModel) Update(msg tea.Msg) tea.Cmd {
7879
case listMsgDeleteResult:
7980
m.locked = nil
8081
if msg.err != nil {
81-
// TODO show popup with error
82-
return nil
82+
return popupviews.OpenMessage(popupviews.MessageError, "Error deleting Public Key:\n"+msg.err.Error(), nil)
8383
}
8484
m.publicKeys = slices.DeleteFunc(m.publicKeys, func(pk client.PublicKey) bool { return pk.Id == msg.publicKey.Id })
8585
m.refreshTable()
86-
// TODO does not work for some reason
8786
return nil
8887

8988
case EditMsgUpdated, CreateMsgCreated:
@@ -100,7 +99,7 @@ func (m *ListModel) Update(msg tea.Msg) tea.Cmd {
10099

101100
case key.Matches(msg, ListBaseKeyMap.Edit):
102101
if m.table.Cursor() == -1 {
103-
return nil // TODO open popup with "please select a public key" text
102+
return popupviews.OpenMessage(popupviews.MessageInfo, "Please select a Public Key to edit.", nil)
104103
}
105104
return m.rc.Push(util.ModelPointer(NewEdit(
106105
m.client,
@@ -110,7 +109,7 @@ func (m *ListModel) Update(msg tea.Msg) tea.Cmd {
110109

111110
case key.Matches(msg, ListBaseKeyMap.Duplicate):
112111
if m.table.Cursor() == -1 {
113-
return nil // TODO open popup with "please select a public key" text
112+
return popupviews.OpenMessage(popupviews.MessageInfo, "Please select a Public Key to duplicate.", nil)
114113
}
115114
publicKey := m.publicKeys[m.table.Cursor()]
116115
return m.rc.Push(util.ModelPointer(NewCreate(m.client, m.rc, &createFormData{publicKey.Data, publicKey.Algorithm, publicKey.Comment, tagsStringify(publicKey.Tags)})))
@@ -197,11 +196,19 @@ func (m *ListModel) reload() tea.Cmd {
197196
}
198197

199198
func (m *ListModel) refreshTable() {
200-
// TODO this code is just a prove of concept and needs improvements like dynamic scaling!
199+
algorithmWidth := slicest.Reduce(m.publicKeys, func(k client.PublicKey, w int) int { return max(w, len(k.Algorithm)) })
200+
commentWidth := slicest.Reduce(m.publicKeys, func(k client.PublicKey, w int) int { return max(w, len(k.Comment)) })
201+
tagsWidth := slicest.Reduce(m.publicKeys, func(k client.PublicKey, w int) int { return max(w, len(strings.Join(k.Tags, ", "))) })
202+
203+
// tags take 50% screen max
204+
tagsWidth = min((m.size.Width-6)/2, tagsWidth)
205+
206+
remainingWidth := m.size.Width - 6 - algorithmWidth - commentWidth - tagsWidth
207+
201208
m.table.SetColumns([]table.Column{
202-
{Title: "Algorithm", Width: 10},
203-
{Title: "Comment", Width: 10},
204-
{Title: "Tags", Width: m.size.Width - 20 - 6},
209+
{Title: "Algorithm", Width: algorithmWidth + remainingWidth/3},
210+
{Title: "Comment", Width: commentWidth + remainingWidth/3},
211+
{Title: "Tags", Width: tagsWidth + remainingWidth/3},
205212
})
206213

207214
m.table.SetRows(slices.Map(m.publicKeys, func(publicKey client.PublicKey) table.Row {

0 commit comments

Comments
 (0)