add findState() helper

This commit is contained in:
Taybin Rutkin 2026-07-22 10:45:26 -04:00
parent 98a55ce534
commit 486c9f4bb2
No known key found for this signature in database
GPG key ID: 605FA2C570B6AC9E

View file

@ -475,13 +475,23 @@ func (c *Config) UpdateTagColor(name, color string) {
} }
} }
// findState returns a pointer to the state with the given name, or nil if no
// such state is configured. The pointer aliases the backing slice, so callers
// may mutate the state in place.
func (c *Config) findState(name string) *StateConfig {
for i := range c.States.States {
if c.States.States[i].Name == name {
return &c.States.States[i]
}
}
return nil
}
// GetStateColor returns the color for a given state name // GetStateColor returns the color for a given state name
func (c *Config) GetStateColor(stateName string) string { func (c *Config) GetStateColor(stateName string) string {
for _, state := range c.States.States { if state := c.findState(stateName); state != nil {
if state.Name == stateName {
return state.Color return state.Color
} }
}
// Return a default color if state not found // Return a default color if state not found
return "99" return "99"
} }
@ -489,13 +499,11 @@ func (c *Config) GetStateColor(stateName string) string {
// AddState adds (or upgrades) a state in the configuration, marking it as a // AddState adds (or upgrades) a state in the configuration, marking it as a
// done state when done is true. // done state when done is true.
func (c *Config) AddState(name, color string, done bool) { func (c *Config) AddState(name, color string, done bool) {
for i := range c.States.States { if state := c.findState(name); state != nil {
if c.States.States[i].Name == name { state.Color = color
c.States.States[i].Color = color state.Done = done
c.States.States[i].Done = done
return return
} }
}
c.States.States = append(c.States.States, StateConfig{Name: name, Color: color, Done: done}) c.States.States = append(c.States.States, StateConfig{Name: name, Color: color, Done: done})
} }
@ -511,11 +519,8 @@ func (c *Config) RemoveState(name string) {
// UpdateStateColor updates the color of an existing state // UpdateStateColor updates the color of an existing state
func (c *Config) UpdateStateColor(name, color string) { func (c *Config) UpdateStateColor(name, color string) {
for i, state := range c.States.States { if state := c.findState(name); state != nil {
if state.Name == name { state.Color = color
c.States.States[i].Color = color
return
}
} }
} }
@ -533,11 +538,9 @@ func (c *Config) IsDoneState(name string) bool {
if name == "" { if name == "" {
return false return false
} }
for _, state := range c.States.States { if state := c.findState(name); state != nil {
if state.Name == name {
return state.Done return state.Done
} }
}
return false return false
} }