Skip to content

Commit e293c69

Browse files
committed
fix: intermittent crash
1 parent b8ee027 commit e293c69

File tree

4 files changed

+48
-25
lines changed

4 files changed

+48
-25
lines changed

cmd/root.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"runtime"
1111
"runtime/debug"
12+
"runtime/pprof"
1213
"time"
1314

1415
tea "github.com/charmbracelet/bubbletea"
@@ -22,7 +23,6 @@ import (
2223
"github.com/dlvhdr/gh-dash/v4/git"
2324
"github.com/dlvhdr/gh-dash/v4/ui"
2425
"github.com/dlvhdr/gh-dash/v4/ui/markdown"
25-
"github.com/dlvhdr/gh-dash/v4/utils"
2626
)
2727

2828
var (
@@ -55,7 +55,8 @@ func createModel(repoPath string, configPath string, debug bool) (ui.Model, *os.
5555

5656
if debug {
5757
var fileErr error
58-
newConfigFile, fileErr := os.OpenFile("debug.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
58+
newConfigFile, fileErr := os.OpenFile("debug.log",
59+
os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
5960
if fileErr == nil {
6061
log.SetOutput(newConfigFile)
6162
log.SetTimeFormat(time.Kitchen)
@@ -122,6 +123,12 @@ func init() {
122123
"passing this flag will allow writing debug output to debug.log",
123124
)
124125

126+
rootCmd.Flags().String(
127+
"cpuprofile",
128+
"",
129+
"write cpu profile to file",
130+
)
131+
125132
rootCmd.Flags().BoolP(
126133
"help",
127134
"h",
@@ -158,7 +165,18 @@ func init() {
158165
defer logger.Close()
159166
}
160167

161-
utils.InitTemplateHandler()
168+
cpuprofile, err := rootCmd.Flags().GetString("cpuprofile")
169+
if err != nil {
170+
log.Fatal("Cannot parse cpuprofile flag", err)
171+
}
172+
if cpuprofile != "" {
173+
f, err := os.Create(cpuprofile)
174+
if err != nil {
175+
log.Fatal(err)
176+
}
177+
_ = pprof.StartCPUProfile(f)
178+
defer pprof.StopCPUProfile()
179+
}
162180

163181
p := tea.NewProgram(
164182
model,

ui/components/section/section.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@ package section
33
import (
44
"bytes"
55
"fmt"
6+
"log/slog"
67
"strings"
78
"text/template"
89
"time"
910

1011
"github.com/charmbracelet/bubbles/spinner"
1112
tea "github.com/charmbracelet/bubbletea"
1213
"github.com/charmbracelet/lipgloss"
14+
"github.com/charmbracelet/log"
1315
"github.com/cli/go-gh/v2/pkg/repository"
16+
"github.com/go-sprout/sprout"
17+
timeregistry "github.com/go-sprout/sprout/registry/time"
1418

1519
"github.com/dlvhdr/gh-dash/v4/config"
1620
"github.com/dlvhdr/gh-dash/v4/data"
@@ -212,11 +216,13 @@ func (m *BaseModel) GetSearchValue() string {
212216
var searchValueWithoutCurrentCloneFilter []string
213217
for _, token := range strings.Fields(searchValue) {
214218
if !strings.HasPrefix(token, currentCloneFilter) {
215-
searchValueWithoutCurrentCloneFilter = append(searchValueWithoutCurrentCloneFilter, token)
219+
searchValueWithoutCurrentCloneFilter =
220+
append(searchValueWithoutCurrentCloneFilter, token)
216221
}
217222
}
218223
if m.IsFilteredByCurrentRemote {
219-
return fmt.Sprintf("%s %s", currentCloneFilter, strings.Join(searchValueWithoutCurrentCloneFilter, " "))
224+
return fmt.Sprintf("%s %s", currentCloneFilter,
225+
strings.Join(searchValueWithoutCurrentCloneFilter, " "))
220226
}
221227
return strings.Join(searchValueWithoutCurrentCloneFilter, " ")
222228
}
@@ -226,9 +232,13 @@ func (m *BaseModel) enrichSearchWithTemplateVars() string {
226232
searchVars := struct{ Now time.Time }{
227233
Now: time.Now(),
228234
}
229-
handler := utils.TemplateHandler()
230-
tmpl, err := template.New("search").Funcs(handler.Build()).Parse(searchValue)
235+
sl := slog.New(log.Default())
236+
handler := sprout.New(sprout.WithRegistries(timeregistry.NewRegistry(), utils.NewRegistry()), sprout.WithLogger(sl))
237+
funcs := handler.Build()
238+
239+
tmpl, err := template.New("search").Funcs(funcs).Parse(searchValue)
231240
if err != nil {
241+
log.Error("bad template", "err", err)
232242
return searchValue
233243
}
234244
var buf bytes.Buffer

ui/ui.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
173173
log.Debug("Key pressed", "key", msg.String())
174174
m.ctx.Error = nil
175175

176-
if currSection != nil && (currSection.IsSearchFocused() || currSection.IsPromptConfirmationFocused()) {
176+
if currSection != nil && (currSection.IsSearchFocused() ||
177+
currSection.IsPromptConfirmationFocused()) {
177178
cmd = m.updateSection(currSection.GetId(), currSection.GetType(), msg)
178179
return m, cmd
179180
}
@@ -262,9 +263,11 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
262263

263264
case key.Matches(msg, m.keys.Help):
264265
if !m.footer.ShowAll {
265-
m.ctx.MainContentHeight = m.ctx.MainContentHeight + common.FooterHeight - common.ExpandedHelpHeight
266+
m.ctx.MainContentHeight = m.ctx.MainContentHeight +
267+
common.FooterHeight - common.ExpandedHelpHeight
266268
} else {
267-
m.ctx.MainContentHeight = m.ctx.MainContentHeight + common.ExpandedHelpHeight - common.FooterHeight
269+
m.ctx.MainContentHeight = m.ctx.MainContentHeight +
270+
common.ExpandedHelpHeight - common.FooterHeight
268271
}
269272

270273
case key.Matches(msg, m.keys.CopyNumber):
@@ -349,7 +352,8 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
349352
case m.ctx.View == config.PRsView:
350353
switch {
351354

352-
case key.Matches(msg, keys.PRKeys.PrevSidebarTab), key.Matches(msg, keys.PRKeys.NextSidebarTab):
355+
case key.Matches(msg, keys.PRKeys.PrevSidebarTab),
356+
key.Matches(msg, keys.PRKeys.NextSidebarTab):
353357
var scmd tea.Cmd
354358
m.prSidebar, scmd = m.prSidebar.Update(msg)
355359
m.syncSidebar()
@@ -518,6 +522,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
518522
m.sidebar.IsOpen = msg.Config.Defaults.Preview.Open
519523
m.tabs.UpdateSectionsConfigs(m.ctx)
520524
m.syncMainContentWidth()
525+
521526
newSections, fetchSectionsCmds := m.fetchAllViewSections()
522527
m.setCurrentViewSections(newSections)
523528
cmds = append(cmds, fetchSectionsCmds, fetchUser, m.doRefreshAtInterval(), m.doUpdateFooterAtInterval())
@@ -657,7 +662,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
657662

658663
func (m Model) View() string {
659664
if m.ctx.Config == nil {
660-
return "Reading config...\n"
665+
return lipgloss.Place(m.ctx.ScreenWidth, m.ctx.ScreenHeight, lipgloss.Center, lipgloss.Center, "Reading config...")
661666
}
662667

663668
s := strings.Builder{}

utils/templateHandler.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ import (
55
"strings"
66
"time"
77

8+
"github.com/charmbracelet/log"
89
"github.com/go-sprout/sprout"
9-
timeregistry "github.com/go-sprout/sprout/registry/time"
1010
)
1111

12-
var handler *sprout.DefaultHandler
13-
1412
type TemplateRegistry struct {
1513
handler sprout.Handler
1614
}
@@ -32,9 +30,11 @@ func (or *TemplateRegistry) NowModify(input string) (string, error) {
3230
now := time.Now()
3331
duration, err := ParseDuration(input)
3432
if err != nil {
33+
log.Error("failed parsing duration", "input", input)
3534
return "", err
3635
}
3736

37+
log.Info("parsed duration", "duration", duration)
3838
return now.Add(duration).Format("2006-01-02"), nil
3939
}
4040

@@ -47,16 +47,6 @@ func (or *TemplateRegistry) RegisterAliases(aliasMap sprout.FunctionAliasMap) er
4747
return nil
4848
}
4949

50-
func InitTemplateHandler() {
51-
handler = sprout.New(sprout.WithRegistries(timeregistry.NewRegistry()))
52-
tr := TemplateRegistry{handler: handler}
53-
handler.AddRegistries(&tr)
54-
}
55-
56-
func TemplateHandler() *sprout.DefaultHandler {
57-
return handler
58-
}
59-
6050
// ParseDuration parses a duration string.
6151
// examples: "10d", "-1.5w" or "3Y4M5d".
6252
// Add time units are "d"="D", "w"="W", "mo=M", "y"="Y".

0 commit comments

Comments
 (0)