Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
version: "2"
run:
go: "1.25"
issues:
max-issues-per-linter: 0
max-same-issues: 0
uniq-by-line: false
linters:
enable:
- copyloopvar
Expand Down
29 changes: 0 additions & 29 deletions docs-master/Custom_Pagers.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

Lazygit supports custom pagers, [configured](/docs/Config.md) in the config.yml file (which can be opened by pressing `e` in the Status panel).

Support does not extend to Windows users, because we're making use of a package which doesn't have Windows support. However, see [below](#emulating-custom-pagers-on-windows) for a workaround.

Multiple pagers are supported; you can cycle through them with the `|` key. This can be useful if you usually prefer a particular pager, but want to use a different one for certain kinds of diffs.

Pagers are configured with the `pagers` array in the git section; here's an example for a multi-pager setup (use an empty object `{}` for the default builtin diff display that doesn't use a pager):
Expand Down Expand Up @@ -92,30 +90,3 @@ git:
This can be useful if you also want to use it for diffs on the command line, and it also has the advantage that you can configure it per file type in `.gitattributes`; see https://git-scm.com/docs/gitattributes#_defining_an_external_diff_driver.

`pager`, `externalDiffCommand`, and `useExternalDiffGitConfig` are alternative ways of producing the diff, so a pager entry may use at most one of them.

## Emulating custom pagers on Windows

There is a trick to emulate custom pagers on Windows using a Powershell script configured as an external diff command. It's not perfect, but certainly better than nothing. To do this, save the following script as `lazygit-pager.ps1` at a convenient place on your disk:

```pwsh
#!/usr/bin/env pwsh

$old = $args[1].Replace('\', '/')
$new = $args[4].Replace('\', '/')
$path = $args[0]
git diff --no-index --no-ext-diff $old $new
| %{ $_.Replace($old, $path).Replace($new, $path) }
| delta --width=$env:LAZYGIT_COLUMNS
```

