Something went wrong. Try again.
tui monitor configuration tool for niri with visual layout, drag-and-drop, and profile management
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031package main
import ( "encoding/json" "fmt" "os" "path/filepath" "strings" "time"
tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" "golang.org/x/term")
// customConfigPath holds the custom configuration path set via -cfg flagvar customConfigPath string
type Profile struct { Name string `json:"name"` Monitors []Monitor `json:"monitors"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"`}
func getProfilesDir() string { // Use custom config path if provided via -cfg flag if customConfigPath != "" { return filepath.Join(customConfigPath, "profiles") }
// Otherwise use default path home, err := os.UserHomeDir() if err != nil { return "" } return filepath.Join(home, ".config", "nirimon", "profiles")}
func ensureProfilesDir() error { dir := getProfilesDir() if dir == "" { return fmt.Errorf("could not determine profiles directory") } return os.MkdirAll(dir, profileDirMode)}
func saveProfile(name string, monitors []Monitor) error { if err := ensureProfilesDir(); err != nil { return err }
profile := Profile{ Name: name, Monitors: monitors, CreatedAt: time.Now(), UpdatedAt: time.Now(), }
filename := filepath.Join(getProfilesDir(), fmt.Sprintf("%s.json", name))
if _, err := os.Stat(filename); err == nil { existingProfile, err := loadProfile(name) if err == nil { profile.CreatedAt = existingProfile.CreatedAt } }
data, err := json.MarshalIndent(profile, "", " ") if err != nil { return fmt.Errorf("failed to marshal profile: %w", err) }
if err := os.WriteFile(filename, data, profileFileMode); err != nil { return fmt.Errorf("failed to write profile file: %w", err) } return nil}
func loadProfile(name string) (*Profile, error) { filename := filepath.Join(getProfilesDir(), fmt.Sprintf("%s.json", name))
data, err := os.ReadFile(filename) if err != nil { return nil, fmt.Errorf("failed to read profile file: %w", err) }
var profile Profile if err := json.Unmarshal(data, &profile); err != nil { return nil, fmt.Errorf("failed to unmarshal profile: %w", err) }
return &profile, nil}
func listProfiles() ([]string, error) { dir := getProfilesDir() if dir == "" { return nil, fmt.Errorf("could not determine profiles directory") }
entries, err := os.ReadDir(dir) if err != nil { if os.IsNotExist(err) { return []string{}, nil } return nil, fmt.Errorf("failed to read profiles directory: %w", err) }
var profiles []string for _, entry := range entries { if !entry.IsDir() && strings.HasSuffix(entry.Name(), ".json") { name := strings.TrimSuffix(entry.Name(), ".json") profiles = append(profiles, name) } }
return profiles, nil}
func deleteProfile(name string) error { filename := filepath.Join(getProfilesDir(), fmt.Sprintf("%s.json", name)) return os.Remove(filename)}
func getProfileOrderFile() string { dir := getProfilesDir() if dir == "" { return "" } return filepath.Join(dir, ".profile_order")}
func loadProfileOrder() ([]string, error) { filename := getProfileOrderFile() if filename == "" { return nil, fmt.Errorf("could not determine profile order file") }
data, err := os.ReadFile(filename) if err != nil { if os.IsNotExist(err) { return []string{}, nil } return nil, fmt.Errorf("failed to read profile order file: %w", err) }
var order []string if err := json.Unmarshal(data, &order); err != nil { return nil, fmt.Errorf("failed to unmarshal profile order: %w", err) }
return order, nil}
func saveProfileOrder(order []string) error { filename := getProfileOrderFile() if filename == "" { return fmt.Errorf("could not determine profile order file") }
if err := ensureProfilesDir(); err != nil { return fmt.Errorf("failed to ensure profiles directory: %w", err) }
data, err := json.MarshalIndent(order, "", " ") if err != nil { return fmt.Errorf("failed to marshal profile order: %w", err) }
if err := os.WriteFile(filename, data, profileFileMode); err != nil { return fmt.Errorf("failed to write profile order: %w", err) } return nil}
func renameProfile(oldName, newName string) error { if oldName == newName { return nil }
// Check if new name already exists newFilename := filepath.Join(getProfilesDir(), fmt.Sprintf("%s.json", newName)) if _, err := os.Stat(newFilename); err == nil { return fmt.Errorf("profile '%s' already exists", newName) }
// Load the old profile profile, err := loadProfile(oldName) if err != nil { return fmt.Errorf("failed to load old profile: %w", err) }
// Update the name profile.Name = newName
// Save with new name if err := saveProfile(newName, profile.Monitors); err != nil { return fmt.Errorf("failed to save renamed profile: %w", err) }
// Delete old file if err := deleteProfile(oldName); err != nil { return fmt.Errorf("failed to delete old profile: %w", err) } return nil}
func applyProfile(name string) error { profile, err := loadProfile(name) if err != nil { return fmt.Errorf("failed to load profile %s: %w", name, err) }
currentMonitors, err := readMonitors() if err != nil { return fmt.Errorf("failed to read current monitors: %w", err) }
resolved := resolveProfileMonitors(profile.Monitors, currentMonitors) if len(resolved) == 0 { return fmt.Errorf("no monitors from profile %q are currently connected", name) }
saveRollback(resolved)
if err := applyMonitors(resolved); err != nil { return fmt.Errorf("failed to apply profile: %w", err) }
return nil}
// compareMonitorConfigurations compares two monitor configurations for equality.// Uses HardwareID as the primary key when available, falling back to Name for// backward compatibility with legacy profiles.func compareMonitorConfigurations(current, saved []Monitor) bool { if len(current) != len(saved) { return false }
// Build lookup from current monitors currentByHWID := make(map[string]Monitor) currentByName := make(map[string]Monitor) for _, m := range current { if m.HardwareID != "" { currentByHWID[m.HardwareID] = m } currentByName[m.Name] = m }
// Try to match each saved monitor to a current monitor matched := make(map[string]bool) // track which current monitors have been matched for _, savedMon := range saved { var currentMon Monitor var found bool var matchedKey string
if savedMon.HardwareID != "" { currentMon, found = currentByHWID[savedMon.HardwareID] if found { matchedKey = currentMon.HardwareID } } else { // Fallback to Name only for legacy profiles (no HardwareID) currentMon, found = currentByName[savedMon.Name] if found { matchedKey = currentMon.Name } }
if !found { return false }
if matched[matchedKey] { return false // Already matched this monitor to a different saved entry } matched[matchedKey] = true
// Compare configuration properties if currentMon.PxW != savedMon.PxW || currentMon.PxH != savedMon.PxH || currentMon.X != savedMon.X || currentMon.Y != savedMon.Y || currentMon.Active != savedMon.Active { return false } }
return true}
// migrateProfileIfNeeded checks if a profile needs HardwareID migration// and performs it using currently connected monitors. Saves the migrated profile.func migrateProfileIfNeeded(profile *Profile, currentMonitors []Monitor) bool { if !needsMigration(profile.Monitors) { return false }
profile.Monitors = migrateProfileMonitors(profile.Monitors, currentMonitors) profile.UpdatedAt = time.Now()
// Best-effort save - don't fail if we can't persist _ = saveProfile(profile.Name, profile.Monitors) return true}
// getCurrentActiveProfile returns the name of the currently active profile by comparing// the current monitor configuration with all saved profilesfunc getCurrentActiveProfile() (string, error) { // Get current monitor configuration currentMonitors, err := readMonitors() if err != nil { return "", fmt.Errorf("failed to read current monitors: %w", err) }
// Get list of all profiles profiles, err := listProfiles() if err != nil { return "", fmt.Errorf("failed to list profiles: %w", err) }
// Compare current configuration with each profile for _, profileName := range profiles { profile, err := loadProfile(profileName) if err != nil { continue // Skip profiles that can't be loaded }
// Migrate legacy profiles on access migrateProfileIfNeeded(profile, currentMonitors)
if compareMonitorConfigurations(currentMonitors, profile.Monitors) { return profileName, nil } }
return "", nil // No matching profile found}
type profileMenuModel struct { profiles []string selected int err error confirmDelete bool deleteCandidate string renaming bool renameCandidate string renameInput string renameCursor int profileOrder []string // Keep track of custom order showHelp bool launchFullUI bool // Flag to indicate launching full UI termWidth int // Terminal width for responsive layout termHeight int // Terminal height}
func initialProfileMenu() (profileMenuModel, error) { profiles, err := listProfiles() if err != nil { return profileMenuModel{err: err}, nil }
// Load saved order savedOrder, _ := loadProfileOrder()
// Apply saved order if it exists if len(savedOrder) > 0 { orderedProfiles := []string{} remainingProfiles := make(map[string]bool)
// Mark all profiles as remaining for _, p := range profiles { remainingProfiles[p] = true }
// Add profiles in saved order for _, name := range savedOrder { if remainingProfiles[name] { orderedProfiles = append(orderedProfiles, name) delete(remainingProfiles, name) } }
// Add any new profiles not in saved order for _, p := range profiles { if remainingProfiles[p] { orderedProfiles = append(orderedProfiles, p) } }
profiles = orderedProfiles }
// Store the profile order (without UI elements) profileOrder := make([]string, len(profiles)) copy(profileOrder, profiles)
// Add options at the end profiles = append(profiles, "──────────────────") profiles = append(profiles, "[ Open Full UI ]")
// Try to get terminal size width, height, err := term.GetSize(int(os.Stdout.Fd())) if err != nil { // Fall back to defaults if we can't get terminal size width = 80 height = 24 }
return profileMenuModel{ profiles: profiles, selected: 0, profileOrder: profileOrder, termWidth: width, termHeight: height, }, nil}
func (m profileMenuModel) Init() tea.Cmd { return tea.WindowSize()}
func (m profileMenuModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case tea.WindowSizeMsg: m.termWidth = msg.Width m.termHeight = msg.Height return m, nil
case tea.KeyMsg: // Handle help screen first if m.showHelp { // Any key closes help m.showHelp = false return m, nil }
// Handle rename input if m.renaming { switch msg.String() { case "enter": // Apply rename newName := strings.TrimSpace(m.renameInput) if newName != "" && newName != m.renameCandidate { if err := renameProfile(m.renameCandidate, newName); err != nil { m.err = err } else { // Update profile order with new name for i, p := range m.profileOrder { if p == m.renameCandidate { m.profileOrder[i] = newName break } } _ = saveProfileOrder(m.profileOrder)
// Rebuild profiles list maintaining order profiles := make([]string, len(m.profileOrder)) copy(profiles, m.profileOrder) profiles = append(profiles, "──────────────────") profiles = append(profiles, "[ Open Full UI ]") m.profiles = profiles
// Try to keep selection on renamed item for i, p := range m.profiles { if p == newName { m.selected = i break } } } } m.renaming = false m.renameCandidate = "" m.renameInput = "" m.renameCursor = 0 return m, nil
case "esc": // Cancel rename m.renaming = false m.renameCandidate = "" m.renameInput = "" m.renameCursor = 0 return m, nil
case "backspace": if m.renameCursor > 0 { m.renameInput = m.renameInput[:m.renameCursor-1] + m.renameInput[m.renameCursor:] m.renameCursor-- }
case "left": if m.renameCursor > 0 { m.renameCursor-- }
case "right": if m.renameCursor < len(m.renameInput) { m.renameCursor++ }
case "home": m.renameCursor = 0
case "end": m.renameCursor = len(m.renameInput)
default: // Add character at cursor position if len(msg.String()) == 1 && msg.String()[0] >= 32 && msg.String()[0] < 127 { m.renameInput = m.renameInput[:m.renameCursor] + msg.String() + m.renameInput[m.renameCursor:] m.renameCursor++ } } return m, nil }
// Handle delete confirmation if m.confirmDelete { switch msg.String() { case "y", "Y": // Confirmed - delete the profile if err := deleteProfile(m.deleteCandidate); err != nil { m.err = err } else { // Remove from profile order newOrder := []string{} for _, p := range m.profileOrder { if p != m.deleteCandidate { newOrder = append(newOrder, p) } } m.profileOrder = newOrder _ = saveProfileOrder(m.profileOrder)
// Rebuild profiles list maintaining order profiles := make([]string, len(m.profileOrder)) copy(profiles, m.profileOrder) profiles = append(profiles, "──────────────────") profiles = append(profiles, "[ Open Full UI ]") m.profiles = profiles if m.selected >= len(m.profiles) { m.selected = len(m.profiles) - 1 } } m.confirmDelete = false m.deleteCandidate = "" return m, nil
case "n", "N", "esc": // Cancelled m.confirmDelete = false m.deleteCandidate = "" return m, nil
default: // Ignore other keys during confirmation return m, nil } }
switch msg.String() { case "q", "ctrl+c", "esc": return m, tea.Quit
case "shift+up": // Reorder profile up if m.selected > 0 && m.selected < len(m.profiles)-2 && !strings.HasPrefix(m.profiles[m.selected], "─") { // Swap in display list m.profiles[m.selected], m.profiles[m.selected-1] = m.profiles[m.selected-1], m.profiles[m.selected]
// Update profile order and save m.profileOrder = make([]string, 0) for _, p := range m.profiles { if !strings.HasPrefix(p, "─") && p != "[ Open Full UI ]" { m.profileOrder = append(m.profileOrder, p) } }
// Save the new order if err := saveProfileOrder(m.profileOrder); err != nil { m.err = err }
m.selected-- }
case "shift+down": // Reorder profile down if m.selected < len(m.profiles)-3 && !strings.HasPrefix(m.profiles[m.selected], "─") && !strings.HasPrefix(m.profiles[m.selected+1], "─") { // Swap in display list m.profiles[m.selected], m.profiles[m.selected+1] = m.profiles[m.selected+1], m.profiles[m.selected]
// Update profile order and save m.profileOrder = make([]string, 0) for _, p := range m.profiles { if !strings.HasPrefix(p, "─") && p != "[ Open Full UI ]" { m.profileOrder = append(m.profileOrder, p) } }
// Save the new order if err := saveProfileOrder(m.profileOrder); err != nil { m.err = err }
m.selected++ }
case "up", "k": if m.selected > 0 { m.selected-- // Skip separator if strings.HasPrefix(m.profiles[m.selected], "─") && m.selected > 0 { m.selected-- } }
case "down", "j": if m.selected < len(m.profiles)-1 { m.selected++ // Skip separator if strings.HasPrefix(m.profiles[m.selected], "─") && m.selected < len(m.profiles)-1 { m.selected++ } }
case "enter": selectedProfile := m.profiles[m.selected]
if selectedProfile == "[ Open Full UI ]" { m.launchFullUI = true return m, tea.Quit } else if strings.HasPrefix(selectedProfile, "─") { // Separator line, do nothing return m, nil } else { err := applyProfile(selectedProfile) if err != nil { m.err = err return m, nil } fmt.Printf("Applied profile: %s\n", selectedProfile) return m, tea.Quit }
case "d", "D": // Don't allow deleting the separator or UI option if m.selected < len(m.profiles)-2 && !strings.HasPrefix(m.profiles[m.selected], "─") { m.deleteCandidate = m.profiles[m.selected] m.confirmDelete = true }
case "r", "R": // Don't allow renaming the separator or UI option if m.selected < len(m.profiles)-2 && !strings.HasPrefix(m.profiles[m.selected], "─") { m.renameCandidate = m.profiles[m.selected] m.renameInput = m.renameCandidate m.renameCursor = len(m.renameInput) m.renaming = true }
case "?": m.showHelp = true return m, nil } }
return m, nil}
func (m profileMenuModel) renderHelp() string { helpStyle := lipgloss.NewStyle(). Padding(2, 4). Border(lipgloss.RoundedBorder()). BorderForeground(lipgloss.Color("214"))
titleStyle := lipgloss.NewStyle(). Bold(true). Foreground(lipgloss.Color("214")). MarginBottom(1)
sectionStyle := lipgloss.NewStyle(). Bold(true). Foreground(lipgloss.Color("42")). MarginTop(1). MarginBottom(1)
keyStyle := lipgloss.NewStyle(). Foreground(lipgloss.Color("214")). Width(20)
var content strings.Builder
// Title and version content.WriteString(titleStyle.Render(fmt.Sprintf("nirimon Profiles %s", ShortVersion()))) content.WriteString("\n") content.WriteString("Copyright © 2025 Eran Sandler\n\n") content.WriteString("Profile management for saved monitor configurations.\n")
// Navigation content.WriteString(sectionStyle.Render("\nNavigation:")) content.WriteString("\n")
navigation := []struct { key string desc string }{ {"↑/↓ or k/j", "Move selection up/down"}, {"Shift+↑/↓", "Reorder profile position"}, {"Enter", "Apply selected profile"}, {"?", "Show this help"}, {"Q / Esc / Ctrl+C", "Exit"}, }
for _, n := range navigation { content.WriteString(fmt.Sprintf("%s %s\n", keyStyle.Render(n.key), n.desc)) }
// Profile Management content.WriteString(sectionStyle.Render("\nProfile Management:")) content.WriteString("\n")
management := []struct { key string desc string }{ {"R", "Rename selected profile"}, {"D", "Delete selected profile (with confirmation)"}, }
for _, m := range management { content.WriteString(fmt.Sprintf("%s %s\n", keyStyle.Render(m.key), m.desc)) }
// About Profiles content.WriteString(sectionStyle.Render("\nAbout Profiles:")) content.WriteString("\n") content.WriteString("• Profiles save your complete monitor configuration\n") content.WriteString("• Includes position, resolution, refresh rate, and scale\n") content.WriteString("• Profiles are stored in ~/.config/nirimon/profiles/\n") content.WriteString("• Custom ordering is preserved between sessions\n") content.WriteString("• Use 'nirimon -profile NAME' to apply directly from CLI\n")
// Menu Options content.WriteString(sectionStyle.Render("\nMenu Options:")) content.WriteString("\n") content.WriteString("• Profile names: Your saved monitor configurations\n") content.WriteString("• [Open Full UI]: Launch the main nirimon interface\n")
content.WriteString("\n") content.WriteString(lipgloss.NewStyle().Foreground(lipgloss.Color("241")).Render("Press any key to close help"))
return helpStyle.Render(content.String())}
func (m profileMenuModel) View() string { // Show help if active if m.showHelp { return m.renderHelp() }
var s strings.Builder
titleStyle := lipgloss.NewStyle(). Bold(true). Foreground(lipgloss.Color("12")). MarginBottom(1)
s.WriteString(titleStyle.Render("nirimon - Profile Selection")) s.WriteString("\n\n")
if m.err != nil { errStyle := lipgloss.NewStyle(). Foreground(lipgloss.Color("9")) s.WriteString(errStyle.Render(fmt.Sprintf("Error: %v", m.err))) s.WriteString("\n\n") }
// Show rename input if active if m.renaming { renameStyle := lipgloss.NewStyle(). Border(lipgloss.RoundedBorder()). BorderForeground(lipgloss.Color("42")). Padding(1, 2). MarginTop(1). MarginBottom(1)
inputStyle := lipgloss.NewStyle(). Border(lipgloss.NormalBorder()). BorderForeground(lipgloss.Color("214")). Padding(0, 1)
// Build input with cursor inputDisplay := m.renameInput[:m.renameCursor] + "│" + m.renameInput[m.renameCursor:]
renamePrompt := fmt.Sprintf("Rename profile '%s':\n\n%s\n\nPress Enter to save, Esc to cancel", m.renameCandidate, inputStyle.Render(inputDisplay)) s.WriteString(renameStyle.Render(renamePrompt)) s.WriteString("\n") return s.String() }
// Show delete confirmation dialog if active if m.confirmDelete { confirmStyle := lipgloss.NewStyle(). Border(lipgloss.RoundedBorder()). BorderForeground(lipgloss.Color("9")). Padding(1, 2). MarginTop(1). MarginBottom(1)
confirmPrompt := fmt.Sprintf("Delete profile '%s'?\n\nPress Y to confirm, N to cancel", m.deleteCandidate) s.WriteString(confirmStyle.Render(confirmPrompt)) s.WriteString("\n") return s.String() }
// Get the currently active profile for display activeProfile, _ := getCurrentActiveProfile()
itemStyle := lipgloss.NewStyle(). PaddingLeft(2)
selectedStyle := lipgloss.NewStyle(). PaddingLeft(1). Foreground(lipgloss.Color("214")). Bold(true)
for i, profile := range m.profiles { if strings.HasPrefix(profile, "─") { // Render separator sepStyle := lipgloss.NewStyle(). Foreground(lipgloss.Color("238")) s.WriteString(sepStyle.Render(profile)) } else if i == m.selected { displayName := profile if profile == activeProfile && profile != "[ Open Full UI ]" { displayName = profile + " *" } s.WriteString(selectedStyle.Render("▶ " + displayName)) } else { displayName := profile if profile == activeProfile && profile != "[ Open Full UI ]" { displayName = profile + " *" } s.WriteString(itemStyle.Render(" " + displayName)) } s.WriteString("\n") }
s.WriteString("\n")
// Render responsive footer s.WriteString(m.renderFooter())
return s.String()}
// profileKeyCommand represents a keyboard command with different verbosity levels for profile menutype profileKeyCommand struct { full string medium string short string priority int // 1 = essential, 2 = important, 3 = nice to have}
func (m profileMenuModel) renderFooter() string { commands := []profileKeyCommand{ {"↑/↓ Navigate", "↑/↓ Nav", "↑↓", 1}, {"Shift+↑/↓ Reorder", "S+↑/↓ Order", "S+↑↓", 2}, {"Enter Select", "Enter Sel", "⏎", 1}, {"R Rename", "R Rename", "R", 2}, {"D Delete", "D Delete", "D", 2}, {"? Help", "? Help", "?", 1}, {"Q Quit", "Q Quit", "Q", 1}, }
// Determine format based on terminal width var keys []string separator := " • "
width := m.termWidth if width <= 0 { width = 80 // Fallback }
if width < 60 { // Very narrow: only essential commands, shortest form separator = " " for _, cmd := range commands { if cmd.priority == 1 { keys = append(keys, cmd.short) } } } else if width < 80 { // Narrow: essential and important, short form separator = " • " for _, cmd := range commands { if cmd.priority <= 2 { keys = append(keys, cmd.short) } } } else if width < 100 { // Medium: all commands, medium form separator = " • " for _, cmd := range commands { keys = append(keys, cmd.medium) } } else { // Wide: all commands, full form for _, cmd := range commands { keys = append(keys, cmd.full) } }
// Check if the footer would overflow and switch to multi-line if needed footerText := strings.Join(keys, separator) if len(footerText) > width-4 && width >= 60 { // Try multi-line layout for medium and wider terminals return m.renderMultiLineFooter(commands, width, keys, separator) }
helpStyle := lipgloss.NewStyle(). Foreground(lipgloss.Color("241"))
return helpStyle.Render(footerText)}
func (m profileMenuModel) renderMultiLineFooter(commands []profileKeyCommand, width int, singleLineKeys []string, separator string) string { var lines []string var currentLine []string var currentLength int
sepLen := len(separator)
// Helper function to add text to current line or start new line addToLine := func(text string) { textLen := len(text) wouldOverflow := len(currentLine) > 0 && currentLength+sepLen+textLen > width-4
if wouldOverflow && len(lines) < 3 { // Start new line if we haven't reached 3 lines yet lines = append(lines, strings.Join(currentLine, separator)) currentLine = []string{text} currentLength = textLen } else if !wouldOverflow { // Add to current line currentLine = append(currentLine, text) if len(currentLine) > 1 { currentLength += sepLen } currentLength += textLen } // If we would overflow and already have 3 lines, skip this item }
// First, try with the keys we already have (based on width) for _, key := range singleLineKeys { addToLine(key) }
// Add remaining line if exists if len(currentLine) > 0 { lines = append(lines, strings.Join(currentLine, separator)) }
// If we're using more than 3 lines, we need to be more selective if len(lines) > 3 { lines = []string{} currentLine = []string{} currentLength = 0
// Use progressively shorter forms until it fits in 3 lines attempts := []struct { priority int format string }{ {3, "medium"}, // All commands, medium form {3, "short"}, // All commands, short form {2, "short"}, // Important and essential only, short form {1, "short"}, // Essential only, short form }
for _, attempt := range attempts { lines = []string{} currentLine = []string{} currentLength = 0
for _, cmd := range commands { if cmd.priority <= attempt.priority { var text string switch attempt.format { case "full": text = cmd.full case "medium": text = cmd.medium case "short": text = cmd.short } addToLine(text) } }
if len(currentLine) > 0 { lines = append(lines, strings.Join(currentLine, separator)) }
// If it fits in 3 lines, we're done if len(lines) <= 3 { break } } }
helpStyle := lipgloss.NewStyle(). Foreground(lipgloss.Color("241"))
return helpStyle.Render(strings.Join(lines, "\n"))}