1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/gui/main_panels.go

159 lines
3.2 KiB
Go
Raw Normal View History

package gui
2021-04-04 15:51:59 +02:00
import (
"os/exec"
"github.com/jesseduffield/gocui"
)
type viewUpdateOpts struct {
title string
2020-08-18 14:02:35 +02:00
// awkwardly calling this noWrap because of how hard Go makes it to have
// a boolean option that defaults to true
noWrap bool
highlight bool
task updateTask
}
type refreshMainOpts struct {
main *viewUpdateOpts
secondary *viewUpdateOpts
}
// constants for updateTask's kind field
type TaskKind int
const (
RENDER_STRING TaskKind = iota
2020-08-18 14:02:35 +02:00
RENDER_STRING_WITHOUT_SCROLL
RUN_COMMAND
2020-08-18 14:02:35 +02:00
RUN_PTY
)
2020-08-18 14:02:35 +02:00
type updateTask interface {
GetKind() TaskKind
2020-08-18 14:02:35 +02:00
}
type renderStringTask struct {
str string
}
func (t *renderStringTask) GetKind() TaskKind {
2020-08-18 14:02:35 +02:00
return RENDER_STRING
}
2021-04-04 15:51:59 +02:00
func NewRenderStringTask(str string) *renderStringTask {
2020-08-18 14:02:35 +02:00
return &renderStringTask{str: str}
}
type renderStringWithoutScrollTask struct {
str string
}
func (t *renderStringWithoutScrollTask) GetKind() TaskKind {
2020-08-18 14:02:35 +02:00
return RENDER_STRING_WITHOUT_SCROLL
}
2021-04-04 15:51:59 +02:00
func NewRenderStringWithoutScrollTask(str string) *renderStringWithoutScrollTask {
2020-08-18 14:02:35 +02:00
return &renderStringWithoutScrollTask{str: str}
}
type runCommandTask struct {
cmd *exec.Cmd
prefix string
2020-08-18 14:02:35 +02:00
}
func (t *runCommandTask) GetKind() TaskKind {
2020-08-18 14:02:35 +02:00
return RUN_COMMAND
}
2021-04-04 15:51:59 +02:00
func NewRunCommandTask(cmd *exec.Cmd) *runCommandTask {
2020-08-18 14:02:35 +02:00
return &runCommandTask{cmd: cmd}
}
2021-04-04 15:51:59 +02:00
func NewRunCommandTaskWithPrefix(cmd *exec.Cmd, prefix string) *runCommandTask {
return &runCommandTask{cmd: cmd, prefix: prefix}
}
2020-08-18 14:02:35 +02:00
type runPtyTask struct {
cmd *exec.Cmd
prefix string
2020-08-18 14:02:35 +02:00
}
func (t *runPtyTask) GetKind() TaskKind {
2020-08-18 14:02:35 +02:00
return RUN_PTY
}
2021-04-04 15:51:59 +02:00
func NewRunPtyTask(cmd *exec.Cmd) *runPtyTask {
2020-08-18 14:02:35 +02:00
return &runPtyTask{cmd: cmd}
}
2020-11-16 11:38:26 +02:00
// currently unused
// func (gui *Gui) createRunPtyTaskWithPrefix(cmd *exec.Cmd, prefix string) *runPtyTask {
// return &runPtyTask{cmd: cmd, prefix: prefix}
// }
2021-04-04 16:31:52 +02:00
func (gui *Gui) runTaskForView(view *gocui.View, task updateTask) error {
2020-08-18 14:02:35 +02:00
switch task.GetKind() {
case RENDER_STRING:
specificTask := task.(*renderStringTask)
2021-04-04 16:31:52 +02:00
return gui.newStringTask(view, specificTask.str)
2020-08-18 14:02:35 +02:00
case RENDER_STRING_WITHOUT_SCROLL:
specificTask := task.(*renderStringWithoutScrollTask)
2021-04-04 16:31:52 +02:00
return gui.newStringTaskWithoutScroll(view, specificTask.str)
2020-08-18 14:02:35 +02:00
case RUN_COMMAND:
specificTask := task.(*runCommandTask)
2021-04-04 16:31:52 +02:00
return gui.newCmdTask(view, specificTask.cmd, specificTask.prefix)
2020-08-18 14:02:35 +02:00
case RUN_PTY:
specificTask := task.(*runPtyTask)
2021-04-04 16:31:52 +02:00
return gui.newPtyTask(view, specificTask.cmd, specificTask.prefix)
2020-08-18 14:02:35 +02:00
}
return nil
}
2021-04-04 15:51:59 +02:00
func (gui *Gui) refreshMainView(opts *viewUpdateOpts, view *gocui.View) error {
2020-08-23 01:46:28 +02:00
view.Title = opts.title
view.Wrap = !opts.noWrap
view.Highlight = opts.highlight
2021-04-04 16:31:52 +02:00
if err := gui.runTaskForView(view, opts.task); err != nil {
2020-08-23 01:46:28 +02:00
gui.Log.Error(err)
return nil
}
2020-08-18 14:02:35 +02:00
2020-08-23 01:46:28 +02:00
return nil
}
func (gui *Gui) refreshMainViews(opts refreshMainOpts) error {
if opts.main != nil {
2021-04-04 15:51:59 +02:00
if err := gui.refreshMainView(opts.main, gui.Views.Main); err != nil {
2020-08-23 01:46:28 +02:00
return err
}
}
if opts.secondary != nil {
2021-04-04 15:51:59 +02:00
if err := gui.refreshMainView(opts.secondary, gui.Views.Secondary); err != nil {
2020-08-23 01:46:28 +02:00
return err
}
}
2021-04-04 15:51:59 +02:00
gui.splitMainPanel(opts.secondary != nil)
return nil
}
func (gui *Gui) splitMainPanel(splitMainPanel bool) {
gui.State.SplitMainPanel = splitMainPanel
}
func (gui *Gui) isMainPanelSplit() bool {
return gui.State.SplitMainPanel
}