Use the pager of your choice with the arguments you like in the last line of the script. Personally I wouldn't want to use lazygit anymore without delta's `--hyperlinks --hyperlinks-file-link-format="lazygit-edit://{path}:{line}"` args, see [above](#delta).

In your lazygit config, use

```yml
git:
pagers:
- externalDiffCommand: "C:/wherever/lazygit-pager.ps1"
```

The main limitation of this approach compared to a "real" pager is that renames are not displayed correctly; they are shown as if they were modifications of the old file. (This affects only the hunk headers; the diff itself is always correct.)
2 changes: 1 addition & 1 deletion pkg/commands/oscommands/cmd_obj.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (self *CmdObj) ShouldStreamOutput() bool {
}

// when you call this, then call Run(), we'll use a PTY to run the command. Only
// has an effect if StreamOutput() was also called. Ignored on Windows.
// has an effect if StreamOutput() was also called.
func (self *CmdObj) UsePty() *CmdObj {
self.usePty = true

Expand Down
26 changes: 22 additions & 4 deletions pkg/commands/oscommands/cmd_obj_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ type cmdHandler struct {
stdoutPipe io.Reader
stdinPipe io.Writer
close func() error
// wait blocks until the child process exits. Needed as a separate
// field because the pty path on Windows spawns via CreateProcess and
// never runs *exec.Cmd.Start — so cmd.Wait wouldn't work there.
wait func() error
}

func (self *cmdObjRunner) runAndStream(cmdObj *CmdObj) error {
Expand Down Expand Up @@ -274,7 +278,7 @@ func (self *cmdObjRunner) runAndStreamAux(

onRun(handler, cmdWriter)

err = cmd.Wait()
err = handler.wait()

self.log.Infof("%s (%s)", cmdObj.ToString(), time.Since(t))

Expand Down Expand Up @@ -376,9 +380,7 @@ func (self *cmdObjRunner) processOutput(
responseChan := promptUserForCredential(askFor)
if responseChan == nil {
// Returning a nil channel means we should terminate the process.
// We achieve this by closing the pty that it's running in. Note that this won't
// work for the case where we're not running in a pty (i.e. on Windows), but
// in that case we'll never be prompted for credentials, so it's not a concern.
// We achieve this by closing the pty that it's running in.
if err := closeFunc(); err != nil {
self.log.Error(err)
}
Expand Down Expand Up @@ -481,5 +483,21 @@ func (self *cmdObjRunner) getCmdHandlerNonPty(cmd *exec.Cmd) (*cmdHandler, error
stdoutPipe: stdoutReader,
stdinPipe: buf,
close: func() error { return nil },
wait: cmd.Wait,
}, nil
}

func (self *cmdObjRunner) getCmdHandlerPty(cmd *exec.Cmd) (*cmdHandler, error) {
// Size will be adjusted by the caller if it cares; this just avoids a
// zero-size pty.
sp, err := StartPty(cmd, 80, 24)
if err != nil {
return nil, err
}
return &cmdHandler{
stdoutPipe: sp.Pty,
stdinPipe: sp.Pty,
close: sp.Pty.Close,
wait: sp.Wait,
}, nil
}
24 changes: 0 additions & 24 deletions pkg/commands/oscommands/cmd_obj_runner_default.go

This file was deleted.

10 changes: 0 additions & 10 deletions pkg/commands/oscommands/cmd_obj_runner_windows.go

This file was deleted.

6 changes: 3 additions & 3 deletions pkg/commands/oscommands/os_default_platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ func (c *OSCommand) UpdateWindowTitle() error {
// this call is reached on every host; only the Windows build does anything.
func setRawCmdLine(cmd *exec.Cmd, cmdLine string) {}

func TerminateProcessGracefully(cmd *exec.Cmd) error {
if cmd.Process == nil {
func TerminateProcessGracefully(proc *os.Process) error {
if proc == nil {
return nil
}

return cmd.Process.Signal(syscall.SIGTERM)
return proc.Signal(syscall.SIGTERM)
}
2 changes: 1 addition & 1 deletion pkg/commands/oscommands/os_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (c *OSCommand) UpdateWindowTitle() error {
return c.Cmd.NewShell(argString, c.UserConfig().OS.ShellFunctionsFile).Run()
}

func TerminateProcessGracefully(cmd *exec.Cmd) error {
func TerminateProcessGracefully(proc *os.Process) error {
// Signals other than SIGKILL are not supported on Windows
return nil
}
34 changes: 34 additions & 0 deletions pkg/commands/oscommands/pty.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package oscommands

import (
"io"
"os"
)

// Pty is the master side of a pseudo-terminal running a subprocess. The
// concrete implementation is platform-specific: creack/pty on Unix and
// ConPTY on Windows.
type Pty interface {
io.ReadWriteCloser
Resize(cols, rows uint16) error
}

// StartedPty is the result of StartPty.
type StartedPty struct {
// Pty is the master side of the pseudo-terminal; read from it to get
// the child's combined stdout/stderr and write to it to feed stdin.
Pty Pty
// Process is the spawned child. Useful for signalling; on Windows the
// original *exec.Cmd was not Start()ed (ConPTY spawns via
// CreateProcess, not os/exec) so cmd.Process is nil and this is the
// only handle.
Process *os.Process
// Wait blocks until the child exits and returns a non-nil error on a
// nonzero exit status, matching *exec.Cmd.Wait semantics.
Wait func() error
}

// StartPty runs cmd in a pseudo-terminal with the given initial dimensions.
// Implemented per-platform in pty_unix.go / pty_windows.go.
//
// func StartPty(cmd *exec.Cmd, cols, rows uint16) (StartedPty, error)
34 changes: 34 additions & 0 deletions pkg/commands/oscommands/pty_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//go:build !windows

package oscommands

import (
"os"
"os/exec"

creackpty "github.com/creack/pty"
)

type unixPty struct {
master *os.File
}

func (u *unixPty) Read(p []byte) (int, error) { return u.master.Read(p) }
func (u *unixPty) Write(p []byte) (int, error) { return u.master.Write(p) }
func (u *unixPty) Close() error { return u.master.Close() }

func (u *unixPty) Resize(cols, rows uint16) error {
return creackpty.Setsize(u.master, &creackpty.Winsize{Cols: cols, Rows: rows})
}

func StartPty(cmd *exec.Cmd, cols, rows uint16) (StartedPty, error) {
f, err := creackpty.StartWithSize(cmd, &creackpty.Winsize{Cols: cols, Rows: rows})
if err != nil {
return StartedPty{}, err
}
return StartedPty{
Pty: &unixPty{master: f},
Process: cmd.Process,
Wait: cmd.Wait,
}, nil
}
Loading
Loading