Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix view sorting being reset #2736

Merged
merged 1 commit into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions internal/config/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
package config

import (
"cmp"
"errors"
"fmt"
"io/fs"
"os"
"slices"
"strings"

"github.com/derailed/k9s/internal/config/data"
Expand Down Expand Up @@ -48,6 +50,16 @@ func (v *ViewSetting) SortCol() (string, bool, error) {
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.
type CustomView struct {
Views map[string]ViewSetting `yaml:"views"`
Expand Down
21 changes: 21 additions & 0 deletions internal/config/views_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,24 @@ func TestViewSettingsLoad(t *testing.T) {
assert.Equal(t, 1, len(cfg.Views))
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)
}
}
2 changes: 1 addition & 1 deletion internal/ui/select_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func (s *SelectTable) markRange(prev, curr int) {
}

// 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]
return ok
}
16 changes: 11 additions & 5 deletions internal/ui/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,16 @@ func (t *Table) getMSort() bool {
return t.manualSort
}

func (t *Table) setVs(vs *config.ViewSetting) {
func (t *Table) setVs(vs *config.ViewSetting) bool {
t.mx.Lock()
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 {
Expand Down Expand Up @@ -150,9 +155,10 @@ func (t *Table) GVR() client.GVR { return t.gvr }

// ViewSettingsChanged notifies listener the view configuration changed.
func (t *Table) ViewSettingsChanged(vs config.ViewSetting) {
t.setVs(&vs)
t.setMSort(false)
t.Refresh()
if t.setVs(&vs) {
t.setMSort(false)
t.Refresh()
}
}

// StylesChanged notifies the skin changed.
Expand Down
4 changes: 2 additions & 2 deletions internal/view/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type Viewer interface {
type TableViewer interface {
Viewer

// Table returns a table component.
// GetTable returns a table component.
GetTable() *Table
}

Expand All @@ -90,7 +90,7 @@ type ResourceViewer interface {
// SetContextFn provision a custom context.
SetContextFn(ContextFunc)

// AddBindKeys provision additional key bindings.
// AddBindKeysFn provision additional key bindings.
AddBindKeysFn(BindKeysFunc)

// SetInstance sets a parent FQN
Expand Down