Skip to content
Open
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/charmbracelet/log v0.4.0
github.com/charmbracelet/x/ansi v0.3.2
github.com/muesli/reflow v0.3.0
github.com/urfave/cli/v3 v3.3.8
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/urfave/cli/v3 v3.3.8 h1:BzolUExliMdet9NlJ/u4m5vHSotJ3PzEqSAZ1oPMa/E=
github.com/urfave/cli/v3 v3.3.8/go.mod h1:FJSKtM/9AiiTOJL4fJ6TbMUkxBXn7GO9guZqoZtpYpo=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
Expand Down
35 changes: 29 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bufio"
"context"
"fmt"
"io"
"os"
Expand All @@ -12,22 +13,45 @@ import (
"github.com/charmbracelet/log"
"github.com/charmbracelet/x/ansi"
"github.com/muesli/termenv"
"github.com/urfave/cli/v3"

"github.com/dlvhdr/diffnav/pkg/ui"
)

func main() {
cmd := &cli.Command{
Usage: "A git diff pager with a file tree",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "unified",
Aliases: []string{"u"},
Usage: "side-by-side (default) or unified diff",
HideDefault: true,
},
&cli.BoolFlag{
Name: "debug",
Usage: "log to debug.log",
Sources: cli.EnvVars("DEBUG"),
},
},
Action: runDiffNav,
}
if err := cmd.Run(context.Background(), os.Args); err != nil {
log.Fatal(err)
}
}

func runDiffNav(ctx context.Context, cmd *cli.Command) error {
stat, err := os.Stdin.Stat()
if err != nil {
panic(err)
}

if stat.Mode()&os.ModeNamedPipe == 0 && stat.Size() == 0 {
fmt.Println("No diff, exiting")
os.Exit(0)
}

if os.Getenv("DEBUG") == "true" {
if cmd.Bool("debug") {
var fileErr error
logFile, fileErr := os.OpenFile("debug.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if fileErr != nil {
Expand Down Expand Up @@ -73,9 +97,8 @@ func main() {
fmt.Println("No input provided, exiting")
os.Exit(0)
}
p := tea.NewProgram(ui.New(input), tea.WithMouseAllMotion())
p := tea.NewProgram(ui.New(input, cmd.Bool("unified")), tea.WithMouseAllMotion())

if _, err := p.Run(); err != nil {
log.Fatal(err)
}
_, err = p.Run()
return err
}
4 changes: 2 additions & 2 deletions pkg/ui/mainModel.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ type mainModel struct {
filtered []string
}

func New(input string) mainModel {
func New(input string, unified bool) mainModel {
m := mainModel{input: input, isShowingFileTree: true}
m.fileTree = filetree.New()
m.diffViewer = diffviewer.New()
m.diffViewer = diffviewer.New(unified)

m.help = help.New()
helpSt := lipgloss.NewStyle()
Expand Down
20 changes: 11 additions & 9 deletions pkg/ui/panes/diffviewer/diffviewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ const dirHeaderHeight = 3

type Model struct {
common.Common
vp viewport.Model
buffer *bytes.Buffer
file *gitdiff.File
vp viewport.Model
unified bool
buffer *bytes.Buffer
file *gitdiff.File
}

func New() Model {
func New(unified bool) Model {
return Model{
vp: viewport.Model{},
vp: viewport.Model{},
unified: unified,
}
}

Expand Down Expand Up @@ -68,7 +70,7 @@ func (m *Model) SetSize(width, height int) tea.Cmd {
m.Height = height
m.vp.Width = m.Width
m.vp.Height = m.Height - dirHeaderHeight
return diff(m.file, m.Width)
return diff(m.file, m.Width, m.unified)
}

func (m Model) headerView() string {
Expand Down Expand Up @@ -109,19 +111,19 @@ func (m Model) headerView() string {
func (m Model) SetFilePatch(file *gitdiff.File) (Model, tea.Cmd) {
m.buffer = new(bytes.Buffer)
m.file = file
return m, diff(m.file, m.Width)
return m, diff(m.file, m.Width, m.unified)
}

func (m *Model) GoToTop() {
m.vp.GotoTop()
}

func diff(file *gitdiff.File, width int) tea.Cmd {
func diff(file *gitdiff.File, width int, unified bool) tea.Cmd {
if width == 0 || file == nil {
return nil
}
return func() tea.Msg {
sideBySide := !file.IsNew && !file.IsDelete
sideBySide := !unified && !file.IsNew && !file.IsDelete
args := []string{"--paging=never", fmt.Sprintf("-w=%d", width)}
if sideBySide {
args = append(args, "--side-by-side")
Expand Down