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
2022-02-06 06:54:26 +02:00
c * types . HelperCommon
2022-02-05 08:04:10 +02:00
list types . IList
viewTrait * ViewTrait
getDisplayStrings func ( startIdx int , length int ) [ ] [ ] string
}
2022-01-29 10:09:20 +02:00
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
}
2022-02-05 08:04:10 +02:00
func ( self * ListContextTrait ) GetViewTrait ( ) types . IViewTrait {
return self . viewTrait
}
2022-01-29 10:09:20 +02:00
func ( self * ListContextTrait ) FocusLine ( ) {
// we need a way of knowing whether we've rendered to the view yet.
2022-01-30 05:46:46 +02:00
self . viewTrait . FocusPoint ( self . list . GetSelectedLineIdx ( ) )
self . viewTrait . SetFooter ( formatListFooter ( self . list . GetSelectedLineIdx ( ) , self . list . GetItemsLength ( ) ) )
2022-01-29 10:09:20 +02:00
}
func formatListFooter ( selectedLineIdx int , length int ) string {
return fmt . Sprintf ( "%d of %d" , selectedLineIdx + 1 , length )
}
func ( self * ListContextTrait ) HandleFocus ( opts ... types . OnFocusOpts ) error {
self . FocusLine ( )
2022-02-05 08:04:10 +02:00
return self . Context . HandleFocus ( opts ... )
2022-01-29 10:09:20 +02:00
}
2022-02-05 08:04:10 +02:00
func ( self * ListContextTrait ) HandleFocusLost ( ) error {
self . viewTrait . SetOriginX ( 0 )
2022-01-29 10:09:20 +02:00
2022-02-13 03:47:15 +02:00
return self . Context . HandleFocusLost ( )
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 ( )
content := utils . RenderDisplayStrings ( self . getDisplayStrings ( 0 , self . list . GetItemsLength ( ) ) )
self . viewTrait . SetContent ( content )
self . c . Render ( )
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-01-29 10:09:20 +02:00
return self . HandleFocus ( )
}