1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-04 10:34:55 +02:00
lazygit/pkg/gui/pty.go

74 lines
1.6 KiB
Go
Raw Normal View History

2021-09-16 15:38:43 +02:00
//go:build !windows
2020-03-03 13:41:35 +02:00
// +build !windows
2020-03-01 03:30:48 +02:00
package gui
import (
2020-03-03 13:41:35 +02:00
"os/exec"
2021-10-17 10:01:02 +02:00
"strings"
2020-03-03 13:41:35 +02:00
2020-03-25 11:37:10 +02:00
"github.com/creack/pty"
2021-04-04 16:31:52 +02:00
"github.com/jesseduffield/gocui"
2020-03-01 03:30:48 +02:00
)
func (gui *Gui) onResize() error {
if gui.State.Ptmx == nil {
return nil
}
2021-04-04 15:51:59 +02:00
width, height := gui.Views.Main.Size()
2020-03-01 03:30:48 +02:00
if err := pty.Setsize(gui.State.Ptmx, &pty.Winsize{Cols: uint16(width), Rows: uint16(height)}); err != nil {
return err
}
// TODO: handle resizing properly
return nil
}
2020-03-03 13:41:35 +02:00
// Some commands need to output for a terminal to active certain behaviour.
// For example, git won't invoke the GIT_PAGER env var unless it thinks it's
// talking to a terminal. We typically write cmd outputs straight to a view,
// which is just an io.Reader. the pty package lets us wrap a command in a
// pseudo-terminal meaning we'll get the behaviour we want from the underlying
// command.
2021-04-04 16:31:52 +02:00
func (gui *Gui) newPtyTask(view *gocui.View, cmd *exec.Cmd, prefix string) error {
2021-04-04 15:51:59 +02:00
width, _ := gui.Views.Main.Size()
2020-03-03 13:41:35 +02:00
pager := gui.GitCommand.GetPager(width)
if pager == "" {
// if we're not using a custom pager we don't need to use a pty
2021-04-04 16:31:52 +02:00
return gui.newCmdTask(view, cmd, prefix)
2020-03-03 13:41:35 +02:00
}
2021-10-17 10:01:02 +02:00
cmdStr := strings.Join(cmd.Args, " ")
2020-03-03 13:41:35 +02:00
cmd.Env = append(cmd.Env, "GIT_PAGER="+pager)
_, height := view.Size()
_, oy := view.Origin()
manager := gui.getManager(view)
ptmx, err := pty.Start(cmd)
if err != nil {
return err
}
gui.State.Ptmx = ptmx
onClose := func() {
ptmx.Close()
gui.State.Ptmx = nil
}
if err := gui.onResize(); err != nil {
return err
}
2021-10-17 10:01:02 +02:00
if err := manager.NewTask(manager.NewCmdTask(ptmx, cmd, prefix, height+oy+10, onClose), cmdStr); err != nil {
2020-03-03 13:41:35 +02:00
return err
}
return nil
}