Skip to content

Commit

Permalink
fix nil incident handling, incident rendering; adds errMsg help
Browse files Browse the repository at this point in the history
* Fixes panic when trying to render incidents without an incident selected
* Fixes broken incident rendering
* Adds custom errMsg help with correct errMsg keymap

Signed-off-by: Chris Collins <collins.christopher@gmail.com>
  • Loading branch information
clcollins committed Mar 13, 2024
1 parent 8aecc25 commit b4cea97
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 21 deletions.
20 changes: 12 additions & 8 deletions pkg/tui/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"slices"

"github.com/PagerDuty/go-pagerduty"
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/clcollins/srepd/pkg/pd"
)
Expand Down Expand Up @@ -459,14 +458,19 @@ func acknowledged(a []pagerduty.Acknowledgement) string {
return dot
}

func doIfIncidentSelected(table table.Model, cmd tea.Cmd) tea.Cmd {
return func() tea.Msg {
if table.SelectedRow() == nil {
return errMsg{errors.New("no incident selected")}
}
func doIfIncidentSelected(m *model, cmd tea.Cmd) tea.Cmd {
debug("doIfIncidentSelected()")
if m.table.SelectedRow() == nil {
debug("doIfIncidentSelected(): selected row is nil")
m.setStatus(nilIncidentErr)
m.viewingIncident = false
return tea.Sequence(
func() tea.Msg { return getIncidentMsg(table.SelectedRow()[1]) },
cmd,
func() tea.Msg { return errMsg{errors.New(nilIncidentErr)} },
)
}
debug("doIfIncidentSelected(): got selected row")
return tea.Sequence(
func() tea.Msg { return getIncidentMsg(m.table.SelectedRow()[1]) },
cmd,
)
}
13 changes: 12 additions & 1 deletion pkg/tui/keymap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package tui
import "github.com/charmbracelet/bubbles/key"

func (k keymap) ShortHelp() []key.Binding {
return []key.Binding{k.Help, k.Quit}
return []key.Binding{k.Help, k.Back, k.Quit}
}

func (k keymap) FullHelp() [][]key.Binding {
Expand Down Expand Up @@ -102,3 +102,14 @@ var defaultKeyMap = keymap{
key.WithHelp("o", "open in browser"),
),
}

var errorViewKeyMap = keymap{
Quit: key.NewBinding(
key.WithKeys("q", "ctrl+c"),
key.WithHelp("q/ctrl+c", "quit"),
),
Back: key.NewBinding(
key.WithKeys("esc"),
key.WithHelp("esc", "back"),
),
}
6 changes: 5 additions & 1 deletion pkg/tui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ func (m *model) setStatus(msg string) {
log.Printf("%s\n", d)
}

func (m *model) toggleHelp() {
m.help.ShowAll = !m.help.ShowAll
}

func newTableWithStyles() table.Model {
debug("newTableWithStyles")
t := table.New(table.WithFocused(true))
Expand All @@ -108,7 +112,7 @@ func newTextInput() textinput.Model {
func newHelp() help.Model {
debug("newHelp")
h := help.New()
h.ShowAll = false
h.ShowAll = true
return h
}

Expand Down
13 changes: 5 additions & 8 deletions pkg/tui/msgHandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func switchTableFocusMode(m model, msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyMsg:
switch {
case key.Matches(msg, defaultKeyMap.Help):
m.help.ShowAll = !m.help.ShowAll
m.toggleHelp()

case key.Matches(msg, defaultKeyMap.Up):
m.table.MoveUp(1)
Expand All @@ -95,8 +95,9 @@ func switchTableFocusMode(m model, msg tea.Msg) (tea.Model, tea.Cmd) {
case key.Matches(msg, defaultKeyMap.Enter):
m.viewingIncident = true
// TODO TODAY - fix this
debug("Enter key pressed")
return m, doIfIncidentSelected(
m.table,
&m,
func() tea.Msg {
return waitForSelectedIncidentThenDoMsg{action: func() tea.Msg { return renderIncidentMsg("render") }, msg: "render"}
},
Expand Down Expand Up @@ -176,7 +177,7 @@ func switchInputFocusMode(m model, msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyMsg:
switch {
case key.Matches(msg, defaultKeyMap.Help):
m.help.ShowAll = !m.help.ShowAll
m.toggleHelp()

case key.Matches(msg, defaultKeyMap.Back):
m.input.Blur()
Expand All @@ -200,7 +201,7 @@ func switchIncidentFocusMode(m model, msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyMsg:
switch {
case key.Matches(msg, defaultKeyMap.Help):
m.help.ShowAll = !m.help.ShowAll
m.toggleHelp()

// This un-sets the selected incident and returns to the table view
case key.Matches(msg, defaultKeyMap.Back):
Expand Down Expand Up @@ -242,12 +243,8 @@ func switchErrorFocusMode(m model, msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, defaultKeyMap.Help):
m.help.ShowAll = !m.help.ShowAll

case key.Matches(msg, defaultKeyMap.Back):
m.err = nil
m.setStatus("")
}
}
return m, nil
Expand Down
6 changes: 4 additions & 2 deletions pkg/tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const (
waitTime = time.Millisecond * 1
defaultInputPrompt = " $ "
u
nilNoteErr = "incident note content is empty"
nilNoteErr = "incident note content is empty"
nilIncidentErr = "no incident selected"
)

type errMsg struct{ error }
Expand All @@ -34,6 +35,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {

case errMsg:
m.Init()
return m.errMsgHandler(msg)

case tea.WindowSizeMsg:
Expand Down Expand Up @@ -240,7 +242,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// This is a catch all for any action that requires a selected incident
//
case waitForSelectedIncidentThenDoMsg:
debug("waitForSelectedIncidentThenDoMsg: ", fmt.Sprintf("%+v, %+v", msg.action, msg.msg))
debug("waitForSelectedIncidentThenDoMsg: ", fmt.Sprintf("action: %+v, msg: %+v", msg.action, msg.msg))
if msg.action == nil {
m.setStatus("failed to perform action: no action included in msg")
return m, nil
Expand Down
4 changes: 3 additions & 1 deletion pkg/tui/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/PagerDuty/go-pagerduty"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/glamour"
Expand Down Expand Up @@ -55,7 +56,8 @@ func (m model) View() string {
switch {
case m.err != nil:
debug("error")
return (errorStyle.Render(dot+"ERROR"+dot+"\n\n"+m.err.Error()) + "\n" + helpView)
errHelpView := helpStyle.Render(help.New().View(errorViewKeyMap))
return (errorStyle.Render(dot+"ERROR"+dot+"\n\n"+m.err.Error()) + "\n" + errHelpView)

case m.viewingIncident:
debug("viewingIncident")
Expand Down

0 comments on commit b4cea97

Please sign in to comment.