diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2022-03-19 09:31:52 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2022-03-19 12:12:57 +1100 |
commit | d93fef4c61db20dd9e2bb535c2fbb742cdbed60a (patch) | |
tree | 0e065ab7f334ea44b4597a79f6f8e36a018f2a15 /pkg/gui/context | |
parent | 4b56d428ffda44cf433d7cfdd83ea99417ec3e86 (diff) |
use generics to DRY up context code
Diffstat (limited to 'pkg/gui/context')
-rw-r--r-- | pkg/gui/context/basic_view_model.go | 34 | ||||
-rw-r--r-- | pkg/gui/context/branches_context.go | 36 | ||||
-rw-r--r-- | pkg/gui/context/commit_files_context.go | 2 | ||||
-rw-r--r-- | pkg/gui/context/list_context_trait.go | 4 | ||||
-rw-r--r-- | pkg/gui/context/local_commits_context.go | 22 | ||||
-rw-r--r-- | pkg/gui/context/menu_context.go | 17 | ||||
-rw-r--r-- | pkg/gui/context/reflog_commits_context.go | 34 | ||||
-rw-r--r-- | pkg/gui/context/remote_branches_context.go | 36 | ||||
-rw-r--r-- | pkg/gui/context/remotes_context.go | 34 | ||||
-rw-r--r-- | pkg/gui/context/stash_context.go | 34 | ||||
-rw-r--r-- | pkg/gui/context/sub_commits_context.go | 34 | ||||
-rw-r--r-- | pkg/gui/context/submodules_context.go | 34 | ||||
-rw-r--r-- | pkg/gui/context/suggestions_context.go | 34 | ||||
-rw-r--r-- | pkg/gui/context/tags_context.go | 36 | ||||
-rw-r--r-- | pkg/gui/context/traits/list_cursor.go | 8 | ||||
-rw-r--r-- | pkg/gui/context/working_tree_context.go | 2 |
16 files changed, 77 insertions, 324 deletions
diff --git a/pkg/gui/context/basic_view_model.go b/pkg/gui/context/basic_view_model.go new file mode 100644 index 000000000..a53be4d91 --- /dev/null +++ b/pkg/gui/context/basic_view_model.go @@ -0,0 +1,34 @@ +package context + +import "github.com/jesseduffield/lazygit/pkg/gui/context/traits" + +type BasicViewModel[T any] struct { + *traits.ListCursor + getModel func() []T +} + +func NewBasicViewModel[T any](getModel func() []T) *BasicViewModel[T] { + self := &BasicViewModel[T]{ + getModel: getModel, + } + + self.ListCursor = traits.NewListCursor(self) + + return self +} + +func (self *BasicViewModel[T]) Len() int { + return len(self.getModel()) +} + +func (self *BasicViewModel[T]) GetSelected() T { + if self.Len() == 0 { + return Zero[T]() + } + + return self.getModel()[self.GetSelectedLineIdx()] +} + +func Zero[T any]() T { + return *new(T) +} diff --git a/pkg/gui/context/branches_context.go b/pkg/gui/context/branches_context.go index 146810a86..e5de639d9 100644 --- a/pkg/gui/context/branches_context.go +++ b/pkg/gui/context/branches_context.go @@ -3,12 +3,11 @@ package context import ( "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/commands/models" - "github.com/jesseduffield/lazygit/pkg/gui/context/traits" "github.com/jesseduffield/lazygit/pkg/gui/types" ) type BranchesContext struct { - *BranchesViewModel + *BasicViewModel[*models.Branch] *ListContextTrait } @@ -25,10 +24,10 @@ func NewBranchesContext( c *types.HelperCommon, ) *BranchesContext { - viewModel := NewBranchesViewModel(getModel) + viewModel := NewBasicViewModel(getModel) return &BranchesContext{ - BranchesViewModel: viewModel, + BasicViewModel: viewModel, ListContextTrait: &ListContextTrait{ Context: NewSimpleContext(NewBaseContext(NewBaseContextOpts{ ViewName: "branches", @@ -58,34 +57,7 @@ func (self *BranchesContext) GetSelectedItemId() string { return item.ID() } -type BranchesViewModel struct { - *traits.ListCursor - getModel func() []*models.Branch -} - -func NewBranchesViewModel(getModel func() []*models.Branch) *BranchesViewModel { - self := &BranchesViewModel{ - getModel: getModel, - } - - self.ListCursor = traits.NewListCursor(self) - - return self -} - -func (self *BranchesViewModel) GetItemsLength() int { - return len(self.getModel()) -} - -func (self *BranchesViewModel) GetSelected() *models.Branch { - if self.GetItemsLength() == 0 { - return nil - } - - return self.getModel()[self.GetSelectedLineIdx()] -} - -func (self *BranchesViewModel) GetSelectedRefName() string { +func (self *BranchesContext) GetSelectedRefName() string { item := self.GetSelected() if item == nil { return "" diff --git a/pkg/gui/context/commit_files_context.go b/pkg/gui/context/commit_files_context.go index 8f9bd91f7..0576be102 100644 --- a/pkg/gui/context/commit_files_context.go +++ b/pkg/gui/context/commit_files_context.go @@ -52,7 +52,7 @@ func NewCommitFilesContext( } func (self *CommitFilesContext) GetSelectedItemId() string { - item := self.GetSelectedFileNode() + item := self.GetSelected() if item == nil { return "" } diff --git a/pkg/gui/context/list_context_trait.go b/pkg/gui/context/list_context_trait.go index e508c8029..a10f0e3e9 100644 --- a/pkg/gui/context/list_context_trait.go +++ b/pkg/gui/context/list_context_trait.go @@ -27,7 +27,7 @@ func (self *ListContextTrait) GetViewTrait() types.IViewTrait { func (self *ListContextTrait) FocusLine() { // we need a way of knowing whether we've rendered to the view yet. self.viewTrait.FocusPoint(self.list.GetSelectedLineIdx()) - self.viewTrait.SetFooter(formatListFooter(self.list.GetSelectedLineIdx(), self.list.GetItemsLength())) + self.viewTrait.SetFooter(formatListFooter(self.list.GetSelectedLineIdx(), self.list.Len())) } func formatListFooter(selectedLineIdx int, length int) string { @@ -49,7 +49,7 @@ func (self *ListContextTrait) HandleFocusLost() error { // OnFocus assumes that the content of the context has already been rendered to the view. OnRender is the function which actually renders the content to the view func (self *ListContextTrait) HandleRender() error { self.list.RefreshSelectedIdx() - content := utils.RenderDisplayStrings(self.getDisplayStrings(0, self.list.GetItemsLength())) + content := utils.RenderDisplayStrings(self.getDisplayStrings(0, self.list.Len())) self.viewTrait.SetContent(content) self.c.Render() diff --git a/pkg/gui/context/local_commits_context.go b/pkg/gui/context/local_commits_context.go index 2930348e8..d8d64392c 100644 --- a/pkg/gui/context/local_commits_context.go +++ b/pkg/gui/context/local_commits_context.go @@ -3,7 +3,6 @@ package context import ( "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/commands/models" - "github.com/jesseduffield/lazygit/pkg/gui/context/traits" "github.com/jesseduffield/lazygit/pkg/gui/types" ) @@ -60,8 +59,7 @@ func (self *LocalCommitsContext) GetSelectedItemId() string { } type LocalCommitsViewModel struct { - *traits.ListCursor - getModel func() []*models.Commit + *BasicViewModel[*models.Commit] // If this is true we limit the amount of commits we load, for the sake of keeping things fast. // If the user attempts to scroll past the end of the list, we will load more commits. @@ -73,12 +71,10 @@ type LocalCommitsViewModel struct { func NewLocalCommitsViewModel(getModel func() []*models.Commit) *LocalCommitsViewModel { self := &LocalCommitsViewModel{ - getModel: getModel, - limitCommits: true, + BasicViewModel: NewBasicViewModel(getModel), + limitCommits: true, } - self.ListCursor = traits.NewListCursor(self) - return self } @@ -96,18 +92,6 @@ func (self *LocalCommitsContext) GetSelectedRefName() string { return item.RefName() } -func (self *LocalCommitsViewModel) GetItemsLength() int { - return len(self.getModel()) -} - -func (self *LocalCommitsViewModel) GetSelected() *models.Commit { - if self.GetItemsLength() == 0 { - return nil - } - - return self.getModel()[self.GetSelectedLineIdx()] -} - func (self *LocalCommitsViewModel) SetLimitCommits(value bool) { self.limitCommits = value } diff --git a/pkg/gui/context/menu_context.go b/pkg/gui/context/menu_context.go index 2e75ba25a..67d6b126a 100644 --- a/pkg/gui/context/menu_context.go +++ b/pkg/gui/context/menu_context.go @@ -2,7 +2,6 @@ package context import ( "github.com/jesseduffield/gocui" - "github.com/jesseduffield/lazygit/pkg/gui/context/traits" "github.com/jesseduffield/lazygit/pkg/gui/presentation" "github.com/jesseduffield/lazygit/pkg/gui/types" ) @@ -59,8 +58,8 @@ func (self *MenuContext) GetSelectedItemId() string { } type MenuViewModel struct { - *traits.ListCursor menuItems []*types.MenuItem + *BasicViewModel[*types.MenuItem] } func NewMenuViewModel() *MenuViewModel { @@ -68,23 +67,11 @@ func NewMenuViewModel() *MenuViewModel { menuItems: nil, } - self.ListCursor = traits.NewListCursor(self) + self.BasicViewModel = NewBasicViewModel(func() []*types.MenuItem { return self.menuItems }) return self } -func (self *MenuViewModel) GetItemsLength() int { - return len(self.menuItems) -} - -func (self *MenuViewModel) GetSelected() *types.MenuItem { - if self.GetItemsLength() == 0 { - return nil - } - - return self.menuItems[self.GetSelectedLineIdx()] -} - func (self *MenuViewModel) SetMenuItems(items []*types.MenuItem) { self.menuItems = items } diff --git a/pkg/gui/context/reflog_commits_context.go b/pkg/gui/context/reflog_commits_context.go index fa136a7d4..815805515 100644 --- a/pkg/gui/context/reflog_commits_context.go +++ b/pkg/gui/context/reflog_commits_context.go @@ -3,12 +3,11 @@ package context import ( "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/commands/models" - "github.com/jesseduffield/lazygit/pkg/gui/context/traits" "github.com/jesseduffield/lazygit/pkg/gui/types" ) type ReflogCommitsContext struct { - *ReflogCommitsViewModel + *BasicViewModel[*models.Commit] *ListContextTrait } @@ -25,10 +24,10 @@ func NewReflogCommitsContext( c *types.HelperCommon, ) *ReflogCommitsContext { - viewModel := NewReflogCommitsViewModel(getModel) + viewModel := NewBasicViewModel(getModel) return &ReflogCommitsContext{ - ReflogCommitsViewModel: viewModel, + BasicViewModel: viewModel, ListContextTrait: &ListContextTrait{ Context: NewSimpleContext(NewBaseContext(NewBaseContextOpts{ ViewName: "commits", @@ -71,30 +70,3 @@ func (self *ReflogCommitsContext) GetSelectedRefName() string { return item.RefName() } - -type ReflogCommitsViewModel struct { - *traits.ListCursor - getModel func() []*models.Commit -} - -func NewReflogCommitsViewModel(getModel func() []*models.Commit) *ReflogCommitsViewModel { - self := &ReflogCommitsViewModel{ - getModel: getModel, - } - - self.ListCursor = traits.NewListCursor(self) - - return self -} - -func (self *ReflogCommitsViewModel) GetItemsLength() int { - return len(self.getModel()) -} - -func (self *ReflogCommitsViewModel) GetSelected() *models.Commit { - if self.GetItemsLength() == 0 { - return nil - } - - return self.getModel()[self.GetSelectedLineIdx()] -} diff --git a/pkg/gui/context/remote_branches_context.go b/pkg/gui/context/remote_branches_context.go index c851c96ac..3cdd43a69 100644 --- a/pkg/gui/context/remote_branches_context.go +++ b/pkg/gui/context/remote_branches_context.go @@ -3,12 +3,11 @@ package context import ( "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/commands/models" - "github.com/jesseduffield/lazygit/pkg/gui/context/traits" "github.com/jesseduffield/lazygit/pkg/gui/types" ) type RemoteBranchesContext struct { - *RemoteBranchesViewModel + *BasicViewModel[*models.RemoteBranch] *ListContextTrait } @@ -25,10 +24,10 @@ func NewRemoteBranchesContext( c *types.HelperCommon, ) *RemoteBranchesContext { - viewModel := NewRemoteBranchesViewModel(getModel) + viewModel := NewBasicViewModel(getModel) return &RemoteBranchesContext{ - RemoteBranchesViewModel: viewModel, + BasicViewModel: viewModel, ListContextTrait: &ListContextTrait{ Context: NewSimpleContext(NewBaseContext(NewBaseContextOpts{ ViewName: "branches", @@ -58,34 +57,7 @@ func (self *RemoteBranchesContext) GetSelectedItemId() string { return item.ID() } -type RemoteBranchesViewModel struct { - *traits.ListCursor - getModel func() []*models.RemoteBranch -} - -func NewRemoteBranchesViewModel(getModel func() []*models.RemoteBranch) *RemoteBranchesViewModel { - self := &RemoteBranchesViewModel{ - getModel: getModel, - } - - self.ListCursor = traits.NewListCursor(self) - - return self -} - -func (self *RemoteBranchesViewModel) GetItemsLength() int { - return len(self.getModel()) -} - -func (self *RemoteBranchesViewModel) GetSelected() *models.RemoteBranch { - if self.GetItemsLength() == 0 { - return nil - } - - return self.getModel()[self.GetSelectedLineIdx()] -} - -func (self *RemoteBranchesViewModel) GetSelectedRefName() string { +func (self *RemoteBranchesContext) GetSelectedRefName() string { item := self.GetSelected() if item == nil { return "" diff --git a/pkg/gui/context/remotes_context.go b/pkg/gui/context/remotes_context.go index 2b6afdeb5..9cb0b6054 100644 --- a/pkg/gui/context/remotes_context.go +++ b/pkg/gui/context/remotes_context.go @@ -3,12 +3,11 @@ package context import ( "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/commands/models" - "github.com/jesseduffield/lazygit/pkg/gui/context/traits" "github.com/jesseduffield/lazygit/pkg/gui/types" ) type RemotesContext struct { - *RemotesViewModel + *BasicViewModel[*models.Remote] *ListContextTrait } @@ -25,10 +24,10 @@ func NewRemotesContext( c *types.HelperCommon, ) *RemotesContext { - viewModel := NewRemotesViewModel(getModel) + viewModel := NewBasicViewModel(getModel) return &RemotesContext{ - RemotesViewModel: viewModel, + BasicViewModel: viewModel, ListContextTrait: &ListContextTrait{ Context: NewSimpleContext(NewBaseContext(NewBaseContextOpts{ ViewName: "branches", @@ -57,30 +56,3 @@ func (self *RemotesContext) GetSelectedItemId() string { return item.ID() } - -type RemotesViewModel struct { - *traits.ListCursor - getModel func() []*models.Remote -} - -func NewRemotesViewModel(getModel func() []*models.Remote) *RemotesViewModel { - self := &RemotesViewModel{ - getModel: getModel, - } - - self.ListCursor = traits.NewListCursor(self) - - return self -} - -func (self *RemotesViewModel) GetItemsLength() int { - return len(self.getModel()) -} - -func (self *RemotesViewModel) GetSelected() *models.Remote { - if self.GetItemsLength() == 0 { - return nil - } - - return self.getModel()[self.GetSelectedLineIdx()] -} diff --git a/pkg/gui/context/stash_context.go b/pkg/gui/context/stash_context.go index d538fadf1..e2af64d10 100644 --- a/pkg/gui/context/stash_context.go +++ b/pkg/gui/context/stash_context.go @@ -3,12 +3,11 @@ package context import ( "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/commands/models" - "github.com/jesseduffield/lazygit/pkg/gui/context/traits" "github.com/jesseduffield/lazygit/pkg/gui/types" ) type StashContext struct { - *StashViewModel + *BasicViewModel[*models.StashEntry] *ListContextTrait } @@ -25,10 +24,10 @@ func NewStashContext( c *types.HelperCommon, ) *StashContext { - viewModel := NewStashViewModel(getModel) + viewModel := NewBasicViewModel(getModel) return &StashContext{ - StashViewModel: viewModel, + BasicViewModel: viewModel, ListContextTrait: &ListContextTrait{ Context: NewSimpleContext(NewBaseContext(NewBaseContextOpts{ ViewName: "stash", @@ -71,30 +70,3 @@ func (self *StashContext) GetSelectedRefName() string { return item.RefName() } - -type StashViewModel struct { - *traits.ListCursor - getModel func() []*models.StashEntry -} - -func NewStashViewModel(getModel func() []*models.StashEntry) *StashViewModel { - self := &StashViewModel{ - getModel: getModel, - } - - self.ListCursor = traits.NewListCursor(self) - - return self -} - -func (self *StashViewModel) GetItemsLength() int { - return len(self.getModel()) -} - -func (self *StashViewModel) GetSelected() *models.StashEntry { - if self.GetItemsLength() == 0 { - return nil - } - - return self.getModel()[self.GetSelectedLineIdx()] -} diff --git a/pkg/gui/context/sub_commits_context.go b/pkg/gui/context/sub_commits_context.go index 83e76e1e0..0f16f1688 100644 --- a/pkg/gui/context/sub_commits_context.go +++ b/pkg/gui/context/sub_commits_context.go @@ -3,12 +3,11 @@ package context import ( "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/commands/models" - "github.com/jesseduffield/lazygit/pkg/gui/context/traits" "github.com/jesseduffield/lazygit/pkg/gui/types" ) type SubCommitsContext struct { - *SubCommitsViewModel + *BasicViewModel[*models.Commit] *ViewportListContextTrait } @@ -25,10 +24,10 @@ func NewSubCommitsContext( c *types.HelperCommon, ) *SubCommitsContext { - viewModel := NewSubCommitsViewModel(getModel) + viewModel := NewBasicViewModel(getModel) return &SubCommitsContext{ - SubCommitsViewModel: viewModel, + BasicViewModel: viewModel, ViewportListContextTrait: &ViewportListContextTrait{ ListContextTrait: &ListContextTrait{ Context: NewSimpleContext(NewBaseContext(NewBaseContextOpts{ @@ -72,30 +71,3 @@ func (self *SubCommitsContext) GetSelectedRefName() string { return item.RefName() } - -type SubCommitsViewModel struct { - *traits.ListCursor - getModel func() []*models.Commit -} - -func NewSubCommitsViewModel(getModel func() []*models.Commit) *SubCommitsViewModel { - self := &SubCommitsViewModel{ - getModel: getModel, - } - - self.ListCursor = traits.NewListCursor(self) - - return self -} - -func (self *SubCommitsViewModel) GetItemsLength() int { - return len(self.getModel()) -} - -func (self *SubCommitsViewModel) GetSelected() *models.Commit { - if self.GetItemsLength() == 0 { - return nil - } - - return self.getModel()[self.GetSelectedLineIdx()] -} diff --git a/pkg/gui/context/submodules_context.go b/pkg/gui/context/submodules_context.go index 2bf5fe274..a88ae0dfc 100644 --- a/pkg/gui/context/submodules_context.go +++ b/pkg/gui/context/submodules_context.go @@ -3,12 +3,11 @@ package context import ( "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/commands/models" - "github.com/jesseduffield/lazygit/pkg/gui/context/traits" "github.com/jesseduffield/lazygit/pkg/gui/types" ) type SubmodulesContext struct { - *SubmodulesViewModel + *BasicViewModel[*models.SubmoduleConfig] *ListContextTrait } @@ -25,10 +24,10 @@ func NewSubmodulesContext( c *types.HelperCommon, ) *SubmodulesContext { - viewModel := NewSubmodulesViewModel(getModel) + viewModel := NewBasicViewModel(getModel) return &SubmodulesContext{ - SubmodulesViewModel: viewModel, + BasicViewModel: viewModel, ListContextTrait: &ListContextTrait{ Context: NewSimpleContext(NewBaseContext(NewBaseContextOpts{ ViewName: "files", @@ -57,30 +56,3 @@ func (self *SubmodulesContext) GetSelectedItemId() string { return item.ID() } - -type SubmodulesViewModel struct { - *traits.ListCursor - getModel func() []*models.SubmoduleConfig -} - -func NewSubmodulesViewModel(getModel func() []*models.SubmoduleConfig) *SubmodulesViewModel { - self := &SubmodulesViewModel{ - getModel: getModel, - } - - self.ListCursor = traits.NewListCursor(self) - - return self -} - -func (self *SubmodulesViewModel) GetItemsLength() int { - return len(self.getModel()) -} - -func (self *SubmodulesViewModel) GetSelected() *models.SubmoduleConfig { - if self.GetItemsLength() == 0 { - return nil - } - - return self.getModel()[self.GetSelectedLineIdx()] -} diff --git a/pkg/gui/context/suggestions_context.go b/pkg/gui/context/suggestions_context.go index 6c565eedf..1291b37c8 100644 --- a/pkg/gui/context/suggestions_context.go +++ b/pkg/gui/context/suggestions_context.go @@ -2,12 +2,11 @@ package context import ( "github.com/jesseduffield/gocui" - "github.com/jesseduffield/lazygit/pkg/gui/context/traits" "github.com/jesseduffield/lazygit/pkg/gui/types" ) type SuggestionsContext struct { - *SuggestionsViewModel + *BasicViewModel[*types.Suggestion] *ListContextTrait } @@ -24,10 +23,10 @@ func NewSuggestionsContext( c *types.HelperCommon, ) *SuggestionsContext { - viewModel := NewSuggestionsViewModel(getModel) + viewModel := NewBasicViewModel(getModel) return &SuggestionsContext{ - SuggestionsViewModel: viewModel, + BasicViewModel: viewModel, ListContextTrait: &ListContextTrait{ Context: NewSimpleContext(NewBaseContext(NewBaseContextOpts{ ViewName: "suggestions", @@ -56,30 +55,3 @@ func (self *SuggestionsContext) GetSelectedItemId() string { return item.Value } - -type SuggestionsViewModel struct { - *traits.ListCursor - getModel func() []*types.Suggestion -} - -func NewSuggestionsViewModel(getModel func() []*types.Suggestion) *SuggestionsViewModel { - self := &SuggestionsViewModel{ - getModel: getModel, - } - - self.ListCursor = traits.NewListCursor(self) - - return self -} - -func (self *SuggestionsViewModel) GetItemsLength() int { - return len(self.getModel()) -} - -func (self *SuggestionsViewModel) GetSelected() *types.Suggestion { - if self.GetItemsLength() == 0 { - return nil - } - - return self.getModel()[self.GetSelectedLineIdx()] -} diff --git a/pkg/gui/context/tags_context.go b/pkg/gui/context/tags_context.go index aa6211f40..fd411ec9a 100644 --- a/pkg/gui/context/tags_context.go +++ b/pkg/gui/context/tags_context.go @@ -3,12 +3,11 @@ package context import ( "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/commands/models" - "github.com/jesseduffield/lazygit/pkg/gui/context/traits" "github.com/jesseduffield/lazygit/pkg/gui/types" ) type TagsContext struct { - *TagsViewModel + *BasicViewModel[*models.Tag] *ListContextTrait } @@ -25,10 +24,10 @@ func NewTagsContext( c *types.HelperCommon, ) *TagsContext { - viewModel := NewTagsViewModel(getModel) + viewModel := NewBasicViewModel(getModel) return &TagsContext{ - TagsViewModel: viewModel, + BasicViewModel: viewModel, ListContextTrait: &ListContextTrait{ Context: NewSimpleContext(NewBaseContext(NewBaseContextOpts{ ViewName: "branches", @@ -58,34 +57,7 @@ func (self *TagsContext) GetSelectedItemId() string { return item.ID() } -type TagsViewModel struct { - *traits.ListCursor - getModel func() []*models.Tag -} - -func NewTagsViewModel(getModel func() []*models.Tag) *TagsViewModel { - self := &TagsViewModel{ - getModel: getModel, - } - - self.ListCursor = traits.NewListCursor(self) - - return self -} - -func (self *TagsViewModel) GetItemsLength() int { - return len(self.getModel()) -} - -func (self *TagsViewModel) GetSelected() *models.Tag { - if self.GetItemsLength() == 0 { - return nil - } - - return self.getModel()[self.GetSelectedLineIdx()] -} - -func (self *TagsViewModel) GetSelectedRefName() string { +func (self *TagsContext) GetSelectedRefName() string { item := self.GetSelected() if item == nil { return "" diff --git a/pkg/gui/context/traits/list_cursor.go b/pkg/gui/context/traits/list_cursor.go index 9423ad89c..6e80643d6 100644 --- a/pkg/gui/context/traits/list_cursor.go +++ b/pkg/gui/context/traits/list_cursor.go @@ -6,7 +6,7 @@ import ( ) type HasLength interface { - GetItemsLength() int + Len() int } type ListCursor struct { @@ -25,7 +25,7 @@ func (self *ListCursor) GetSelectedLineIdx() int { } func (self *ListCursor) SetSelectedLineIdx(value int) { - self.selectedIdx = utils.Clamp(value, 0, self.list.GetItemsLength()-1) + self.selectedIdx = utils.Clamp(value, 0, self.list.Len()-1) } // moves the cursor up or down by the given amount @@ -38,6 +38,6 @@ func (self *ListCursor) RefreshSelectedIdx() { self.SetSelectedLineIdx(self.selectedIdx) } -func (self *ListCursor) GetItemsLength() int { - return self.list.GetItemsLength() +func (self *ListCursor) Len() int { + return self.list.Len() } diff --git a/pkg/gui/context/working_tree_context.go b/pkg/gui/context/working_tree_context.go index ae647afb3..5223f2982 100644 --- a/pkg/gui/context/working_tree_context.go +++ b/pkg/gui/context/working_tree_context.go @@ -50,7 +50,7 @@ func NewWorkingTreeContext( } func (self *WorkingTreeContext) GetSelectedItemId() string { - item := self.GetSelectedFileNode() + item := self.GetSelected() if item == nil { return "" } |