on main help sign in
refactor: simplify some code by using `cmp.Or` (#2253)
commit699c8d79c3ac44fa676c00c674e5ed8787a0bfeb authorAndrey Nering <andreynering@users.noreply.github.com> dateWed 18 Feb 2026, 16:32 (8 months ago) parent44c7535
8 files changed+18−50
internal/agent/agent.go+1−4 internal/agent/tools/glob.go+2−4 internal/agent/tools/grep.go+2−4 internal/agent/tools/list_mcp_resources.go+2−7 internal/config/load.go+4−12 internal/ui/dialog/arguments.go+2−4 internal/ui/dialog/models.go+2−7 internal/ui/model/ui.go+3−8
internal/agent/agent.go+1−4
@@ -849,10 +849,7 @@ func (a *sessionAgent) generateTitle(ctx context.Context, sessionID string, user
849849 title = thinkTagRegex.ReplaceAllString(title, "")
850850
851851 title = strings.TrimSpace(title)
852− if title == "" {
853− slog.Debug("Empty title; using fallback")
854− title = defaultSessionName
855− }
852+ title = cmp.Or(title, defaultSessionName)
856853
857854 // Calculate usage and cost.
858855 var openrouterCost *float64
internal/agent/tools/glob.go+2−4
@@ -2,6 +2,7 @@ package tools
22
33import (
44 "bytes"
5+ "cmp"
56 "context"
67 _ "embed"
78 "fmt"
@@ -39,10 +40,7 @@ func NewGlobTool(workingDir string) fantasy.AgentTool {
3940 return fantasy.NewTextErrorResponse("pattern is required"), nil
4041 }
4142
42− searchPath := params.Path
43− if searchPath == "" {
44− searchPath = workingDir
45− }
43+ searchPath := cmp.Or(params.Path, workingDir)
4644
4745 files, truncated, err := globFiles(ctx, params.Pattern, searchPath, 100)
4846 if err != nil {
internal/agent/tools/grep.go+2−4
@@ -3,6 +3,7 @@ package tools
33import (
44 "bufio"
55 "bytes"
6+ "cmp"
67 "context"
78 _ "embed"
89 "encoding/json"
@@ -115,10 +116,7 @@ func NewGrepTool(workingDir string, config config.ToolGrep) fantasy.AgentTool {
115116 searchPattern = escapeRegexPattern(params.Pattern)
116117 }
117118
118− searchPath := params.Path
119− if searchPath == "" {
120− searchPath = workingDir
121− }
119+ searchPath := cmp.Or(params.Path, workingDir)
122120
123121 searchCtx, cancel := context.WithTimeout(ctx, config.GetTimeout())
124122 defer cancel()
internal/agent/tools/list_mcp_resources.go+2−7
@@ -1,6 +1,7 @@
11package tools
22
33import (
4+ "cmp"
45 "context"
56 _ "embed"
67 "fmt"
@@ -74,13 +75,7 @@ func NewListMCPResourcesTool(cfg *config.Config, permissions permission.Service)
7475 if resource == nil {
7576 continue
7677 }
77− title := resource.Title
78− if title == "" {
79− title = resource.Name
80− }
81− if title == "" {
82− title = resource.URI
83− }
78+ title := cmp.Or(resource.Title, resource.Name, resource.URI)
8479 line := fmt.Sprintf("- %s", title)
8580 if resource.URI != "" {
8681 line = fmt.Sprintf("%s (%s)", line, resource.URI)
internal/config/load.go+4−12
@@ -278,13 +278,9 @@ func (c *Config) configureProviders(env env.Env, resolver VariableResolver, know
278278
279279 // Make sure the provider ID is set
280280 providerConfig.ID = id
281− if providerConfig.Name == "" {
282− providerConfig.Name = id // Use ID as name if not set
283− }
281+ providerConfig.Name = cmp.Or(providerConfig.Name, id) // Use ID as name if not set
284282 // default to OpenAI if not set
285− if providerConfig.Type == "" {
286− providerConfig.Type = catwalk.TypeOpenAICompat
287− }
283+ providerConfig.Type = cmp.Or(providerConfig.Type, catwalk.TypeOpenAICompat)
288284 if !slices.Contains(catwalk.KnownProviderTypes(), providerConfig.Type) && providerConfig.Type != hyper.Name {
289285 slog.Warn("Skipping custom provider due to unsupported provider type", "provider", id)
290286 c.Providers.Del(id)
@@ -412,9 +408,7 @@ func (c *Config) setDefaults(workingDir, dataDir string) {
412408 c.Options.Attribution.TrailerStyle = TrailerStyleAssistedBy
413409 }
414410 }
415− if c.Options.InitializeAs == "" {
416− c.Options.InitializeAs = defaultInitializeAs
417− }
411+ c.Options.InitializeAs = cmp.Or(c.Options.InitializeAs, defaultInitializeAs)
418412}
419413
420414// applyLSPDefaults applies default values from powernap to LSP configurations
@@ -445,9 +439,7 @@ func (c *Config) applyLSPDefaults() {
445439 if len(cfg.RootMarkers) == 0 {
446440 cfg.RootMarkers = base.RootMarkers
447441 }
448− if cfg.Command == "" {
449− cfg.Command = base.Command
450− }
442+ cfg.Command = cmp.Or(cfg.Command, base.Command)
451443 if len(cfg.Args) == 0 {
452444 cfg.Args = base.Args
453445 }
internal/ui/dialog/arguments.go+2−4
@@ -1,6 +1,7 @@
11package dialog
22
33import (
4+ "cmp"
45 "strings"
56
67 "charm.land/bubbles/v2/help"
@@ -311,10 +312,7 @@ func (a *Arguments) Draw(scr uv.Screen, area uv.Rectangle) *tea.Cursor {
311312 // Use standard header
312313 titleStyle := s.Dialog.Title
313314
314− titleText := a.title
315− if titleText == "" {
316− titleText = "Arguments"
317− }
315+ titleText := cmp.Or(a.title, "Arguments")
318316
319317 header := common.DialogTitle(s, titleText, width, s.Primary, s.Secondary)
320318
internal/ui/dialog/models.go+2−7
@@ -445,18 +445,13 @@ func (m *Models) setProviderItems() error {
445445 }
446446 continue
447447 }
448− if model.Name == "" {
449− model.Name = model.ID
450− }
448+ model.Name = cmp.Or(model.Name, model.ID)
451449 displayProvider.Models = append(displayProvider.Models, model)
452450 modelIndex[model.ID] = len(displayProvider.Models) - 1
453451 }
454452 }
455453
456− name := displayProvider.Name
457− if name == "" {
458− name = providerID
459− }
454+ name := cmp.Or(displayProvider.Name, providerID)
460455
461456 group := NewModelGroup(t, name, providerConfigured)
462457 for _, model := range displayProvider.Models {
internal/ui/model/ui.go+3−8
@@ -2,6 +2,7 @@ package model
22
33import (
44 "bytes"
5+ "cmp"
56 "context"
67 "errors"
78 "fmt"
@@ -1393,10 +1394,7 @@ func (m *UI) handleDialogMsg(msg tea.Msg) tea.Cmd {
13931394 case dialog.ActionRunMCPPrompt:
13941395 if len(msg.Arguments) > 0 && msg.Args == nil {
13951396 m.dialog.CloseFrontDialog()
1396− title := msg.Title
1397− if title == "" {
1398− title = "MCP Prompt Arguments"
1399− }
1397+ title := cmp.Or(msg.Title, "MCP Prompt Arguments")
14001398 argsDialog := dialog.NewArguments(
14011399 m.com,
14021400 title,
@@ -2563,10 +2561,7 @@ func (m *UI) insertFileCompletion(path string) tea.Cmd {
25632561// insertMCPResourceCompletion inserts the selected resource into the textarea,
25642562// replacing the @query, and adds the resource as an attachment.
25652563func (m *UI) insertMCPResourceCompletion(item completions.ResourceCompletionValue) tea.Cmd {
2566− displayText := item.Title
2567− if displayText == "" {
2568− displayText = item.URI
2569− }
2564+ displayText := cmp.Or(item.Title, item.URI)
25702565
25712566 if !m.insertCompletionText(displayText) {
25722567 return nil