2019-11-16 05:00:27 +02:00
package gui
2020-08-19 13:51:50 +02:00
import (
2021-03-31 13:08:55 +02:00
"github.com/fatih/color"
2020-08-19 13:51:50 +02:00
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
2021-03-31 13:08:55 +02:00
"github.com/jesseduffield/lazygit/pkg/utils"
2020-08-19 13:51:50 +02:00
)
2019-11-16 05:00:27 +02:00
2020-08-17 13:58:30 +02:00
type ListContext struct {
2020-08-20 00:52:51 +02:00
ViewName string
2021-04-04 15:51:59 +02:00
ContextKey ContextKey
2020-08-20 00:52:51 +02:00
GetItemsLength func ( ) int
GetDisplayStrings func ( ) [ ] [ ] string
OnFocus func ( ) error
OnFocusLost func ( ) error
OnClickSelectedItem func ( ) error
2020-08-23 02:50:27 +02:00
OnGetOptionsMap func ( ) map [ string ] string
2020-08-22 07:56:30 +02:00
2020-08-23 01:42:30 +02:00
// the boolean here tells us whether the item is nil. This is needed because you can't work it out on the calling end once the pointer is wrapped in an interface (unless you want to use reflection)
2020-08-22 07:56:30 +02:00
SelectedItem func ( ) ( ListItem , bool )
GetPanelState func ( ) IListPanelState
2020-08-19 10:06:51 +02:00
2020-08-23 01:42:30 +02:00
Gui * Gui
ResetMainViewOriginOnFocus bool
2021-03-31 14:55:06 +02:00
Kind ContextKind
2020-08-23 01:42:30 +02:00
ParentContext Context
2020-08-24 00:26:42 +02:00
// we can't know on the calling end whether a Context is actually a nil value without reflection, so we're storing this flag here to tell us. There has got to be a better way around this.
hasParent bool
WindowName string
2019-11-16 05:00:27 +02:00
}
2021-03-31 15:24:41 +02:00
type IListPanelState interface {
SetSelectedLineIdx ( int )
GetSelectedLineIdx ( ) int
}
2020-08-20 00:24:35 +02:00
type ListItem interface {
2020-08-22 02:14:53 +02:00
// ID is a SHA when the item is a commit, a filename when the item is a file, 'stash@{4}' when it's a stash entry, 'my_branch' when it's a branch
2020-08-20 00:24:35 +02:00
ID ( ) string
2020-08-22 02:14:53 +02:00
// Description is something we would show in a message e.g. '123as14: push blah' for a commit
Description ( ) string
2020-08-20 00:24:35 +02:00
}
2020-08-22 07:56:30 +02:00
func ( lc * ListContext ) GetSelectedItem ( ) ( ListItem , bool ) {
2020-08-22 01:55:49 +02:00
return lc . SelectedItem ( )
}
2020-08-21 11:53:45 +02:00
func ( lc * ListContext ) SetWindowName ( windowName string ) {
lc . WindowName = windowName
}
func ( lc * ListContext ) GetWindowName ( ) string {
windowName := lc . WindowName
if windowName != "" {
return windowName
}
// TODO: actually set this for everything so we don't default to the view name
return lc . ViewName
}
2020-08-21 01:12:45 +02:00
func ( lc * ListContext ) SetParentContext ( c Context ) {
lc . ParentContext = c
2020-08-24 00:26:42 +02:00
lc . hasParent = true
2020-08-21 01:12:45 +02:00
}
2020-08-24 00:26:42 +02:00
func ( lc * ListContext ) GetParentContext ( ) ( Context , bool ) {
return lc . ParentContext , lc . hasParent
2020-08-21 01:12:45 +02:00
}
2020-08-20 00:24:35 +02:00
func ( lc * ListContext ) GetSelectedItemId ( ) string {
2020-08-22 07:56:30 +02:00
item , ok := lc . SelectedItem ( )
2020-08-20 00:24:35 +02:00
2020-08-22 07:56:30 +02:00
if ! ok {
2020-08-20 00:24:35 +02:00
return ""
}
return item . ID ( )
}
2020-08-23 02:50:27 +02:00
func ( lc * ListContext ) GetOptionsMap ( ) map [ string ] string {
if lc . OnGetOptionsMap != nil {
return lc . OnGetOptionsMap ( )
}
return nil
}
2020-08-19 13:51:50 +02:00
// 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 ( lc * ListContext ) OnRender ( ) error {
view , err := lc . Gui . g . View ( lc . ViewName )
if err != nil {
return nil
}
if lc . GetDisplayStrings != nil {
2020-08-20 00:52:51 +02:00
lc . Gui . refreshSelectedLine ( lc . GetPanelState ( ) , lc . GetItemsLength ( ) )
2020-08-19 13:51:50 +02:00
lc . Gui . renderDisplayStrings ( view , lc . GetDisplayStrings ( ) )
}
return nil
}
2021-04-04 15:51:59 +02:00
func ( lc * ListContext ) GetKey ( ) ContextKey {
2020-08-17 13:58:30 +02:00
return lc . ContextKey
2020-08-16 05:58:29 +02:00
}
2021-03-31 14:55:06 +02:00
func ( lc * ListContext ) GetKind ( ) ContextKind {
2020-08-17 13:58:30 +02:00
return lc . Kind
2020-08-16 05:58:29 +02:00
}
2020-08-17 13:58:30 +02:00
func ( lc * ListContext ) GetViewName ( ) string {
return lc . ViewName
2020-08-16 05:58:29 +02:00
}
2020-08-17 13:58:30 +02:00
func ( lc * ListContext ) HandleFocusLost ( ) error {
if lc . OnFocusLost != nil {
return lc . OnFocusLost ( )
2020-08-16 05:58:29 +02:00
}
return nil
}
2020-08-17 13:58:30 +02:00
func ( lc * ListContext ) HandleFocus ( ) error {
2020-08-19 14:27:31 +02:00
if lc . Gui . popupPanelFocused ( ) {
return nil
}
2020-08-22 00:49:02 +02:00
view , err := lc . Gui . g . View ( lc . ViewName )
if err != nil {
return nil
}
view . FocusPoint ( 0 , lc . GetPanelState ( ) . GetSelectedLineIdx ( ) )
2021-04-02 10:57:18 +02:00
if lc . ResetMainViewOriginOnFocus {
2021-04-04 15:51:59 +02:00
if err := lc . Gui . resetOrigin ( lc . Gui . Views . Main ) ; err != nil {
2021-04-02 10:57:18 +02:00
return err
}
2021-04-04 15:51:59 +02:00
if err := lc . Gui . resetOrigin ( lc . Gui . Views . Secondary ) ; err != nil {
2021-04-02 10:57:18 +02:00
return err
}
}
2020-08-22 03:44:03 +02:00
if lc . Gui . State . Modes . Diffing . Active ( ) {
2020-08-19 14:27:31 +02:00
return lc . Gui . renderDiff ( )
}
if lc . OnFocus != nil {
return lc . OnFocus ( )
}
return nil
2020-08-16 05:58:29 +02:00
}
2020-08-19 10:06:51 +02:00
func ( lc * ListContext ) HandleRender ( ) error {
return lc . OnRender ( )
}
2021-04-02 10:20:40 +02:00
func ( lc * ListContext ) handlePrevLine ( ) error {
2020-08-17 13:58:30 +02:00
return lc . handleLineChange ( - 1 )
2019-11-16 05:00:27 +02:00
}
2021-04-02 10:20:40 +02:00
func ( lc * ListContext ) handleNextLine ( ) error {
2020-08-17 13:58:30 +02:00
return lc . handleLineChange ( 1 )
2019-11-16 05:00:27 +02:00
}
2020-08-17 13:58:30 +02:00
func ( lc * ListContext ) handleLineChange ( change int ) error {
if ! lc . Gui . isPopupPanel ( lc . ViewName ) && lc . Gui . popupPanelFocused ( ) {
2019-11-16 05:00:27 +02:00
return nil
}
2020-08-17 13:58:30 +02:00
view , err := lc . Gui . g . View ( lc . ViewName )
2020-08-16 01:18:57 +02:00
if err != nil {
return err
}
2020-10-01 13:40:20 +02:00
selectedLineIdx := lc . GetPanelState ( ) . GetSelectedLineIdx ( )
if ( change < 0 && selectedLineIdx == 0 ) || ( change > 0 && selectedLineIdx == lc . GetItemsLength ( ) - 1 ) {
return nil
}
2020-08-20 00:52:51 +02:00
lc . Gui . changeSelectedLine ( lc . GetPanelState ( ) , lc . GetItemsLength ( ) , change )
view . FocusPoint ( 0 , lc . GetPanelState ( ) . GetSelectedLineIdx ( ) )
2019-11-16 05:00:27 +02:00
2020-08-19 14:27:31 +02:00
return lc . HandleFocus ( )
2019-11-16 05:00:27 +02:00
}
2021-04-02 10:20:40 +02:00
func ( lc * ListContext ) handleNextPage ( ) error {
2020-08-17 13:58:30 +02:00
view , err := lc . Gui . g . View ( lc . ViewName )
2020-03-28 04:44:20 +02:00
if err != nil {
return nil
}
2020-10-01 23:56:14 +02:00
delta := lc . Gui . pageDelta ( view )
2020-08-17 13:58:30 +02:00
return lc . handleLineChange ( delta )
2020-03-28 04:44:20 +02:00
}
2021-04-02 10:20:40 +02:00
func ( lc * ListContext ) handleGotoTop ( ) error {
2020-08-17 13:58:30 +02:00
return lc . handleLineChange ( - lc . GetItemsLength ( ) )
2020-03-28 04:44:20 +02:00
}
2021-04-02 10:20:40 +02:00
func ( lc * ListContext ) handleGotoBottom ( ) error {
2020-08-17 13:58:30 +02:00
return lc . handleLineChange ( lc . GetItemsLength ( ) )
2020-03-28 04:44:20 +02:00
}
2021-04-02 10:20:40 +02:00
func ( lc * ListContext ) handlePrevPage ( ) error {
2020-08-17 13:58:30 +02:00
view , err := lc . Gui . g . View ( lc . ViewName )
2020-03-28 04:44:20 +02:00
if err != nil {
return nil
}
2020-10-01 23:56:14 +02:00
delta := lc . Gui . pageDelta ( view )
2020-08-17 13:58:30 +02:00
return lc . handleLineChange ( - delta )
2020-03-28 04:44:20 +02:00
}
2021-04-02 10:20:40 +02:00
func ( lc * ListContext ) handleClick ( ) error {
2020-08-17 13:58:30 +02:00
if ! lc . Gui . isPopupPanel ( lc . ViewName ) && lc . Gui . popupPanelFocused ( ) {
2019-11-17 08:23:06 +02:00
return nil
}
2021-04-02 10:20:40 +02:00
view , err := lc . Gui . g . View ( lc . ViewName )
if err != nil {
return nil
}
2020-08-20 00:52:51 +02:00
prevSelectedLineIdx := lc . GetPanelState ( ) . GetSelectedLineIdx ( )
2021-04-02 10:20:40 +02:00
newSelectedLineIdx := view . SelectedLineIdx ( )
2019-11-17 08:23:06 +02:00
2020-08-16 01:18:57 +02:00
// we need to focus the view
2020-11-28 04:14:48 +02:00
if err := lc . Gui . pushContext ( lc ) ; err != nil {
2020-08-16 01:18:57 +02:00
return err
}
2020-08-17 13:58:30 +02:00
if newSelectedLineIdx > lc . GetItemsLength ( ) - 1 {
2020-08-16 05:58:29 +02:00
return nil
2019-11-17 08:23:06 +02:00
}
2020-08-20 00:52:51 +02:00
lc . GetPanelState ( ) . SetSelectedLineIdx ( newSelectedLineIdx )
2019-11-17 08:23:06 +02:00
2020-08-17 13:58:30 +02:00
prevViewName := lc . Gui . currentViewName ( )
if prevSelectedLineIdx == newSelectedLineIdx && prevViewName == lc . ViewName && lc . OnClickSelectedItem != nil {
return lc . OnClickSelectedItem ( )
2019-11-17 08:23:06 +02:00
}
2020-08-19 14:27:31 +02:00
return lc . HandleFocus ( )
2020-08-16 01:18:57 +02:00
}
2020-08-17 13:58:30 +02:00
func ( lc * ListContext ) onSearchSelect ( selectedLineIdx int ) error {
2020-08-20 00:52:51 +02:00
lc . GetPanelState ( ) . SetSelectedLineIdx ( selectedLineIdx )
2020-08-19 14:27:31 +02:00
return lc . HandleFocus ( )
2020-08-16 01:18:57 +02:00
}
2020-08-17 13:58:30 +02:00
func ( gui * Gui ) menuListContext ( ) * ListContext {
return & ListContext {
2020-08-23 02:13:56 +02:00
ViewName : "menu" ,
ContextKey : "menu" ,
2021-04-04 15:51:59 +02:00
GetItemsLength : func ( ) int { return gui . Views . Menu . LinesHeight ( ) } ,
2020-08-23 02:13:56 +02:00
GetPanelState : func ( ) IListPanelState { return gui . State . Panels . Menu } ,
OnFocus : gui . handleMenuSelect ,
2020-11-16 11:38:26 +02:00
OnClickSelectedItem : gui . onMenuPress ,
2020-08-23 01:42:30 +02:00
Gui : gui ,
ResetMainViewOriginOnFocus : false ,
Kind : PERSISTENT_POPUP ,
2020-08-23 02:50:27 +02:00
OnGetOptionsMap : gui . getMenuOptions ,
2020-08-19 13:51:50 +02:00
// no GetDisplayStrings field because we do a custom render on menu creation
2020-08-16 01:18:57 +02:00
}
}
2020-08-17 13:58:30 +02:00
func ( gui * Gui ) filesListContext ( ) * ListContext {
return & ListContext {
2020-08-23 01:42:30 +02:00
ViewName : "files" ,
ContextKey : FILES_CONTEXT_KEY ,
2021-03-31 14:26:53 +02:00
GetItemsLength : func ( ) int { return gui . State . FileManager . GetItemsLength ( ) } ,
2020-08-23 01:42:30 +02:00
GetPanelState : func ( ) IListPanelState { return gui . State . Panels . Files } ,
OnFocus : gui . focusAndSelectFile ,
OnClickSelectedItem : gui . handleFilePress ,
Gui : gui ,
ResetMainViewOriginOnFocus : false ,
Kind : SIDE_CONTEXT ,
2020-08-19 13:51:50 +02:00
GetDisplayStrings : func ( ) [ ] [ ] string {
2021-03-31 14:26:53 +02:00
lines := gui . State . FileManager . Render ( gui . State . Modes . Diffing . Ref , gui . State . Submodules )
2020-11-15 01:45:55 +02:00
mappedLines := make ( [ ] [ ] string , len ( lines ) )
for i , line := range lines {
mappedLines [ i ] = [ ] string { line }
}
return mappedLines
2020-08-19 13:51:50 +02:00
} ,
2020-08-22 07:56:30 +02:00
SelectedItem : func ( ) ( ListItem , bool ) {
2021-03-31 14:26:53 +02:00
item := gui . getSelectedFileNode ( )
2020-08-22 07:56:30 +02:00
return item , item != nil
} ,
2020-08-16 01:18:57 +02:00
}
}
2020-08-17 13:58:30 +02:00
func ( gui * Gui ) branchesListContext ( ) * ListContext {
return & ListContext {
2020-08-23 01:42:30 +02:00
ViewName : "branches" ,
ContextKey : LOCAL_BRANCHES_CONTEXT_KEY ,
GetItemsLength : func ( ) int { return len ( gui . State . Branches ) } ,
GetPanelState : func ( ) IListPanelState { return gui . State . Panels . Branches } ,
OnFocus : gui . handleBranchSelect ,
Gui : gui ,
ResetMainViewOriginOnFocus : true ,
Kind : SIDE_CONTEXT ,
2020-08-19 13:51:50 +02:00
GetDisplayStrings : func ( ) [ ] [ ] string {
2020-08-22 03:05:37 +02:00
return presentation . GetBranchListDisplayStrings ( gui . State . Branches , gui . State . ScreenMode != SCREEN_NORMAL , gui . State . Modes . Diffing . Ref )
2020-08-19 13:51:50 +02:00
} ,
2020-08-22 07:56:30 +02:00
SelectedItem : func ( ) ( ListItem , bool ) {
item := gui . getSelectedBranch ( )
return item , item != nil
} ,
2020-08-16 01:18:57 +02:00
}
}
2020-08-17 13:58:30 +02:00
func ( gui * Gui ) remotesListContext ( ) * ListContext {
return & ListContext {
2020-08-23 01:42:30 +02:00
ViewName : "branches" ,
ContextKey : REMOTES_CONTEXT_KEY ,
GetItemsLength : func ( ) int { return len ( gui . State . Remotes ) } ,
GetPanelState : func ( ) IListPanelState { return gui . State . Panels . Remotes } ,
OnFocus : gui . handleRemoteSelect ,
OnClickSelectedItem : gui . handleRemoteEnter ,
Gui : gui ,
ResetMainViewOriginOnFocus : true ,
Kind : SIDE_CONTEXT ,
2020-08-19 13:51:50 +02:00
GetDisplayStrings : func ( ) [ ] [ ] string {
2020-08-22 03:05:37 +02:00
return presentation . GetRemoteListDisplayStrings ( gui . State . Remotes , gui . State . Modes . Diffing . Ref )
2020-08-19 13:51:50 +02:00
} ,
2020-08-22 07:56:30 +02:00
SelectedItem : func ( ) ( ListItem , bool ) {
item := gui . getSelectedRemote ( )
return item , item != nil
} ,
2020-08-16 01:18:57 +02:00
}
}
2020-08-17 13:58:30 +02:00
func ( gui * Gui ) remoteBranchesListContext ( ) * ListContext {
return & ListContext {
2020-08-23 01:42:30 +02:00
ViewName : "branches" ,
ContextKey : REMOTE_BRANCHES_CONTEXT_KEY ,
GetItemsLength : func ( ) int { return len ( gui . State . RemoteBranches ) } ,
GetPanelState : func ( ) IListPanelState { return gui . State . Panels . RemoteBranches } ,
OnFocus : gui . handleRemoteBranchSelect ,
Gui : gui ,
ResetMainViewOriginOnFocus : true ,
Kind : SIDE_CONTEXT ,
2020-08-19 13:51:50 +02:00
GetDisplayStrings : func ( ) [ ] [ ] string {
2020-08-22 03:05:37 +02:00
return presentation . GetRemoteBranchListDisplayStrings ( gui . State . RemoteBranches , gui . State . Modes . Diffing . Ref )
2020-08-19 13:51:50 +02:00
} ,
2020-08-22 07:56:30 +02:00
SelectedItem : func ( ) ( ListItem , bool ) {
item := gui . getSelectedRemoteBranch ( )
return item , item != nil
} ,
2020-08-16 01:18:57 +02:00
}
}
2020-08-17 13:58:30 +02:00
func ( gui * Gui ) tagsListContext ( ) * ListContext {
return & ListContext {
2020-08-23 01:42:30 +02:00
ViewName : "branches" ,
ContextKey : TAGS_CONTEXT_KEY ,
GetItemsLength : func ( ) int { return len ( gui . State . Tags ) } ,
GetPanelState : func ( ) IListPanelState { return gui . State . Panels . Tags } ,
OnFocus : gui . handleTagSelect ,
Gui : gui ,
ResetMainViewOriginOnFocus : true ,
Kind : SIDE_CONTEXT ,
2020-08-19 13:51:50 +02:00
GetDisplayStrings : func ( ) [ ] [ ] string {
2020-08-22 03:05:37 +02:00
return presentation . GetTagListDisplayStrings ( gui . State . Tags , gui . State . Modes . Diffing . Ref )
2020-08-19 13:51:50 +02:00
} ,
2020-08-22 07:56:30 +02:00
SelectedItem : func ( ) ( ListItem , bool ) {
item := gui . getSelectedTag ( )
return item , item != nil
} ,
2020-08-16 01:18:57 +02:00
}
}
2020-08-17 13:58:30 +02:00
func ( gui * Gui ) branchCommitsListContext ( ) * ListContext {
return & ListContext {
2020-08-23 01:42:30 +02:00
ViewName : "commits" ,
ContextKey : BRANCH_COMMITS_CONTEXT_KEY ,
GetItemsLength : func ( ) int { return len ( gui . State . Commits ) } ,
GetPanelState : func ( ) IListPanelState { return gui . State . Panels . Commits } ,
OnFocus : gui . handleCommitSelect ,
OnClickSelectedItem : gui . handleViewCommitFiles ,
Gui : gui ,
ResetMainViewOriginOnFocus : true ,
Kind : SIDE_CONTEXT ,
2020-08-19 13:51:50 +02:00
GetDisplayStrings : func ( ) [ ] [ ] string {
2020-08-22 03:57:44 +02:00
return presentation . GetCommitListDisplayStrings ( gui . State . Commits , gui . State . ScreenMode != SCREEN_NORMAL , gui . cherryPickedCommitShaMap ( ) , gui . State . Modes . Diffing . Ref )
2020-08-19 13:51:50 +02:00
} ,
2020-08-22 07:56:30 +02:00
SelectedItem : func ( ) ( ListItem , bool ) {
item := gui . getSelectedLocalCommit ( )
return item , item != nil
} ,
2020-08-16 01:18:57 +02:00
}
}
2020-08-17 13:58:30 +02:00
func ( gui * Gui ) reflogCommitsListContext ( ) * ListContext {
return & ListContext {
2020-08-23 01:42:30 +02:00
ViewName : "commits" ,
ContextKey : REFLOG_COMMITS_CONTEXT_KEY ,
GetItemsLength : func ( ) int { return len ( gui . State . FilteredReflogCommits ) } ,
GetPanelState : func ( ) IListPanelState { return gui . State . Panels . ReflogCommits } ,
OnFocus : gui . handleReflogCommitSelect ,
Gui : gui ,
ResetMainViewOriginOnFocus : true ,
Kind : SIDE_CONTEXT ,
2020-08-19 13:51:50 +02:00
GetDisplayStrings : func ( ) [ ] [ ] string {
2020-08-22 03:57:44 +02:00
return presentation . GetReflogCommitListDisplayStrings ( gui . State . FilteredReflogCommits , gui . State . ScreenMode != SCREEN_NORMAL , gui . cherryPickedCommitShaMap ( ) , gui . State . Modes . Diffing . Ref )
2020-08-19 13:51:50 +02:00
} ,
2020-08-22 07:56:30 +02:00
SelectedItem : func ( ) ( ListItem , bool ) {
item := gui . getSelectedReflogCommit ( )
return item , item != nil
} ,
2020-08-22 00:49:02 +02:00
}
}
func ( gui * Gui ) subCommitsListContext ( ) * ListContext {
return & ListContext {
2020-08-23 01:42:30 +02:00
ViewName : "branches" ,
ContextKey : SUB_COMMITS_CONTEXT_KEY ,
GetItemsLength : func ( ) int { return len ( gui . State . SubCommits ) } ,
GetPanelState : func ( ) IListPanelState { return gui . State . Panels . SubCommits } ,
OnFocus : gui . handleSubCommitSelect ,
Gui : gui ,
ResetMainViewOriginOnFocus : true ,
Kind : SIDE_CONTEXT ,
2020-08-22 00:49:02 +02:00
GetDisplayStrings : func ( ) [ ] [ ] string {
2020-08-22 03:57:44 +02:00
return presentation . GetCommitListDisplayStrings ( gui . State . SubCommits , gui . State . ScreenMode != SCREEN_NORMAL , gui . cherryPickedCommitShaMap ( ) , gui . State . Modes . Diffing . Ref )
2020-08-22 00:49:02 +02:00
} ,
2020-08-22 07:56:30 +02:00
SelectedItem : func ( ) ( ListItem , bool ) {
item := gui . getSelectedSubCommit ( )
return item , item != nil
} ,
2020-08-16 01:18:57 +02:00
}
}
2020-08-17 13:58:30 +02:00
func ( gui * Gui ) stashListContext ( ) * ListContext {
return & ListContext {
2020-08-23 01:42:30 +02:00
ViewName : "stash" ,
ContextKey : STASH_CONTEXT_KEY ,
GetItemsLength : func ( ) int { return len ( gui . State . StashEntries ) } ,
GetPanelState : func ( ) IListPanelState { return gui . State . Panels . Stash } ,
OnFocus : gui . handleStashEntrySelect ,
Gui : gui ,
ResetMainViewOriginOnFocus : true ,
Kind : SIDE_CONTEXT ,
2020-08-19 13:51:50 +02:00
GetDisplayStrings : func ( ) [ ] [ ] string {
2020-08-22 03:05:37 +02:00
return presentation . GetStashEntryListDisplayStrings ( gui . State . StashEntries , gui . State . Modes . Diffing . Ref )
2020-08-19 13:51:50 +02:00
} ,
2020-08-22 07:56:30 +02:00
SelectedItem : func ( ) ( ListItem , bool ) {
item := gui . getSelectedStashEntry ( )
return item , item != nil
} ,
2020-08-16 01:18:57 +02:00
}
}
2020-08-17 13:58:30 +02:00
func ( gui * Gui ) commitFilesListContext ( ) * ListContext {
return & ListContext {
2020-08-23 01:42:30 +02:00
ViewName : "commitFiles" ,
WindowName : "commits" ,
ContextKey : COMMIT_FILES_CONTEXT_KEY ,
2021-03-31 14:26:53 +02:00
GetItemsLength : func ( ) int { return gui . State . CommitFileManager . GetItemsLength ( ) } ,
2020-08-23 01:42:30 +02:00
GetPanelState : func ( ) IListPanelState { return gui . State . Panels . CommitFiles } ,
OnFocus : gui . handleCommitFileSelect ,
Gui : gui ,
ResetMainViewOriginOnFocus : true ,
Kind : SIDE_CONTEXT ,
2020-08-19 13:51:50 +02:00
GetDisplayStrings : func ( ) [ ] [ ] string {
2021-03-31 14:26:53 +02:00
if gui . State . CommitFileManager . GetItemsLength ( ) == 0 {
2021-03-31 13:08:55 +02:00
return [ ] [ ] string { { utils . ColoredString ( "(none)" , color . FgRed ) } }
}
2021-03-31 14:26:53 +02:00
lines := gui . State . CommitFileManager . Render ( gui . State . Modes . Diffing . Ref , gui . GitCommand . PatchManager )
2021-03-31 13:08:55 +02:00
mappedLines := make ( [ ] [ ] string , len ( lines ) )
for i , line := range lines {
mappedLines [ i ] = [ ] string { line }
}
return mappedLines
2020-08-19 13:51:50 +02:00
} ,
2020-08-22 07:56:30 +02:00
SelectedItem : func ( ) ( ListItem , bool ) {
2021-03-31 13:08:55 +02:00
item := gui . getSelectedCommitFileNode ( )
2020-08-22 07:56:30 +02:00
return item , item != nil
} ,
2020-08-16 01:18:57 +02:00
}
2019-11-17 08:23:06 +02:00
}
2020-09-30 00:27:23 +02:00
func ( gui * Gui ) submodulesListContext ( ) * ListContext {
return & ListContext {
ViewName : "files" ,
WindowName : "files" ,
ContextKey : SUBMODULES_CONTEXT_KEY ,
GetItemsLength : func ( ) int { return len ( gui . State . Submodules ) } ,
GetPanelState : func ( ) IListPanelState { return gui . State . Panels . Submodules } ,
OnFocus : gui . handleSubmoduleSelect ,
Gui : gui ,
ResetMainViewOriginOnFocus : true ,
Kind : SIDE_CONTEXT ,
GetDisplayStrings : func ( ) [ ] [ ] string {
return presentation . GetSubmoduleListDisplayStrings ( gui . State . Submodules )
} ,
SelectedItem : func ( ) ( ListItem , bool ) {
item := gui . getSelectedSubmodule ( )
return item , item != nil
} ,
}
}
2020-11-28 04:14:48 +02:00
func ( gui * Gui ) suggestionsListContext ( ) * ListContext {
return & ListContext {
ViewName : "suggestions" ,
WindowName : "suggestions" ,
ContextKey : SUGGESTIONS_CONTEXT_KEY ,
GetItemsLength : func ( ) int { return len ( gui . State . Suggestions ) } ,
GetPanelState : func ( ) IListPanelState { return gui . State . Panels . Suggestions } ,
OnFocus : func ( ) error { return nil } ,
Gui : gui ,
ResetMainViewOriginOnFocus : false ,
Kind : PERSISTENT_POPUP ,
GetDisplayStrings : func ( ) [ ] [ ] string {
return presentation . GetSuggestionListDisplayStrings ( gui . State . Suggestions )
} ,
}
}
2020-08-17 13:58:30 +02:00
func ( gui * Gui ) getListContexts ( ) [ ] * ListContext {
return [ ] * ListContext {
2021-04-03 06:56:11 +02:00
gui . State . Contexts . Menu ,
gui . State . Contexts . Files ,
gui . State . Contexts . Branches ,
gui . State . Contexts . Remotes ,
gui . State . Contexts . RemoteBranches ,
gui . State . Contexts . Tags ,
gui . State . Contexts . BranchCommits ,
gui . State . Contexts . BranchCommits ,
gui . State . Contexts . ReflogCommits ,
gui . State . Contexts . SubCommits ,
gui . State . Contexts . Stash ,
gui . State . Contexts . CommitFiles ,
gui . State . Contexts . Submodules ,
gui . State . Contexts . Suggestions ,
2019-11-16 05:00:27 +02:00
}
}
2020-08-17 14:05:15 +02:00
func ( gui * Gui ) getListContextKeyBindings ( ) [ ] * Binding {
bindings := make ( [ ] * Binding , 0 )
2020-10-03 06:54:55 +02:00
keybindingConfig := gui . Config . GetUserConfig ( ) . Keybinding
2020-08-17 14:05:15 +02:00
for _ , listContext := range gui . getListContexts ( ) {
2021-04-03 02:32:14 +02:00
listContext := listContext
2020-08-17 14:05:15 +02:00
bindings = append ( bindings , [ ] * Binding {
2021-04-04 15:51:59 +02:00
{ ViewName : listContext . ViewName , Tag : "navigation" , Contexts : [ ] string { string ( listContext . ContextKey ) } , Key : gui . getKey ( keybindingConfig . Universal . PrevItemAlt ) , Modifier : gocui . ModNone , Handler : listContext . handlePrevLine } ,
{ ViewName : listContext . ViewName , Tag : "navigation" , Contexts : [ ] string { string ( listContext . ContextKey ) } , Key : gui . getKey ( keybindingConfig . Universal . PrevItem ) , Modifier : gocui . ModNone , Handler : listContext . handlePrevLine } ,
{ ViewName : listContext . ViewName , Tag : "navigation" , Contexts : [ ] string { string ( listContext . ContextKey ) } , Key : gocui . MouseWheelUp , Modifier : gocui . ModNone , Handler : listContext . handlePrevLine } ,
{ ViewName : listContext . ViewName , Tag : "navigation" , Contexts : [ ] string { string ( listContext . ContextKey ) } , Key : gui . getKey ( keybindingConfig . Universal . NextItemAlt ) , Modifier : gocui . ModNone , Handler : listContext . handleNextLine } ,
{ ViewName : listContext . ViewName , Tag : "navigation" , Contexts : [ ] string { string ( listContext . ContextKey ) } , Key : gui . getKey ( keybindingConfig . Universal . NextItem ) , Modifier : gocui . ModNone , Handler : listContext . handleNextLine } ,
{ ViewName : listContext . ViewName , Tag : "navigation" , Contexts : [ ] string { string ( listContext . ContextKey ) } , Key : gui . getKey ( keybindingConfig . Universal . PrevPage ) , Modifier : gocui . ModNone , Handler : listContext . handlePrevPage , Description : gui . Tr . LcPrevPage } ,
{ ViewName : listContext . ViewName , Tag : "navigation" , Contexts : [ ] string { string ( listContext . ContextKey ) } , Key : gui . getKey ( keybindingConfig . Universal . NextPage ) , Modifier : gocui . ModNone , Handler : listContext . handleNextPage , Description : gui . Tr . LcNextPage } ,
{ ViewName : listContext . ViewName , Tag : "navigation" , Contexts : [ ] string { string ( listContext . ContextKey ) } , Key : gui . getKey ( keybindingConfig . Universal . GotoTop ) , Modifier : gocui . ModNone , Handler : listContext . handleGotoTop , Description : gui . Tr . LcGotoTop } ,
{ ViewName : listContext . ViewName , Tag : "navigation" , Contexts : [ ] string { string ( listContext . ContextKey ) } , Key : gocui . MouseWheelDown , Modifier : gocui . ModNone , Handler : listContext . handleNextLine } ,
{ ViewName : listContext . ViewName , Contexts : [ ] string { string ( listContext . ContextKey ) } , Key : gocui . MouseLeft , Modifier : gocui . ModNone , Handler : listContext . handleClick } ,
2020-08-17 14:05:15 +02:00
} ... )
// the commits panel needs to lazyload things so it has a couple of its own handlers
openSearchHandler := gui . handleOpenSearch
gotoBottomHandler := listContext . handleGotoBottom
if listContext . ViewName == "commits" {
openSearchHandler = gui . handleOpenSearchForCommitsPanel
gotoBottomHandler = gui . handleGotoBottomForCommitsPanel
}
bindings = append ( bindings , [ ] * Binding {
{
ViewName : listContext . ViewName ,
2021-04-04 15:51:59 +02:00
Contexts : [ ] string { string ( listContext . ContextKey ) } ,
2020-10-03 06:54:55 +02:00
Key : gui . getKey ( keybindingConfig . Universal . StartSearch ) ,
2021-04-02 10:20:40 +02:00
Handler : func ( ) error { return openSearchHandler ( listContext . ViewName ) } ,
2020-10-04 02:00:48 +02:00
Description : gui . Tr . LcStartSearch ,
2020-10-01 23:32:48 +02:00
Tag : "navigation" ,
2020-08-17 14:05:15 +02:00
} ,
{
ViewName : listContext . ViewName ,
2021-04-04 15:51:59 +02:00
Contexts : [ ] string { string ( listContext . ContextKey ) } ,
2020-10-03 06:54:55 +02:00
Key : gui . getKey ( keybindingConfig . Universal . GotoBottom ) ,
2020-08-17 14:05:15 +02:00
Handler : gotoBottomHandler ,
2020-10-04 02:00:48 +02:00
Description : gui . Tr . LcGotoBottom ,
2020-10-01 23:32:48 +02:00
Tag : "navigation" ,
2020-08-17 14:05:15 +02:00
} ,
} ... )
}
return bindings
}