d05bbd007b
Extract shared computeTableLayout() into table_helpers.go — takes column definitions with short/full headers, min/max widths, and a flex column that absorbs surplus space. All tabs now use it: - Alerts: CONFIG column is flex, NAME/TYPE/SENT expand with width - Maint: TITLE column is flex, TYPE/MONITORS/STATUS/dates expand - Nodes: NAME column is flex, REGION/LAST SEEN/VERSION expand - Users: PUBLIC KEY column is flex, USERNAME expands - Sites: uses same colDef type (keeps special dual-flex for NAME+HISTORY) Headers auto-switch short/full based on available width across all tabs.
124 lines
2.6 KiB
Go
124 lines
2.6 KiB
Go
package tui
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"github.com/charmbracelet/huh"
|
|
)
|
|
|
|
type userFormData struct {
|
|
Username string
|
|
PublicKey string
|
|
Role string
|
|
}
|
|
|
|
func fmtRole(role string) string {
|
|
if role == "admin" {
|
|
return specialStyle.Render(role)
|
|
}
|
|
return role
|
|
}
|
|
|
|
func fmtKey(key string) string {
|
|
if len(key) > 40 {
|
|
return key[:20] + "..." + key[len(key)-17:]
|
|
}
|
|
return key
|
|
}
|
|
|
|
func (m Model) viewUsersTab() string {
|
|
if len(m.users) == 0 {
|
|
return "\n No users configured. Press [n] to add one."
|
|
}
|
|
|
|
cols := []colDef{
|
|
{"#", "#", 4, 4, false},
|
|
{"USER", "USERNAME", 10, 18, false},
|
|
{"ROLE", "ROLE", 8, 10, false},
|
|
{"KEY", "PUBLIC KEY", 20, 60, true},
|
|
}
|
|
headers, widths := m.computeTableLayout(cols, 0)
|
|
userW := widths[1]
|
|
|
|
return m.renderTable(
|
|
headers,
|
|
len(m.users),
|
|
func(start, end int) [][]string {
|
|
var rows [][]string
|
|
for i := start; i < end; i++ {
|
|
u := m.users[i]
|
|
rows = append(rows, []string{
|
|
fmt.Sprintf("%d", i+1),
|
|
m.zones.Mark(fmt.Sprintf("user-%d", i), limitStr(u.Username, userW-2)),
|
|
fmtRole(u.Role),
|
|
fmtKey(u.PublicKey),
|
|
})
|
|
}
|
|
return rows
|
|
},
|
|
widths, nil,
|
|
)
|
|
}
|
|
|
|
func (m *Model) initUserHuhForm() tea.Cmd {
|
|
m.userFormData = &userFormData{
|
|
Role: "user",
|
|
}
|
|
|
|
if m.editID > 0 {
|
|
for _, u := range m.users {
|
|
if u.ID == m.editID {
|
|
m.userFormData.Username = u.Username
|
|
m.userFormData.PublicKey = u.PublicKey
|
|
m.userFormData.Role = u.Role
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
m.huhForm = huh.NewForm(
|
|
huh.NewGroup(
|
|
huh.NewInput().Title("Username").
|
|
Placeholder("admin").
|
|
Value(&m.userFormData.Username).
|
|
Validate(func(s string) error {
|
|
if s == "" {
|
|
return fmt.Errorf("username is required")
|
|
}
|
|
return nil
|
|
}),
|
|
huh.NewInput().Title("SSH Public Key").
|
|
Placeholder("ssh-ed25519 AAAA...").
|
|
Value(&m.userFormData.PublicKey).
|
|
Validate(func(s string) error {
|
|
if s == "" {
|
|
return fmt.Errorf("public key is required")
|
|
}
|
|
return nil
|
|
}),
|
|
huh.NewSelect[string]().Title("Role").
|
|
Options(
|
|
huh.NewOption("User", "user"),
|
|
huh.NewOption("Admin", "admin"),
|
|
).Value(&m.userFormData.Role),
|
|
).Title("SSH Access"),
|
|
).WithTheme(m.theme.HuhTheme())
|
|
|
|
return m.huhForm.Init()
|
|
}
|
|
|
|
func (m *Model) submitUserForm() {
|
|
d := m.userFormData
|
|
if m.editID > 0 {
|
|
if err := m.store.UpdateUser(m.editID, d.Username, d.PublicKey, d.Role); err != nil {
|
|
m.engine.AddLog("Update user failed: " + err.Error())
|
|
}
|
|
} else {
|
|
if err := m.store.AddUser(d.Username, d.PublicKey, d.Role); err != nil {
|
|
m.engine.AddLog("Add user failed: " + err.Error())
|
|
}
|
|
}
|
|
m.state = stateUsers
|
|
}
|