-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
81 lines (69 loc) · 1.61 KB
/
main.go
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
package main
import (
"bufio"
"fmt"
"io"
"os"
"strings"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/log"
"github.com/charmbracelet/x/ansi"
"github.com/muesli/termenv"
"github.com/dlvhdr/diffnav/pkg/ui"
)
func main() {
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)
}
p := tea.NewProgram(ui.New(input), tea.WithMouseAllMotion())
if _, err := p.Run(); err != nil {
log.Fatal(err)
}
}