diff --git a/go.mod b/go.mod index 2d0db04..8c81277 100644 --- a/go.mod +++ b/go.mod @@ -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 ( diff --git a/go.sum b/go.sum index ddfcc6b..4290781 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index c9fd82e..0ee95dc 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "bufio" + "context" "fmt" "io" "os" @@ -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 { @@ -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 } diff --git a/pkg/ui/mainModel.go b/pkg/ui/mainModel.go index 2ae11f7..c73b375 100644 --- a/pkg/ui/mainModel.go +++ b/pkg/ui/mainModel.go @@ -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() diff --git a/pkg/ui/panes/diffviewer/diffviewer.go b/pkg/ui/panes/diffviewer/diffviewer.go index 95648bd..548e305 100644 --- a/pkg/ui/panes/diffviewer/diffviewer.go +++ b/pkg/ui/panes/diffviewer/diffviewer.go @@ -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, } } @@ -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 { @@ -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")