fix view sorting being reset (#2736)

mine
Jayson Wang 2024-06-15 23:03:13 +08:00 committed by GitHub
parent c4ff75c81a
commit 7380be9cf8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 47 additions and 8 deletions

View File

@ -4,10 +4,12 @@
package config package config
import ( import (
"cmp"
"errors" "errors"
"fmt" "fmt"
"io/fs" "io/fs"
"os" "os"
"slices"
"strings" "strings"
"github.com/derailed/k9s/internal/config/data" "github.com/derailed/k9s/internal/config/data"
@ -48,6 +50,16 @@ func (v *ViewSetting) SortCol() (string, bool, error) {
return tt[0], tt[1] == "desc", nil return tt[0], tt[1] == "desc", nil
} }
func (v *ViewSetting) Equals(vs *ViewSetting) bool {
if v == nil || vs == nil {
return v == nil && vs == nil
}
if c := slices.Compare(v.Columns, vs.Columns); c != 0 {
return false
}
return cmp.Compare(v.SortColumn, vs.SortColumn) == 0
}
// CustomView represents a collection of view customization. // CustomView represents a collection of view customization.
type CustomView struct { type CustomView struct {
Views map[string]ViewSetting `yaml:"views"` Views map[string]ViewSetting `yaml:"views"`

View File

@ -17,3 +17,24 @@ func TestViewSettingsLoad(t *testing.T) {
assert.Equal(t, 1, len(cfg.Views)) assert.Equal(t, 1, len(cfg.Views))
assert.Equal(t, 4, len(cfg.Views["v1/pods"].Columns)) assert.Equal(t, 4, len(cfg.Views["v1/pods"].Columns))
} }
func TestViewSetting_Equals(t *testing.T) {
tests := []struct {
v1, v2 *config.ViewSetting
equals bool
}{
{nil, nil, true},
{&config.ViewSetting{}, nil, false},
{nil, &config.ViewSetting{}, false},
{&config.ViewSetting{}, &config.ViewSetting{}, true},
{&config.ViewSetting{Columns: []string{"A"}}, &config.ViewSetting{}, false},
{&config.ViewSetting{Columns: []string{"A"}}, &config.ViewSetting{Columns: []string{"A"}}, true},
{&config.ViewSetting{Columns: []string{"A"}}, &config.ViewSetting{Columns: []string{"B"}}, false},
{&config.ViewSetting{SortColumn: "A"}, &config.ViewSetting{SortColumn: "B"}, false},
{&config.ViewSetting{SortColumn: "A"}, &config.ViewSetting{SortColumn: "A"}, true},
}
for _, tt := range tests {
assert.Equalf(t, tt.equals, tt.v1.Equals(tt.v2), "%#v and %#v", tt.v1, tt.v2)
}
}

View File

@ -218,7 +218,7 @@ func (s *SelectTable) markRange(prev, curr int) {
} }
// IsMarked returns true if this item was marked. // IsMarked returns true if this item was marked.
func (s *Table) IsMarked(item string) bool { func (s *SelectTable) IsMarked(item string) bool {
_, ok := s.marks[item] _, ok := s.marks[item]
return ok return ok
} }

View File

@ -106,11 +106,16 @@ func (t *Table) getMSort() bool {
return t.manualSort return t.manualSort
} }
func (t *Table) setVs(vs *config.ViewSetting) { func (t *Table) setVs(vs *config.ViewSetting) bool {
t.mx.Lock() t.mx.Lock()
defer t.mx.Unlock() defer t.mx.Unlock()
t.viewSetting = vs if !t.viewSetting.Equals(vs) {
t.viewSetting = vs
return true
}
return false
} }
func (t *Table) getVs() *config.ViewSetting { func (t *Table) getVs() *config.ViewSetting {
@ -150,9 +155,10 @@ func (t *Table) GVR() client.GVR { return t.gvr }
// ViewSettingsChanged notifies listener the view configuration changed. // ViewSettingsChanged notifies listener the view configuration changed.
func (t *Table) ViewSettingsChanged(vs config.ViewSetting) { func (t *Table) ViewSettingsChanged(vs config.ViewSetting) {
t.setVs(&vs) if t.setVs(&vs) {
t.setMSort(false) t.setMSort(false)
t.Refresh() t.Refresh()
}
} }
// StylesChanged notifies the skin changed. // StylesChanged notifies the skin changed.

View File

@ -73,7 +73,7 @@ type Viewer interface {
type TableViewer interface { type TableViewer interface {
Viewer Viewer
// Table returns a table component. // GetTable returns a table component.
GetTable() *Table GetTable() *Table
} }
@ -90,7 +90,7 @@ type ResourceViewer interface {
// SetContextFn provision a custom context. // SetContextFn provision a custom context.
SetContextFn(ContextFunc) SetContextFn(ContextFunc)
// AddBindKeys provision additional key bindings. // AddBindKeysFn provision additional key bindings.
AddBindKeysFn(BindKeysFunc) AddBindKeysFn(BindKeysFunc)
// SetInstance sets a parent FQN // SetInstance sets a parent FQN