Private
Public Access
1
0

Update tui error handling

- Allow each view to position error banners where they choose
- Add global 'esc' key handler to dismiss errors
This commit is contained in:
2024-12-12 07:09:58 +00:00
parent d9d1b02ef3
commit 02228d65ac
6 changed files with 40 additions and 12 deletions

View File

@@ -60,6 +60,14 @@ func (m *Model) Init() tea.Cmd {
}
func (m *Model) handleGlobalInput(msg tea.KeyMsg) tea.Cmd {
switch msg.String() {
case "esc":
if len(m.errs) > 0 {
m.errs = m.errs[1:]
return shared.KeyHandled(msg)
}
}
view, cmd := m.views[m.activeView].Update(msg)
m.views[m.activeView] = view
if cmd != nil {
@@ -108,10 +116,16 @@ func (m *Model) View() string {
errBanners := make([]string, len(m.errs))
for idx, err := range m.errs {
errBanners[idx] = tuiutil.ErrorBanner(err, m.width)
fixedUIHeight += tuiutil.Height(errBanners[idx])
}
var errors string
if len(errBanners) > 0 {
errors = lipgloss.JoinVertical(lipgloss.Left, errBanners...)
} else {
errors = ""
}
fixedUIHeight += tuiutil.Height(errors)
content := m.views[m.activeView].Content(m.width, m.height-fixedUIHeight)
content := m.views[m.activeView].Content(m.width, m.height-fixedUIHeight, errors)
sections := make([]string, 0, 4)
if header != "" {
@@ -123,9 +137,6 @@ func (m *Model) View() string {
if footer != "" {
sections = append(sections, footer)
}
for _, errBanner := range errBanners {
sections = append(sections, errBanner)
}
return lipgloss.JoinVertical(lipgloss.Left, sections...)
}