2022-01-29 10:09:20 +02:00
package context
import (
"fmt"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils"
)
type ListContextTrait struct {
2022-02-05 08:04:10 +02:00
types . Context
2022-01-29 10:09:20 +02:00
2023-03-23 03:35:07 +02:00
c * ContextCommon
2022-02-05 08:04:10 +02:00
list types . IList
getDisplayStrings func ( startIdx int , length int ) [ ] [ ] string
2023-05-25 09:09:18 +02:00
// Alignment for each column. If nil, the default is left alignment
2023-05-21 04:06:22 +02:00
columnAlignments [ ] utils . Alignment
2023-05-25 09:09:18 +02:00
// Some contexts, like the commit context, will highlight the path from the selected commit
// to its parents, because it's ambiguous otherwise. For these, we need to refresh the viewport
// so that we show the highlighted path.
// TODO: now that we allow scrolling, we should be smarter about what gets refreshed:
// we should find out exactly which lines are now part of the path and refresh those.
// We should also keep track of the previous path and refresh those lines too.
2023-06-01 11:28:26 +02:00
refreshViewportOnChange bool
2022-02-05 08:04:10 +02:00
}
2022-01-29 10:09:20 +02:00
2023-03-23 13:05:25 +02:00
func ( self * ListContextTrait ) IsListContext ( ) { }
2022-02-05 08:04:10 +02:00
func ( self * ListContextTrait ) GetList ( ) types . IList {
return self . list
2022-01-29 10:09:20 +02:00
}
func ( self * ListContextTrait ) FocusLine ( ) {
2022-06-13 03:01:26 +02:00
self . GetViewTrait ( ) . FocusPoint ( self . list . GetSelectedLineIdx ( ) )
2022-04-16 07:59:02 +02:00
self . setFooter ( )
2023-05-25 09:09:18 +02:00
2023-06-01 11:28:26 +02:00
if self . refreshViewportOnChange {
2023-05-25 09:09:18 +02:00
self . refreshViewport ( )
}
}
func ( self * ListContextTrait ) refreshViewport ( ) {
startIdx , length := self . GetViewTrait ( ) . ViewPortYBounds ( )
displayStrings := self . getDisplayStrings ( startIdx , length )
content := utils . RenderDisplayStrings ( displayStrings , nil )
self . GetViewTrait ( ) . SetViewPortContent ( content )
2022-04-16 07:59:02 +02:00
}
func ( self * ListContextTrait ) setFooter ( ) {
2022-06-13 03:01:26 +02:00
self . GetViewTrait ( ) . SetFooter ( formatListFooter ( self . list . GetSelectedLineIdx ( ) , self . list . Len ( ) ) )
2022-01-29 10:09:20 +02:00
}
func formatListFooter ( selectedLineIdx int , length int ) string {
return fmt . Sprintf ( "%d of %d" , selectedLineIdx + 1 , length )
}
2022-06-13 03:01:26 +02:00
func ( self * ListContextTrait ) HandleFocus ( opts types . OnFocusOpts ) error {
2022-01-29 10:09:20 +02:00
self . FocusLine ( )
2022-06-13 03:01:26 +02:00
self . GetViewTrait ( ) . SetHighlight ( self . list . Len ( ) > 0 )
2022-04-15 06:01:13 +02:00
2022-06-13 03:01:26 +02:00
return self . Context . HandleFocus ( opts )
2022-01-29 10:09:20 +02:00
}
2022-06-13 03:01:26 +02:00
func ( self * ListContextTrait ) HandleFocusLost ( opts types . OnFocusLostOpts ) error {
self . GetViewTrait ( ) . SetOriginX ( 0 )
2022-01-29 10:09:20 +02:00
2023-06-01 11:28:26 +02:00
if self . refreshViewportOnChange {
self . refreshViewport ( )
}
2022-06-13 03:01:26 +02:00
return self . Context . HandleFocusLost ( opts )
2022-01-29 10:09:20 +02:00
}
2022-02-05 08:04:10 +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 ( self * ListContextTrait ) HandleRender ( ) error {
self . list . RefreshSelectedIdx ( )
2023-05-21 04:06:22 +02:00
content := utils . RenderDisplayStrings (
self . getDisplayStrings ( 0 , self . list . Len ( ) ) ,
self . columnAlignments ,
)
2022-06-13 03:01:26 +02:00
self . GetViewTrait ( ) . SetContent ( content )
2022-02-05 08:04:10 +02:00
self . c . Render ( )
2022-04-16 07:59:02 +02:00
self . setFooter ( )
2022-01-29 10:09:20 +02:00
return nil
}
func ( self * ListContextTrait ) OnSearchSelect ( selectedLineIdx int ) error {
2022-02-05 08:04:10 +02:00
self . GetList ( ) . SetSelectedLineIdx ( selectedLineIdx )
2022-06-13 03:01:26 +02:00
return self . HandleFocus ( types . OnFocusOpts { } )
2022-01-29 10:09:20 +02:00
}