2021-04-11 04:12:54 +02:00
package gui
2021-10-24 01:43:48 +02:00
import (
"io"
2022-01-29 10:15:46 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/context"
2021-10-24 01:43:48 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/style"
2022-01-29 10:09:20 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/types"
2021-10-24 01:43:48 +02:00
)
2021-04-11 04:12:54 +02:00
func ( gui * Gui ) handleCreateExtrasMenuPanel ( ) error {
2022-01-29 10:09:20 +02:00
return gui . c . Menu ( types . CreateMenuOptions {
2022-01-16 05:46:53 +02:00
Title : gui . c . Tr . CommandLog ,
2022-01-29 10:09:20 +02:00
Items : [ ] * types . MenuItem {
2022-01-28 11:44:36 +02:00
{
2022-05-08 06:23:32 +02:00
Label : gui . c . Tr . ToggleShowCommandLog ,
2022-01-28 11:44:36 +02:00
OnPress : func ( ) error {
2022-12-30 14:24:24 +02:00
currentContext := gui . c . CurrentStaticContext ( )
2023-03-23 09:47:29 +02:00
if gui . c . State ( ) . GetShowExtrasWindow ( ) && currentContext . GetKey ( ) == context . COMMAND_LOG_CONTEXT_KEY {
2022-02-22 11:17:26 +02:00
if err := gui . c . PopContext ( ) ; err != nil {
2022-01-28 11:44:36 +02:00
return err
}
2021-04-11 07:01:49 +02:00
}
2023-03-23 09:47:29 +02:00
show := ! gui . c . State ( ) . GetShowExtrasWindow ( )
gui . c . State ( ) . SetShowExtrasWindow ( show )
2022-01-16 05:46:53 +02:00
gui . c . GetAppState ( ) . HideCommandLog = ! show
2023-07-28 14:31:14 +02:00
if err := gui . c . SaveAppState ( ) ; err != nil {
gui . c . Log . Errorf ( "error when saving app state: %v" , err )
}
2022-01-28 11:44:36 +02:00
return nil
} ,
} ,
{
2022-05-08 06:23:32 +02:00
Label : gui . c . Tr . FocusCommandLog ,
OnPress : gui . handleFocusCommandLog ,
2021-04-11 04:12:54 +02:00
} ,
} ,
2022-01-28 11:44:36 +02:00
} )
2021-04-11 07:01:49 +02:00
}
func ( gui * Gui ) handleFocusCommandLog ( ) error {
2023-03-23 09:47:29 +02:00
gui . c . State ( ) . SetShowExtrasWindow ( true )
2022-01-16 05:46:53 +02:00
// TODO: is this necessary? Can't I just call 'return from context'?
2022-12-30 14:24:24 +02:00
gui . State . Contexts . CommandLog . SetParentContext ( gui . c . CurrentSideContext ( ) )
2022-01-16 05:46:53 +02:00
return gui . c . PushContext ( gui . State . Contexts . CommandLog )
2021-04-11 07:01:49 +02:00
}
func ( gui * Gui ) scrollUpExtra ( ) error {
gui . Views . Extras . Autoscroll = false
2022-04-16 07:27:56 +02:00
gui . scrollUpView ( gui . Views . Extras )
return nil
2021-04-11 07:01:49 +02:00
}
func ( gui * Gui ) scrollDownExtra ( ) error {
gui . Views . Extras . Autoscroll = false
2022-04-16 07:27:56 +02:00
gui . scrollDownView ( gui . Views . Extras )
2021-04-11 07:01:49 +02:00
return nil
2021-04-11 04:12:54 +02:00
}
2021-10-24 01:43:48 +02:00
func ( gui * Gui ) getCmdWriter ( ) io . Writer {
2022-01-16 05:46:53 +02:00
return & prefixWriter { writer : gui . Views . Extras , prefix : style . FgMagenta . Sprintf ( "\n\n%s\n" , gui . c . Tr . GitOutput ) }
2021-10-24 01:43:48 +02:00
}
// Ensures that the first write is preceded by writing a prefix.
// This allows us to say 'Git output:' before writing the actual git output.
// We could just write directly to the view in this package before running the command but we already have code in the commands package that writes to the same view beforehand (with the command it's about to run) so things would be out of order.
type prefixWriter struct {
prefix string
prefixWritten bool
writer io . Writer
}
2023-06-03 07:49:38 +02:00
func ( self * prefixWriter ) Write ( p [ ] byte ) ( int , error ) {
2021-10-24 01:43:48 +02:00
if ! self . prefixWritten {
self . prefixWritten = true
// assuming we can write this prefix in one go
2023-06-03 07:49:38 +02:00
n , err := self . writer . Write ( [ ] byte ( self . prefix ) )
2021-10-24 01:43:48 +02:00
if err != nil {
2023-06-03 07:49:38 +02:00
return n , err
2021-10-24 01:43:48 +02:00
}
}
return self . writer . Write ( p )
}