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
func (c *Config) GetStateColor(stateName string) string {
for _, state := range c.States.States {
if state.Name == stateName {
if state := c.findState(stateName); state != nil {
return state.Color
}
}
// Return a default color if state not found
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
// done state when done is true.
func (c *Config) AddState(name, color string, done bool) {
for i := range c.States.States {
if c.States.States[i].Name == name {
c.States.States[i].Color = color
c.States.States[i].Done = done
if state := c.findState(name); state != nil {
state.Color = color
state.Done = done
return
}
}
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
func (c *Config) UpdateStateColor(name, color string) {
for i, state := range c.States.States {
if state.Name == name {
c.States.States[i].Color = color
return
}
if state := c.findState(name); state != nil {
state.Color = color
}
}
@ -533,11 +538,9 @@ func (c *Config) IsDoneState(name string) bool {
if name == "" {
return false
}
for _, state := range c.States.States {
if state.Name == name {
if state := c.findState(name); state != nil {
return state.Done
}
}
return false
}