From 023a39a9037c4910be54f12edf8a0a34f7f6255c Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sat, 19 May 2018 11:16:34 +1000 Subject: [PATCH] initial commit --- gitcommands.go | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++ gui.go | 52 +++++++++++++++++++++++++++ main.go | 11 ++++++ 3 files changed, 159 insertions(+) create mode 100644 gitcommands.go create mode 100644 gui.go create mode 100644 main.go diff --git a/gitcommands.go b/gitcommands.go new file mode 100644 index 000000000..cbcd00e9f --- /dev/null +++ b/gitcommands.go @@ -0,0 +1,96 @@ +// Go has various value types including strings, +// integers, floats, booleans, etc. Here are a few +// basic examples. + +package gitgot + +import ( + "fmt" + // "log" + "os/exec" + "os" + "strings" + "regexp" + "runtime" +) + +// Map (from https://gobyexample.com/collection-functions) +func Map(vs []string, f func(string) string) []string { + vsm := make([]string, len(vs)) + for i, v := range vs { + vsm[i] = f(v) + } + return vsm +} + +func sanitisedFileString(fileString string) string { + r := regexp.MustCompile("\\s| \\(new commits\\)|.* ") + fileString = r.ReplaceAllString(fileString, "") + return fileString +} + +func filesByMatches(statusString string, targets []string) []string { + files := make([]string, 0) + for _, target := range targets { + if strings.Index(statusString, target) == -1 { + continue + } + r := regexp.MustCompile("(?s)" + target + ".*?\n\n(.*?)\n\n") + // fmt.Println(r) + + matchedFileStrings := strings.Split(r.FindStringSubmatch(statusString)[1], "\n") + // fmt.Println(matchedFileStrings) + + matchedFiles := Map(matchedFileStrings, sanitisedFileString) + // fmt.Println(matchedFiles) + files = append(files, matchedFiles...) + + } + + breakHere() + + // fmt.Println(files) + return files +} + +func breakHere() { + if len(os.Args) > 1 && os.Args[1] == "debug" { + runtime.Breakpoint() + } +} + +func filesToStage(statusString string) []string { + targets := []string{"Changes not staged for commit:", "Untracked files:"} + return filesByMatches(statusString, targets) +} + +func filesToUnstage(statusString string) []string { + targets := []string{"Changes to be committed:"} + return filesByMatches(statusString, targets) +} + +func runCommand(cmd string) (string, error) { + splitCmd := strings.Split(cmd, " ") + // fmt.Println(splitCmd) + + cmdOut, err := exec.Command(splitCmd[0], splitCmd[1:]...).Output() + + // if err != nil { + // os.Exit(1) + // } + + return string(cmdOut), err +} + +func testGettingFiles() { + + statusString, _ := runCommand("git status") + fmt.Println(filesToStage(statusString)) + fmt.Println(filesToUnstage(statusString)) + + runCommand("git add hello-world.go") + + +} + + diff --git a/gui.go b/gui.go new file mode 100644 index 000000000..aa1eb3e40 --- /dev/null +++ b/gui.go @@ -0,0 +1,52 @@ +package gitgot + +import ( + "fmt" + "log" + "github.com/jroimartin/gocui" +) + +func main() { + g, err := gocui.NewGui(gocui.OutputNormal) + if err != nil { + log.Panicln(err) + } + defer g.Close() + + g.SetManagerFunc(layout) + + if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil { + log.Panicln(err) + } + + if err := g.SetKeybinding("", 'a', gocui.ModNone, quit); err != nil { + log.Panicln(err) + } + + if err := g.MainLoop(); err != nil && err != gocui.ErrQuit { + log.Panicln(err) + } +} + +func layout(g *gocui.Gui) error { + maxX, maxY := g.Size() + if v, err := g.SetView("hello", maxX/2-7, maxY/2, maxX/2+7, maxY/2+2); err != nil { + if err != gocui.ErrUnknownView { + return err + } + fmt.Fprintln(v, "Hello world!") + } + + if v2, err := g.SetView("hello2", maxX/2-7, maxY/2, maxX/2+7, maxY/2+2); err != nil { + if err != gocui.ErrUnknownView { + return err + } + fmt.Fprintln(v2, "Hello world 2!") + } + + return nil +} + +func quit(g *gocui.Gui, v *gocui.View) error { + return gocui.ErrQuit +} diff --git a/main.go b/main.go new file mode 100644 index 000000000..668dd1c08 --- /dev/null +++ b/main.go @@ -0,0 +1,11 @@ +package main + +import ( + "./gitgot" +) + +func main() { + testGettingFiles() +} + +