forked from dlvhdr/diffnav
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
103 lines (87 loc) · 2.27 KB
/
main.go
File metadata and controls
103 lines (87 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"bufio"
"flag"
"fmt"
"io"
"os"
"strings"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/log"
"github.com/charmbracelet/x/ansi"
zone "github.com/lrstanley/bubblezone"
"github.com/muesli/termenv"
"github.com/dlvhdr/diffnav/pkg/config"
"github.com/dlvhdr/diffnav/pkg/ui"
)
func main() {
// Parse CLI flags
sideBySideFlag := flag.Bool("side-by-side", false, "Force side-by-side diff view")
flag.BoolVar(sideBySideFlag, "s", false, "Force side-by-side diff view (shorthand)")
unifiedFlag := flag.Bool("unified", false, "Force unified diff view")
flag.BoolVar(unifiedFlag, "u", false, "Force unified diff view (shorthand)")
flag.Parse()
zone.NewGlobal()
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" {
var fileErr error
logFile, fileErr := os.OpenFile("debug.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if fileErr != nil {
fmt.Println("Error opening debug.log:", fileErr)
os.Exit(1)
}
defer logFile.Close()
if fileErr == nil {
log.SetOutput(logFile)
log.SetTimeFormat(time.Kitchen)
log.SetReportCaller(true)
log.SetLevel(log.DebugLevel)
log.SetOutput(logFile)
log.SetColorProfile(termenv.TrueColor)
wd, err := os.Getwd()
if err != nil {
fmt.Println("Error getting current working dir", err)
os.Exit(1)
}
log.Debug("🚀 Starting diffnav", "logFile", wd+string(os.PathSeparator)+logFile.Name())
}
}
reader := bufio.NewReader(os.Stdin)
var b strings.Builder
for {
r, _, err := reader.ReadRune()
if err != nil && err == io.EOF {
break
}
_, err = b.WriteRune(r)
if err != nil {
fmt.Println("Error getting input:", err)
os.Exit(1)
}
}
input := ansi.Strip(b.String())
if strings.TrimSpace(input) == "" {
fmt.Println("No input provided, exiting")
os.Exit(0)
}
cfg := config.Load()
// Determine sideBySide: CLI flag > config > default
sideBySide := cfg.UI.SideBySide
if *unifiedFlag {
sideBySide = false
} else if *sideBySideFlag {
sideBySide = true
}
p := tea.NewProgram(ui.New(input, cfg, sideBySide), tea.WithMouseAllMotion())
if _, err := p.Run(); err != nil {
log.Fatal(err)
}
}