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

82 lines
1.9 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 (
2021-11-02 07:39:15 +02:00
"io"
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
)
2021-11-22 11:58:40 +02:00
func (gui *Gui) desiredPtySize() *pty.Winsize {
width, height := gui.Views.Main.Size()
return &pty.Winsize{Cols: uint16(width), Rows: uint16(height)}
}
2020-03-01 03:30:48 +02:00
func (gui *Gui) onResize() error {
if gui.State.Ptmx == nil {
return nil
}
2021-11-22 11:58:40 +02:00
// TODO: handle resizing properly: we need to actually clear the main view
// and re-read the output from our pty. Or we could just re-run the original
// command from scratch
if err := pty.Setsize(gui.State.Ptmx, gui.desiredPtySize()); err != nil {
2020-03-01 03:30:48 +02:00
return err
}
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()
2022-01-02 01:34:33 +02:00
pager := gui.GitCommand.Config.GetPager(width)
2020-03-03 13:41:35 +02:00
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)
2021-11-02 07:39:15 +02:00
start := func() (*exec.Cmd, io.Reader) {
2021-11-22 11:58:40 +02:00
ptmx, err := pty.StartWithSize(cmd, gui.desiredPtySize())
2021-11-02 07:39:15 +02:00
if err != nil {
gui.Log.Error(err)
}
gui.State.Ptmx = ptmx
return cmd, ptmx
2020-03-03 13:41:35 +02:00
}
onClose := func() {
2021-11-02 07:39:15 +02:00
gui.State.Ptmx.Close()
2020-03-03 13:41:35 +02:00
gui.State.Ptmx = nil
}
2021-11-02 07:39:15 +02:00
if err := manager.NewTask(manager.NewCmdTask(start, prefix, height+oy+10, onClose), cmdStr); err != nil {
2020-03-03 13:41:35 +02:00
return err
}
return nil
}