Skip to content

Commit 1c08157

Browse files
Copilotcanack
andcommitted
Fix explicit panic calls with graceful error handling
Co-authored-by: canack <[email protected]>
1 parent fd90c62 commit 1c08157

File tree

4 files changed

+57
-18
lines changed

4 files changed

+57
-18
lines changed

internal/terminal/handler/ghworkflowhistory.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/charmbracelet/bubbles/table"
1212
tea "github.com/charmbracelet/bubbletea"
1313
"github.com/charmbracelet/lipgloss"
14-
"github.com/termkit/gama/internal/config"
1514
gu "github.com/termkit/gama/internal/github/usecase"
1615
"github.com/termkit/gama/pkg/browser"
1716
"github.com/termkit/skeleton"
@@ -63,10 +62,7 @@ type workflowHistoryUpdateMsg struct {
6362
// -----------------------------------------------------------------------------
6463

6564
func SetupModelGithubWorkflowHistory(s *skeleton.Skeleton, githubUseCase gu.UseCase) *ModelGithubWorkflowHistory {
66-
cfg, err := config.LoadConfig()
67-
if err != nil {
68-
panic(fmt.Sprintf("failed to load config: %v", err))
69-
}
65+
cfg := loadConfig()
7066

7167
modelStatus := SetupModelStatus(s)
7268
tabOptions := NewOptions(s, modelStatus)

internal/terminal/handler/handler.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package handler
22

33
import (
4-
"fmt"
54
tea "github.com/charmbracelet/bubbletea"
65
"github.com/termkit/gama/internal/config"
76
gu "github.com/termkit/gama/internal/github/usecase"
@@ -12,7 +11,14 @@ import (
1211
func SetupTerminal(githubUseCase gu.UseCase, version pkgversion.Version) tea.Model {
1312
cfg, err := config.LoadConfig()
1413
if err != nil {
15-
panic(fmt.Sprintf("failed to load config: %v", err))
14+
// Return a skeleton with minimal error handling
15+
// Instead of panicking, we'll create a basic working terminal
16+
cfg = &config.Config{}
17+
// Apply defaults manually
18+
cfg.Shortcuts.SwitchTabRight = "shift+right"
19+
cfg.Shortcuts.SwitchTabLeft = "shift+left"
20+
cfg.Shortcuts.Quit = "ctrl+c"
21+
cfg.Settings.LiveMode.Enabled = false
1622
}
1723

1824
s := skeleton.NewSkeleton()
@@ -37,6 +43,8 @@ func SetupTerminal(githubUseCase gu.UseCase, version pkgversion.Version) tea.Mod
3743
s.SetTerminalViewportWidth(MinTerminalWidth)
3844
s.SetTerminalViewportHeight(MinTerminalHeight)
3945

46+
handlerKeys := createHandlerKeys(cfg)
47+
4048
s.KeyMap.SetKeyNextTab(handlerKeys.SwitchTabRight)
4149
s.KeyMap.SetKeyPrevTab(handlerKeys.SwitchTabLeft)
4250
s.KeyMap.SetKeyQuit(handlerKeys.Quit)

internal/terminal/handler/keymap.go

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,65 @@ package handler
22

33
import (
44
"fmt"
5+
"time"
56

67
"github.com/termkit/gama/internal/config"
78

89
teakey "github.com/charmbracelet/bubbles/key"
910
)
1011

12+
// ---------------------------------------------------------------------------
13+
14+
type handlerKeyMap struct {
15+
SwitchTabRight teakey.Binding
16+
SwitchTabLeft teakey.Binding
17+
Quit teakey.Binding
18+
}
19+
1120
func loadConfig() *config.Config {
1221
cfg, err := config.LoadConfig()
1322
if err != nil {
14-
panic(fmt.Sprintf("failed to load config: %v", err))
23+
// Return a config with default values instead of panicking
24+
cfg = &config.Config{}
25+
cfg = fillDefaultShortcuts(cfg)
26+
cfg = fillDefaultSettings(cfg)
1527
}
1628
return cfg
1729
}
1830

19-
// ---------------------------------------------------------------------------
20-
21-
type handlerKeyMap struct {
22-
SwitchTabRight teakey.Binding
23-
SwitchTabLeft teakey.Binding
24-
Quit teakey.Binding
31+
func fillDefaultShortcuts(cfg *config.Config) *config.Config {
32+
if cfg.Shortcuts.SwitchTabRight == "" {
33+
cfg.Shortcuts.SwitchTabRight = "shift+right"
34+
}
35+
if cfg.Shortcuts.SwitchTabLeft == "" {
36+
cfg.Shortcuts.SwitchTabLeft = "shift+left"
37+
}
38+
if cfg.Shortcuts.Quit == "" {
39+
cfg.Shortcuts.Quit = "ctrl+c"
40+
}
41+
if cfg.Shortcuts.Refresh == "" {
42+
cfg.Shortcuts.Refresh = "ctrl+r"
43+
}
44+
if cfg.Shortcuts.Enter == "" {
45+
cfg.Shortcuts.Enter = "enter"
46+
}
47+
if cfg.Shortcuts.Tab == "" {
48+
cfg.Shortcuts.Tab = "tab"
49+
}
50+
if cfg.Shortcuts.LiveMode == "" {
51+
cfg.Shortcuts.LiveMode = "ctrl+l"
52+
}
53+
return cfg
2554
}
2655

27-
var handlerKeys = func() handlerKeyMap {
28-
cfg := loadConfig()
56+
func fillDefaultSettings(cfg *config.Config) *config.Config {
57+
if cfg.Settings.LiveMode.Interval == 0 {
58+
cfg.Settings.LiveMode.Interval = 15 * time.Second
59+
}
60+
return cfg
61+
}
2962

63+
func createHandlerKeys(cfg *config.Config) handlerKeyMap {
3064
return handlerKeyMap{
3165
SwitchTabRight: teakey.NewBinding(
3266
teakey.WithKeys(cfg.Shortcuts.SwitchTabRight),
@@ -38,7 +72,7 @@ var handlerKeys = func() handlerKeyMap {
3872
teakey.WithKeys(cfg.Shortcuts.Quit),
3973
),
4074
}
41-
}()
75+
}
4276

4377
// ---------------------------------------------------------------------------
4478

main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ var Version = "under development" // will be set by build flag
2323
func main() {
2424
cfg, err := config.LoadConfig()
2525
if err != nil {
26-
panic(fmt.Sprintf("failed to load config: %v", err))
26+
fmt.Fprintf(os.Stderr, "Error: failed to load config: %v\n", err)
27+
os.Exit(1)
2728
}
2829

2930
version := pkgversion.New(repositoryOwner, repositoryName, Version)

0 commit comments

Comments
 (0)