From 101e23780e5f8bc97f5683f5901fbd31f56ce29e Mon Sep 17 00:00:00 2001 From: Benjamin Chausse Date: Sat, 28 Dec 2024 00:46:18 -0500 Subject: First attempt at auto-resize --- internal/switcher/switcher.go | 47 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) (limited to 'internal/switcher/switcher.go') diff --git a/internal/switcher/switcher.go b/internal/switcher/switcher.go index 8de31c6..cf16b6a 100644 --- a/internal/switcher/switcher.go +++ b/internal/switcher/switcher.go @@ -32,6 +32,7 @@ type Model struct { input textinput.Model notices notices.Model fullHelp bool // When false, only show help for the switcher (not children) + h, w int } func New() Model { @@ -101,6 +102,9 @@ func (m Model) Init() tea.Cmd { } func (m Model) View() string { + pickerStr := m.pickers[m.active].View() + w := lipgloss.Width(pickerStr) + tabs := make([]string, len(m.pickers)) for i, p := range m.pickers { if i == m.active { @@ -115,9 +119,6 @@ func (m Model) View() string { ui.Style().TabGeom.Render(ui.TabSepRight), }, " ") - pickerStr := m.pickers[m.active].View() - w := lipgloss.Width(pickerStr) - m.preview.SetWidth(w) previewStr := m.preview.View() @@ -147,13 +148,27 @@ func (m Model) View() string { helpStr, }, "\n")) - return strings.Join( + viewStr := strings.Join( []string{ tabStr, mainArea, inputStr, m.notices.View(), }, "\n") + + // if lipgloss.Width(viewStr) > m.w { + // return ui.Style().TooSmall. + // Align(lipgloss.Center). + // Width(m.w). + // Height(m.h). + // Render("Terminal too small!\nPlease increase terminal size...") + // } + + return lipgloss.NewStyle(). + // Align(lipgloss.Center). + // Width(m.w). + // Height(m.h). + Render(viewStr) } func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { @@ -166,6 +181,30 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.notices = newNotices.(notices.Model) cmds = append(cmds, cmd) + case tea.WindowSizeMsg: + m.w = msg.Width + m.h = msg.Height + for i, p := range m.pickers { + var cmd tea.Cmd + var model tea.Model + model, cmd = p.Update(msg) + m.pickers[i] = model.(picker.Model) + cmds = append(cmds, cmd) + } + // iterate over every model contained in the model using a struct + // assign the new model to the model and append the command to the commands + // return the model and the batched commands + newInput, cmd := m.input.Update(msg) + m.input = newInput + cmds = append(cmds, cmd) + newPreview, cmd := m.preview.Update(msg) + m.preview = newPreview.(preview.Model) + cmds = append(cmds, cmd) + newNotices, cmd := m.notices.Update(msg) + m.notices = newNotices.(notices.Model) + cmds = append(cmds, cmd) + return m, tea.Batch(cmds...) + case tea.KeyMsg: if m.input.Focused() && msg.String() != "ctrl+c" { -- cgit v1.2.3