2018-08-14 11:05:26 +02:00
package gui
2018-05-26 05:23:39 +02:00
import (
2018-07-21 07:51:18 +02:00
"fmt"
"sort"
"strings"
2020-03-26 14:48:11 +02:00
"sync"
2018-05-26 05:23:39 +02:00
2018-07-21 07:51:18 +02:00
"github.com/jesseduffield/gocui"
2018-08-19 13:20:50 +02:00
"github.com/jesseduffield/lazygit/pkg/utils"
2018-08-16 07:53:53 +02:00
"github.com/spkg/bom"
2018-05-26 05:23:39 +02:00
)
2020-08-16 05:58:29 +02:00
func ( gui * Gui ) getCyclableWindows ( ) [ ] string {
2020-05-17 13:54:51 +02:00
return [ ] string { "status" , "files" , "branches" , "commits" , "stash" }
}
2018-06-06 04:17:49 +02:00
2020-03-28 02:32:31 +02:00
// models/views that we can refresh
2021-03-31 14:55:06 +02:00
type RefreshableView int
2020-03-27 10:12:15 +02:00
const (
2021-03-31 14:55:06 +02:00
COMMITS RefreshableView = iota
2020-03-27 10:12:15 +02:00
BRANCHES
FILES
STASH
REFLOG
TAGS
REMOTES
2020-03-28 00:57:36 +02:00
STATUS
2020-09-30 00:27:23 +02:00
SUBMODULES
2020-03-27 10:12:15 +02:00
)
2021-03-31 14:55:06 +02:00
func getScopeNames ( scopes [ ] RefreshableView ) [ ] string {
scopeNameMap := map [ RefreshableView ] string {
2020-09-30 00:27:23 +02:00
COMMITS : "commits" ,
BRANCHES : "branches" ,
FILES : "files" ,
SUBMODULES : "submodules" ,
STASH : "stash" ,
REFLOG : "reflog" ,
TAGS : "tags" ,
REMOTES : "remotes" ,
STATUS : "status" ,
2020-09-26 02:23:10 +02:00
}
scopeNames := make ( [ ] string , len ( scopes ) )
for i , scope := range scopes {
scopeNames [ i ] = scopeNameMap [ scope ]
}
return scopeNames
}
2021-03-31 14:55:06 +02:00
func getModeName ( mode RefreshMode ) string {
2020-09-26 02:23:10 +02:00
switch mode {
case SYNC :
return "sync"
case ASYNC :
return "async"
case BLOCK_UI :
return "block-ui"
default :
return "unknown mode"
}
}
2021-03-31 14:55:06 +02:00
type RefreshMode int
2020-03-27 10:12:15 +02:00
const (
2021-03-31 14:55:06 +02:00
SYNC RefreshMode = iota // wait until everything is done before returning
ASYNC // return immediately, allowing each independent thing to update itself
BLOCK_UI // wrap code in an update call to ensure UI updates all at once and keybindings aren't executed till complete
2020-03-27 10:12:15 +02:00
)
type refreshOptions struct {
then func ( )
2021-03-31 14:55:06 +02:00
scope [ ] RefreshableView // e.g. []int{COMMITS, BRANCHES}. Leave empty to refresh everything
mode RefreshMode // one of SYNC (default), ASYNC, and BLOCK_UI
2020-03-27 10:12:15 +02:00
}
2021-03-31 14:55:06 +02:00
func arrToMap ( arr [ ] RefreshableView ) map [ RefreshableView ] bool {
output := map [ RefreshableView ] bool { }
2020-03-27 10:12:15 +02:00
for _ , el := range arr {
output [ el ] = true
}
return output
}
func ( gui * Gui ) refreshSidePanels ( options refreshOptions ) error {
2020-09-26 02:23:10 +02:00
if options . scope == nil {
gui . Log . Infof (
"refreshing all scopes in %s mode" ,
getModeName ( options . mode ) ,
)
} else {
gui . Log . Infof (
"refreshing the following scopes in %s mode: %s" ,
getModeName ( options . mode ) ,
strings . Join ( getScopeNames ( options . scope ) , "," ) ,
)
}
2020-03-26 14:48:11 +02:00
wg := sync . WaitGroup { }
2020-03-27 10:12:15 +02:00
f := func ( ) {
2021-03-31 14:55:06 +02:00
var scopeMap map [ RefreshableView ] bool
2020-03-27 10:12:15 +02:00
if len ( options . scope ) == 0 {
2021-03-31 14:55:06 +02:00
scopeMap = arrToMap ( [ ] RefreshableView { COMMITS , BRANCHES , FILES , STASH , REFLOG , TAGS , REMOTES , STATUS } )
2020-03-27 10:12:15 +02:00
} else {
2021-03-31 14:55:06 +02:00
scopeMap = arrToMap ( options . scope )
2020-03-27 10:12:15 +02:00
}
2020-03-28 01:27:34 +02:00
if scopeMap [ COMMITS ] || scopeMap [ BRANCHES ] || scopeMap [ REFLOG ] {
2020-03-27 10:12:15 +02:00
wg . Add ( 1 )
func ( ) {
if options . mode == ASYNC {
2020-11-16 11:38:26 +02:00
go utils . Safe ( func ( ) { _ = gui . refreshCommits ( ) } )
2020-03-27 10:12:15 +02:00
} else {
2020-11-16 11:38:26 +02:00
_ = gui . refreshCommits ( )
2020-03-27 10:12:15 +02:00
}
wg . Done ( )
} ( )
}
2020-03-26 14:48:11 +02:00
2020-09-30 00:27:23 +02:00
if scopeMap [ FILES ] || scopeMap [ SUBMODULES ] {
2020-03-27 10:12:15 +02:00
wg . Add ( 1 )
func ( ) {
if options . mode == ASYNC {
2020-11-16 11:38:26 +02:00
go utils . Safe ( func ( ) { _ = gui . refreshFilesAndSubmodules ( ) } )
2020-03-27 10:12:15 +02:00
} else {
2020-11-16 11:38:26 +02:00
_ = gui . refreshFilesAndSubmodules ( )
2020-03-27 10:12:15 +02:00
}
wg . Done ( )
} ( )
}
if scopeMap [ STASH ] {
wg . Add ( 1 )
func ( ) {
if options . mode == ASYNC {
2020-11-16 11:38:26 +02:00
go utils . Safe ( func ( ) { _ = gui . refreshStashEntries ( ) } )
2020-03-27 10:12:15 +02:00
} else {
2020-11-16 11:38:26 +02:00
_ = gui . refreshStashEntries ( )
2020-03-27 10:12:15 +02:00
}
wg . Done ( )
} ( )
}
2020-03-26 14:48:11 +02:00
2020-03-28 01:27:34 +02:00
if scopeMap [ TAGS ] {
wg . Add ( 1 )
func ( ) {
if options . mode == ASYNC {
2020-11-16 11:38:26 +02:00
go utils . Safe ( func ( ) { _ = gui . refreshTags ( ) } )
2020-03-28 01:27:34 +02:00
} else {
2020-11-16 11:38:26 +02:00
_ = gui . refreshTags ( )
2020-03-28 01:27:34 +02:00
}
wg . Done ( )
} ( )
}
if scopeMap [ REMOTES ] {
wg . Add ( 1 )
func ( ) {
if options . mode == ASYNC {
2020-11-16 11:38:26 +02:00
go utils . Safe ( func ( ) { _ = gui . refreshRemotes ( ) } )
2020-03-28 01:27:34 +02:00
} else {
2020-11-16 11:38:26 +02:00
_ = gui . refreshRemotes ( )
2020-03-28 01:27:34 +02:00
}
wg . Done ( )
} ( )
}
2020-03-27 10:12:15 +02:00
wg . Wait ( )
2020-03-26 14:48:11 +02:00
2020-03-28 01:27:34 +02:00
gui . refreshStatus ( )
2020-03-27 10:12:15 +02:00
if options . then != nil {
options . then ( )
}
}
if options . mode == BLOCK_UI {
gui . g . Update ( func ( g * gocui . Gui ) error {
f ( )
return nil
} )
} else {
f ( )
}
2019-03-11 04:04:08 +02:00
2020-03-26 14:48:11 +02:00
return nil
2018-06-06 04:17:49 +02:00
}
2018-08-14 11:05:26 +02:00
func ( gui * Gui ) resetOrigin ( v * gocui . View ) error {
2019-04-25 21:37:19 +02:00
_ = v . SetCursor ( 0 , 0 )
2018-07-21 07:51:18 +02:00
return v . SetOrigin ( 0 , 0 )
2018-06-09 11:06:33 +02:00
}
2019-01-15 11:12:31 +02:00
func ( gui * Gui ) cleanString ( s string ) string {
2018-12-12 13:34:20 +02:00
output := string ( bom . Clean ( [ ] byte ( s ) ) )
2019-01-15 11:12:31 +02:00
return utils . NormalizeLinefeeds ( output )
}
2020-05-19 10:01:29 +02:00
func ( gui * Gui ) setViewContent ( v * gocui . View , s string ) {
2019-01-15 11:12:31 +02:00
v . Clear ( )
fmt . Fprint ( v , gui . cleanString ( s ) )
2018-12-12 13:34:20 +02:00
}
2019-01-15 11:12:31 +02:00
// renderString resets the origin of a view and sets its content
2020-08-15 08:36:39 +02:00
func ( gui * Gui ) renderString ( viewName , s string ) {
gui . g . Update ( func ( * gocui . Gui ) error {
2020-08-16 10:25:08 +02:00
return gui . renderStringSync ( viewName , s )
2018-07-21 07:51:18 +02:00
} )
2018-05-26 05:23:39 +02:00
}
2020-08-16 10:25:08 +02:00
func ( gui * Gui ) renderStringSync ( viewName , s string ) error {
v , err := gui . g . View ( viewName )
if err != nil {
return nil // return gracefully if view has been deleted
}
if err := v . SetOrigin ( 0 , 0 ) ; err != nil {
return err
}
if err := v . SetCursor ( 0 , 0 ) ; err != nil {
return err
}
gui . setViewContent ( v , s )
return nil
}
2018-08-14 11:05:26 +02:00
func ( gui * Gui ) optionsMapToString ( optionsMap map [ string ] string ) string {
2018-07-21 07:51:18 +02:00
optionsArray := make ( [ ] string , 0 )
for key , description := range optionsMap {
optionsArray = append ( optionsArray , key + ": " + description )
}
sort . Strings ( optionsArray )
return strings . Join ( optionsArray , ", " )
2018-06-09 11:06:33 +02:00
}
2020-08-23 02:50:27 +02:00
func ( gui * Gui ) renderOptionsMap ( optionsMap map [ string ] string ) {
2020-08-15 08:36:39 +02:00
gui . renderString ( "options" , gui . optionsMapToString ( optionsMap ) )
2018-06-09 11:06:33 +02:00
}
2018-07-22 04:07:36 +02:00
2018-08-14 11:05:26 +02:00
func ( gui * Gui ) trimmedContent ( v * gocui . View ) string {
return strings . TrimSpace ( v . Buffer ( ) )
}
2019-02-25 13:11:35 +02:00
func ( gui * Gui ) currentViewName ( ) string {
currentView := gui . g . CurrentView ( )
2020-05-16 04:35:19 +02:00
if currentView == nil {
return ""
}
2018-08-14 11:05:26 +02:00
return currentView . Name ( )
}
2018-09-05 11:07:46 +02:00
2020-08-15 09:23:16 +02:00
func ( gui * Gui ) resizeCurrentPopupPanel ( ) error {
v := gui . g . CurrentView ( )
2020-08-18 14:41:14 +02:00
if v == nil {
return nil
}
2019-03-11 04:04:08 +02:00
if gui . isPopupPanel ( v . Name ( ) ) {
2020-08-15 09:23:16 +02:00
return gui . resizePopupPanel ( v )
2018-09-05 11:07:46 +02:00
}
return nil
}
2020-08-15 09:23:16 +02:00
func ( gui * Gui ) resizePopupPanel ( v * gocui . View ) error {
2018-09-05 11:07:46 +02:00
// If the confirmation panel is already displayed, just resize the width,
// otherwise continue
content := v . Buffer ( )
2020-08-15 09:23:16 +02:00
x0 , y0 , x1 , y1 := gui . getConfirmationPanelDimensions ( v . Wrap , content )
2018-09-05 11:07:46 +02:00
vx0 , vy0 , vx1 , vy1 := v . Dimensions ( )
if vx0 == x0 && vy0 == y0 && vx1 == x1 && vy1 == y1 {
return nil
}
2020-08-15 09:23:16 +02:00
_ , err := gui . g . SetView ( v . Name ( ) , x0 , y0 , x1 , y1 , 0 )
2018-09-05 11:07:46 +02:00
return err
}
2018-12-04 10:50:11 +02:00
2020-08-20 00:52:51 +02:00
func ( gui * Gui ) changeSelectedLine ( panelState IListPanelState , total int , change int ) {
2019-11-16 05:00:27 +02:00
// TODO: find out why we're doing this
2020-08-20 00:52:51 +02:00
line := panelState . GetSelectedLineIdx ( )
if line == - 1 {
2019-11-16 05:00:27 +02:00
return
}
2020-08-20 00:52:51 +02:00
var newLine int
if line + change < 0 {
newLine = 0
} else if line + change >= total {
newLine = total - 1
2018-12-04 10:50:11 +02:00
} else {
2020-08-20 00:52:51 +02:00
newLine = line + change
2018-12-04 10:50:11 +02:00
}
2020-08-20 00:52:51 +02:00
panelState . SetSelectedLineIdx ( newLine )
2018-12-04 10:50:11 +02:00
}
2020-08-20 00:52:51 +02:00
func ( gui * Gui ) refreshSelectedLine ( panelState IListPanelState , total int ) {
line := panelState . GetSelectedLineIdx ( )
if line == - 1 && total > 0 {
panelState . SetSelectedLineIdx ( 0 )
} else if total - 1 < line {
panelState . SetSelectedLineIdx ( total - 1 )
2018-12-04 10:50:11 +02:00
}
}
2018-12-07 09:52:31 +02:00
2020-02-25 11:11:07 +02:00
func ( gui * Gui ) renderDisplayStrings ( v * gocui . View , displayStrings [ ] [ ] string ) {
gui . g . Update ( func ( g * gocui . Gui ) error {
list := utils . RenderDisplayStrings ( displayStrings )
v . Clear ( )
fmt . Fprint ( v , list )
return nil
} )
}
2020-08-23 02:50:27 +02:00
func ( gui * Gui ) globalOptionsMap ( ) map [ string ] string {
2020-10-03 06:54:55 +02:00
keybindingConfig := gui . Config . GetUserConfig ( ) . Keybinding
2020-08-23 02:50:27 +02:00
return map [ string ] string {
2020-10-04 02:00:48 +02:00
fmt . Sprintf ( "%s/%s" , gui . getKeyDisplay ( keybindingConfig . Universal . ScrollUpMain ) , gui . getKeyDisplay ( keybindingConfig . Universal . ScrollDownMain ) ) : gui . Tr . LcScroll ,
fmt . Sprintf ( "%s %s %s %s" , gui . getKeyDisplay ( keybindingConfig . Universal . PrevBlock ) , gui . getKeyDisplay ( keybindingConfig . Universal . NextBlock ) , gui . getKeyDisplay ( keybindingConfig . Universal . PrevItem ) , gui . getKeyDisplay ( keybindingConfig . Universal . NextItem ) ) : gui . Tr . LcNavigate ,
gui . getKeyDisplay ( keybindingConfig . Universal . Return ) : gui . Tr . LcCancel ,
gui . getKeyDisplay ( keybindingConfig . Universal . Quit ) : gui . Tr . LcQuit ,
gui . getKeyDisplay ( keybindingConfig . Universal . OptionMenu ) : gui . Tr . LcMenu ,
"1-5" : gui . Tr . LcJump ,
2020-08-23 02:50:27 +02:00
}
2020-03-29 01:31:34 +02:00
}
2019-03-11 04:04:08 +02:00
func ( gui * Gui ) isPopupPanel ( viewName string ) bool {
return viewName == "commitMessage" || viewName == "credentials" || viewName == "confirmation" || viewName == "menu"
}
2019-02-25 13:11:35 +02:00
func ( gui * Gui ) popupPanelFocused ( ) bool {
2019-03-11 04:04:08 +02:00
return gui . isPopupPanel ( gui . currentViewName ( ) )
2019-02-25 13:11:35 +02:00
}
2019-11-10 07:20:35 +02:00
2020-05-18 14:00:07 +02:00
// secondaryViewFocused tells us whether it appears that the secondary view is focused. The view is actually never focused for real: we just swap the main and secondary views and then you're still focused on the main view so that we can give you access to all its keybindings for free. I will probably regret this design decision soon enough.
func ( gui * Gui ) secondaryViewFocused ( ) bool {
2020-10-07 23:01:04 +02:00
state := gui . State . Panels . LineByLine
return state != nil && state . SecondaryFocused
2020-05-18 14:00:07 +02:00
}
2020-08-17 13:58:30 +02:00
func ( gui * Gui ) clearEditorView ( v * gocui . View ) {
v . Clear ( )
_ = v . SetCursor ( 0 , 0 )
_ = v . SetOrigin ( 0 , 0 )
}
2020-08-19 11:31:58 +02:00
func ( gui * Gui ) onViewTabClick ( viewName string , tabIndex int ) error {
2021-04-03 05:36:22 +02:00
context := gui . State . ViewTabContextMap [ viewName ] [ tabIndex ] . contexts [ 0 ]
2020-08-19 11:31:58 +02:00
2020-11-28 04:14:48 +02:00
return gui . pushContext ( context )
2020-08-19 11:31:58 +02:00
}
2021-04-02 10:20:40 +02:00
func ( gui * Gui ) handleNextTab ( ) error {
v := gui . g . CurrentView ( )
2021-04-02 11:51:52 +02:00
if v == nil {
2021-04-02 10:20:40 +02:00
return nil
}
2020-08-19 11:31:58 +02:00
return gui . onViewTabClick (
v . Name ( ) ,
utils . ModuloWithWrap ( v . TabIndex + 1 , len ( v . Tabs ) ) ,
)
}
2021-04-02 10:20:40 +02:00
func ( gui * Gui ) handlePrevTab ( ) error {
v := gui . g . CurrentView ( )
2021-04-02 11:51:52 +02:00
if v == nil {
2021-04-02 10:20:40 +02:00
return nil
}
2020-08-19 11:31:58 +02:00
return gui . onViewTabClick (
v . Name ( ) ,
utils . ModuloWithWrap ( v . TabIndex - 1 , len ( v . Tabs ) ) ,
)
}
2020-10-01 23:56:14 +02:00
// this is the distance we will move the cursor when paging up or down in a view
func ( gui * Gui ) pageDelta ( view * gocui . View ) int {
_ , height := view . Size ( )
delta := height - 1
if delta == 0 {
return 1
}
return delta
}