2018-05-26 05:23:39 +02:00
// lots of this has been directly ported from one of the example files, will brush up later
// Copyright 2014 The gocui Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
2018-08-14 11:05:26 +02:00
package gui
2018-05-26 05:23:39 +02:00
import (
2018-08-05 14:00:02 +02:00
"strings"
2018-12-05 10:33:46 +02:00
"time"
2018-05-26 05:23:39 +02:00
2018-08-05 14:00:02 +02:00
"github.com/fatih/color"
"github.com/jesseduffield/gocui"
2019-10-18 09:48:37 +02:00
"github.com/jesseduffield/lazygit/pkg/theme"
2018-05-26 05:23:39 +02:00
)
2020-08-15 08:36:39 +02:00
type createPopupPanelOpts struct {
hasLoader bool
editable bool
title string
prompt string
handleConfirm func ( ) error
handleConfirmPrompt func ( string ) error
handleClose func ( ) error
2020-08-23 05:09:36 +02:00
// when handlersManageFocus is true, do not return from the confirmation context automatically. It's expected that the handlers will manage focus, whether that means switching to another context, or manually returning the context.
handlersManageFocus bool
2020-08-15 08:36:39 +02:00
}
2020-08-15 08:38:16 +02:00
type askOpts struct {
2020-08-23 05:09:36 +02:00
title string
prompt string
handleConfirm func ( ) error
handleClose func ( ) error
handlersManageFocus bool
2020-08-15 08:36:39 +02:00
}
func ( gui * Gui ) createLoaderPanel ( currentView * gocui . View , prompt string ) error {
return gui . createPopupPanel ( createPopupPanelOpts {
2020-08-23 04:20:49 +02:00
prompt : prompt ,
hasLoader : true ,
2020-08-15 08:36:39 +02:00
} )
}
2020-08-15 08:38:16 +02:00
func ( gui * Gui ) ask ( opts askOpts ) error {
2020-08-15 08:36:39 +02:00
return gui . createPopupPanel ( createPopupPanelOpts {
2020-08-23 05:09:36 +02:00
title : opts . title ,
prompt : opts . prompt ,
handleConfirm : opts . handleConfirm ,
handleClose : opts . handleClose ,
handlersManageFocus : opts . handlersManageFocus ,
2020-08-15 08:36:39 +02:00
} )
}
2020-08-23 04:20:49 +02:00
func ( gui * Gui ) prompt ( title string , initialContent string , handleConfirm func ( string ) error ) error {
2020-08-15 08:36:39 +02:00
return gui . createPopupPanel ( createPopupPanelOpts {
title : title ,
prompt : initialContent ,
editable : true ,
handleConfirmPrompt : handleConfirm ,
} )
}
2020-08-23 05:09:36 +02:00
func ( gui * Gui ) wrappedConfirmationFunction ( handlersManageFocus bool , function func ( ) error ) func ( * gocui . Gui , * gocui . View ) error {
2018-08-05 14:00:02 +02:00
return func ( g * gocui . Gui , v * gocui . View ) error {
if function != nil {
2020-08-15 08:36:39 +02:00
if err := function ( ) ; err != nil {
return err
}
}
2020-08-23 05:09:36 +02:00
if err := gui . closeConfirmationPrompt ( handlersManageFocus ) ; err != nil {
return err
}
return nil
2020-08-15 08:36:39 +02:00
}
}
2020-08-23 05:09:36 +02:00
func ( gui * Gui ) wrappedPromptConfirmationFunction ( handlersManageFocus bool , function func ( string ) error ) func ( * gocui . Gui , * gocui . View ) error {
2020-08-15 08:36:39 +02:00
return func ( g * gocui . Gui , v * gocui . View ) error {
if function != nil {
if err := function ( v . Buffer ( ) ) ; err != nil {
2020-08-16 14:01:14 +02:00
return gui . surfaceError ( err )
2018-08-05 14:00:02 +02:00
}
}
2019-11-17 03:02:39 +02:00
2020-08-23 05:09:36 +02:00
if err := gui . closeConfirmationPrompt ( handlersManageFocus ) ; err != nil {
return err
}
return nil
2018-08-05 14:00:02 +02:00
}
2018-05-27 08:32:09 +02:00
}
2020-08-23 05:09:36 +02:00
func ( gui * Gui ) deleteConfirmationView ( ) {
gui . g . DeleteKeybinding ( "confirmation" , gui . getKey ( "universal.confirm" ) , gocui . ModNone )
gui . g . DeleteKeybinding ( "confirmation" , gui . getKey ( "universal.confirm-alt1" ) , gocui . ModNone )
gui . g . DeleteKeybinding ( "confirmation" , gui . getKey ( "universal.return" ) , gocui . ModNone )
_ = gui . g . DeleteView ( "confirmation" )
}
func ( gui * Gui ) closeConfirmationPrompt ( handlersManageFocus bool ) error {
2020-08-16 05:58:29 +02:00
view := gui . getConfirmationView ( )
if view == nil {
2018-12-07 09:52:31 +02:00
return nil // if it's already been closed we can just return
2018-08-05 14:00:02 +02:00
}
2020-08-17 09:53:50 +02:00
2020-08-23 05:09:36 +02:00
if ! handlersManageFocus {
if err := gui . returnFromContext ( ) ; err != nil {
return err
}
}
2020-08-17 09:53:50 +02:00
2020-08-23 05:09:36 +02:00
gui . deleteConfirmationView ( )
return nil
2018-05-27 08:32:09 +02:00
}
2019-01-15 10:33:42 +02:00
func ( gui * Gui ) getMessageHeight ( wrap bool , message string , width int ) int {
2018-08-05 14:00:02 +02:00
lines := strings . Split ( message , "\n" )
lineCount := 0
2018-12-26 13:39:16 +02:00
// if we need to wrap, calculate height to fit content within view's width
2019-01-15 10:33:42 +02:00
if wrap {
2018-12-26 13:39:16 +02:00
for _ , line := range lines {
lineCount += len ( line ) / width + 1
}
} else {
lineCount = len ( lines )
2018-08-05 14:00:02 +02:00
}
return lineCount
2018-05-26 05:23:39 +02:00
}
2020-08-15 09:23:16 +02:00
func ( gui * Gui ) getConfirmationPanelDimensions ( wrap bool , prompt string ) ( int , int , int , int ) {
width , height := gui . g . Size ( )
2020-08-12 13:48:29 +02:00
// we want a minimum width up to a point, then we do it based on ratio.
2019-11-10 13:07:45 +02:00
panelWidth := 4 * width / 7
2020-08-12 13:48:29 +02:00
minWidth := 80
if panelWidth < minWidth {
if width - 2 < minWidth {
panelWidth = width - 2
} else {
panelWidth = minWidth
}
}
2019-01-15 10:33:42 +02:00
panelHeight := gui . getMessageHeight ( wrap , prompt , panelWidth )
2020-03-26 12:39:59 +02:00
if panelHeight > height * 3 / 4 {
panelHeight = height * 3 / 4
}
2018-08-05 14:00:02 +02:00
return width / 2 - panelWidth / 2 ,
height / 2 - panelHeight / 2 - panelHeight % 2 - 1 ,
width / 2 + panelWidth / 2 ,
height / 2 + panelHeight / 2
2018-05-26 05:23:39 +02:00
}
2020-08-23 04:20:49 +02:00
func ( gui * Gui ) prepareConfirmationPanel ( title , prompt string , hasLoader bool ) ( * gocui . View , error ) {
2020-08-15 09:23:16 +02:00
x0 , y0 , x1 , y1 := gui . getConfirmationPanelDimensions ( true , prompt )
2018-08-25 07:55:49 +02:00
confirmationView , err := gui . g . SetView ( "confirmation" , x0 , y0 , x1 , y1 , 0 )
if err != nil {
2019-02-16 03:03:22 +02:00
if err . Error ( ) != "unknown view" {
2018-08-25 07:55:49 +02:00
return nil , err
2018-08-05 14:00:02 +02:00
}
2019-02-16 03:03:22 +02:00
confirmationView . HasLoader = hasLoader
2020-01-31 15:06:07 +02:00
if hasLoader {
gui . g . StartTicking ( )
}
2018-08-05 14:00:02 +02:00
confirmationView . Title = title
2018-12-05 10:33:46 +02:00
confirmationView . Wrap = true
2019-10-18 09:48:37 +02:00
confirmationView . FgColor = theme . GocuiDefaultTextColor
2018-08-05 14:00:02 +02:00
}
2018-12-07 09:52:31 +02:00
gui . g . Update ( func ( g * gocui . Gui ) error {
2020-08-16 05:58:29 +02:00
return gui . switchContext ( gui . Contexts . Confirmation . Context )
2018-12-07 09:52:31 +02:00
} )
2018-08-25 07:55:49 +02:00
return confirmationView , nil
2018-05-27 08:32:09 +02:00
}
2020-08-15 08:36:39 +02:00
func ( gui * Gui ) createPopupPanel ( opts createPopupPanelOpts ) error {
gui . g . Update ( func ( g * gocui . Gui ) error {
2018-08-05 14:00:02 +02:00
// delete the existing confirmation panel if it exists
if view , _ := g . View ( "confirmation" ) ; view != nil {
2020-08-23 05:09:36 +02:00
gui . deleteConfirmationView ( )
2018-08-05 14:00:02 +02:00
}
2020-08-23 04:20:49 +02:00
confirmationView , err := gui . prepareConfirmationPanel ( opts . title , opts . prompt , opts . hasLoader )
2018-08-25 07:55:49 +02:00
if err != nil {
return err
2018-08-05 14:00:02 +02:00
}
2020-08-15 08:36:39 +02:00
confirmationView . Editable = opts . editable
if opts . editable {
2019-11-17 08:58:24 +02:00
go func ( ) {
// TODO: remove this wait (right now if you remove it the EditGotoToEndOfLine method doesn't seem to work)
time . Sleep ( time . Millisecond )
gui . g . Update ( func ( g * gocui . Gui ) error {
confirmationView . EditGotoToEndOfLine ( )
return nil
} )
} ( )
}
2020-08-15 08:36:39 +02:00
gui . renderString ( "confirmation" , opts . prompt )
return gui . setKeyBindings ( opts )
2018-08-05 14:00:02 +02:00
} )
return nil
2018-06-05 10:47:57 +02:00
}
2020-08-15 08:36:39 +02:00
func ( gui * Gui ) setKeyBindings ( opts createPopupPanelOpts ) error {
2018-08-15 10:53:05 +02:00
actions := gui . Tr . TemplateLocalize (
"CloseConfirm" ,
2018-08-16 11:31:03 +02:00
Teml {
2018-08-15 10:53:05 +02:00
"keyBindClose" : "esc" ,
"keyBindConfirm" : "enter" ,
} ,
)
2020-03-26 12:39:59 +02:00
2020-08-15 08:36:39 +02:00
gui . renderString ( "options" , actions )
2020-08-17 09:53:50 +02:00
var onConfirm func ( * gocui . Gui , * gocui . View ) error
2020-08-15 08:36:39 +02:00
if opts . handleConfirmPrompt != nil {
2020-08-23 05:09:36 +02:00
onConfirm = gui . wrappedPromptConfirmationFunction ( opts . handlersManageFocus , opts . handleConfirmPrompt )
2020-08-15 08:36:39 +02:00
} else {
2020-08-23 05:09:36 +02:00
onConfirm = gui . wrappedConfirmationFunction ( opts . handlersManageFocus , opts . handleConfirm )
2020-08-17 09:53:50 +02:00
}
2020-08-17 12:30:10 +02:00
if err := gui . g . SetKeybinding ( "confirmation" , nil , gui . getKey ( "universal.confirm" ) , gocui . ModNone , onConfirm ) ; err != nil {
return err
}
if err := gui . g . SetKeybinding ( "confirmation" , nil , gui . getKey ( "universal.confirm-alt1" ) , gocui . ModNone , onConfirm ) ; err != nil {
return err
2018-12-16 08:05:34 +02:00
}
2020-08-23 05:09:36 +02:00
return gui . g . SetKeybinding ( "confirmation" , nil , gui . getKey ( "universal.return" ) , gocui . ModNone , gui . wrappedConfirmationFunction ( opts . handlersManageFocus , opts . handleClose ) )
2020-08-15 08:36:39 +02:00
}
func ( gui * Gui ) createErrorPanel ( message string ) error {
2018-08-05 14:00:02 +02:00
colorFunction := color . New ( color . FgRed ) . SprintFunc ( )
coloredMessage := colorFunction ( strings . TrimSpace ( message ) )
2020-03-28 03:52:45 +02:00
if err := gui . refreshSidePanels ( refreshOptions { mode : ASYNC } ) ; err != nil {
return err
}
2020-08-15 08:38:16 +02:00
return gui . ask ( askOpts {
2020-08-23 04:20:49 +02:00
title : gui . Tr . SLocalize ( "Error" ) ,
prompt : coloredMessage ,
2020-08-15 08:36:39 +02:00
} )
2020-03-28 02:47:54 +02:00
}
func ( gui * Gui ) surfaceError ( err error ) error {
return gui . createErrorPanel ( err . Error ( ) )
2018-06-01 15:23:31 +02:00
}