logging durations and more stuff

This commit is contained in:
Jesse Duffield 2018-06-02 08:35:49 +10:00
parent a555a75565
commit 103a6fd219
10 changed files with 73 additions and 93 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
development.log development.log
commands.log commands.log
extra/lgit.rb

View file

@ -1,18 +1,6 @@
// lots of this has been directly ported from one of the example files, will brush up later
// Copyright 2014 The gocui Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main package main
import ( import (
// "io"
// "io/ioutil"
// "strings"
"fmt" "fmt"
"github.com/jroimartin/gocui" "github.com/jroimartin/gocui"
@ -41,34 +29,36 @@ func getSelectedBranch(v *gocui.View) Branch {
return state.Branches[lineNumber] return state.Branches[lineNumber]
} }
// may want to standardise how these select methods work
func handleBranchSelect(g *gocui.Gui, v *gocui.View) error { func handleBranchSelect(g *gocui.Gui, v *gocui.View) error {
renderString(g, "options", "space: checkout, f: force checkout") renderString(g, "options", "space: checkout, f: force checkout")
if len(state.Branches) == 0 { if len(state.Branches) == 0 {
return renderString(g, "main", "No branches for this repo") return renderString(g, "main", "No branches for this repo")
} }
// may want to standardise how these select methods work go func() {
lineNumber := getItemPosition(v) lineNumber := getItemPosition(v)
branch := state.Branches[lineNumber] branch := state.Branches[lineNumber]
diff, _ := getBranchDiff(branch.Name, branch.BaseBranch) diff, _ := getBranchDiff(branch.Name, branch.BaseBranch)
if err := renderString(g, "main", diff); err != nil { renderString(g, "main", diff)
return err }()
}
return nil return nil
} }
// refreshStatus is called at the end of this because that's when we can // refreshStatus is called at the end of this because that's when we can
// be sure there is a state.Branches array to pick the current branch from // be sure there is a state.Branches array to pick the current branch from
func refreshBranches(g *gocui.Gui) error { func refreshBranches(g *gocui.Gui) error {
v, err := g.View("branches") g.Update(func(g *gocui.Gui) error {
if err != nil { v, err := g.View("branches")
panic(err) if err != nil {
} panic(err)
state.Branches = getGitBranches() }
v.Clear() state.Branches = getGitBranches()
for _, branch := range state.Branches { v.Clear()
fmt.Fprintln(v, branch.DisplayString) for _, branch := range state.Branches {
} fmt.Fprintln(v, branch.DisplayString)
resetOrigin(v) }
refreshStatus(g) resetOrigin(v)
return refreshStatus(g)
})
return nil return nil
} }

View file

@ -1,9 +1,3 @@
// lots of this has been directly ported from one of the example files, will brush up later
// Copyright 2014 The gocui Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main package main
import ( import (
@ -13,7 +7,6 @@ import (
) )
func handleCommitPress(g *gocui.Gui, currentView *gocui.View) error { func handleCommitPress(g *gocui.Gui, currentView *gocui.View) error {
devLog(stagedFiles(state.GitFiles))
if len(stagedFiles(state.GitFiles)) == 0 { if len(stagedFiles(state.GitFiles)) == 0 {
return createSimpleConfirmationPanel(g, currentView, "Nothing to Commit", "There are no staged files to commit (esc)") return createSimpleConfirmationPanel(g, currentView, "Nothing to Commit", "There are no staged files to commit (esc)")
} }
@ -40,7 +33,6 @@ func handleCommitSubmit(g *gocui.Gui, v *gocui.View) error {
// for whatever reason, a successful commit returns an error, so we're not // for whatever reason, a successful commit returns an error, so we're not
// going to check for an error here // going to check for an error here
if err := gitCommit(message); err != nil { if err := gitCommit(message); err != nil {
devLog(err)
panic(err) panic(err)
} }
refreshFiles(g) refreshFiles(g)

View file

@ -1,9 +1,3 @@
// lots of this has been directly ported from one of the example files, will brush up later
// Copyright 2014 The gocui Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main package main
import ( import (

View file

@ -1,9 +1,3 @@
// lots of this has been directly ported from one of the example files, will brush up later
// Copyright 2014 The gocui Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main package main
import ( import (

View file

@ -1,7 +1,3 @@
// Go has various value types including strings,
// integers, floats, booleans, etc. Here are a few
// basic examples.
package main package main
import ( import (
@ -11,6 +7,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"strings" "strings"
"time"
"github.com/fatih/color" "github.com/fatih/color"
) )
@ -52,7 +49,7 @@ func colorLog(colour color.Attribute, objects ...interface{}) {
func commandLog(objects ...interface{}) { func commandLog(objects ...interface{}) {
localLog(color.FgWhite, "/Users/jesseduffieldduffield/go/src/github.com/jesseduffield/gitgot/commands.log", objects...) localLog(color.FgWhite, "/Users/jesseduffieldduffield/go/src/github.com/jesseduffield/gitgot/commands.log", objects...)
localLog(color.FgWhite, "/Users/jesseduffieldduffield/go/src/github.com/jesseduffield/gitgot/development.log", objects...) // localLog(color.FgWhite, "/Users/jesseduffieldduffield/go/src/github.com/jesseduffield/gitgot/development.log", objects...)
} }
func localLog(colour color.Attribute, path string, objects ...interface{}) { func localLog(colour color.Attribute, path string, objects ...interface{}) {
@ -100,8 +97,12 @@ func mergeGitStatusFiles(oldGitFiles, newGitFiles []GitFile) []GitFile {
} }
func runDirectCommand(command string) (string, error) { func runDirectCommand(command string) (string, error) {
timeStart := time.Now()
commandLog(command) commandLog(command)
cmdOut, err := exec.Command("bash", "-c", command).CombinedOutput() cmdOut, err := exec.Command("bash", "-c", command).CombinedOutput()
devLog("run direct command time for command: ", command, time.Now().Sub(timeStart))
return string(cmdOut), err return string(cmdOut), err
} }
@ -158,18 +159,12 @@ func getGitBranches() []Branch {
for i, line := range branchLines { for i, line := range branchLines {
branches = append(branches, branchFromLine(line, i)) branches = append(branches, branchFromLine(line, i))
} }
devLog(branches)
return branches return branches
} }
func getGitStatusFiles() []GitFile { func getGitStatusFiles() []GitFile {
statusOutput, _ := getGitStatus() statusOutput, _ := getGitStatus()
statusStrings := splitLines(statusOutput) statusStrings := splitLines(statusOutput)
devLog(statusStrings)
// a file can have both staged and unstaged changes
// I'll probably end up ignoring the unstaged flag for now but might revisit
// tracked, staged, unstaged
gitFiles := make([]GitFile, 0) gitFiles := make([]GitFile, 0)
for _, statusString := range statusStrings { for _, statusString := range statusStrings {
@ -199,9 +194,11 @@ func gitCheckout(branch string, force bool) (string, error) {
} }
func runCommand(command string) (string, error) { func runCommand(command string) (string, error) {
startTime := time.Now()
commandLog(command) commandLog(command)
splitCmd := strings.Split(command, " ") splitCmd := strings.Split(command, " ")
cmdOut, err := exec.Command(splitCmd[0], splitCmd[1:]...).CombinedOutput() cmdOut, err := exec.Command(splitCmd[0], splitCmd[1:]...).CombinedOutput()
devLog("run command time: ", time.Now().Sub(startTime))
return string(cmdOut), err return string(cmdOut), err
} }
@ -245,7 +242,9 @@ func getCommits() []Commit {
} }
func getLog() string { func getLog() string {
result, err := runDirectCommand("git log --oneline") // currently limiting to 30 for performance reasons
// TODO: add lazyloading when you scroll down
result, err := runDirectCommand("git log --oneline -30")
if err != nil { if err != nil {
// assume if there is an error there are no commits yet for this branch // assume if there is an error there are no commits yet for this branch
return "" return ""

27
gui.go
View file

@ -1,9 +1,3 @@
// lots of this has been directly ported from one of the example files, will brush up later
// Copyright 2014 The gocui Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main package main
import ( import (
@ -11,7 +5,6 @@ import (
// "io" // "io"
// "io/ioutil" // "io/ioutil"
"fmt"
"log" "log"
// "strings" // "strings"
@ -181,26 +174,6 @@ func handleLogState(g *gocui.Gui, v *gocui.View) error {
return nil return nil
} }
func refreshStatus(g *gocui.Gui) error {
v, err := g.View("status")
if err != nil {
return err
}
v.Clear()
up, down := gitUpstreamDifferenceCount()
devLog(up, down)
fmt.Fprint(v, "↑"+up+"↓"+down)
branches := state.Branches
if len(branches) == 0 {
return nil
}
branch := branches[0]
// utilising the fact these all have padding to only grab the name
// from the display string with the existing coloring applied
fmt.Fprint(v, " "+branch.DisplayString[4:])
return nil
}
func layout(g *gocui.Gui) error { func layout(g *gocui.Gui) error {
width, height := g.Size() width, height := g.Size()
leftSideWidth := width / 3 leftSideWidth := width / 3

12
main.go
View file

@ -1,11 +1,15 @@
package main package main
import "github.com/fatih/color" import (
"time"
)
// StartTime : The starting time of the app
var StartTime time.Time
func main() { func main() {
verifyInGitRepo()
a, b := gitUpstreamDifferenceCount()
colorLog(color.FgRed, a, b)
devLog("\n\n\n\n\n\n\n\n\n\n") devLog("\n\n\n\n\n\n\n\n\n\n")
StartTime = time.Now()
verifyInGitRepo()
run() run()
} }

30
status_panel.go Normal file
View file

@ -0,0 +1,30 @@
package main
import (
"fmt"
"time"
"github.com/fatih/color"
"github.com/jroimartin/gocui"
)
func refreshStatus(g *gocui.Gui) error {
v, err := g.View("status")
if err != nil {
return err
}
v.Clear()
up, down := gitUpstreamDifferenceCount()
fmt.Fprint(v, "↑"+up+"↓"+down)
branches := state.Branches
if len(branches) == 0 {
return nil
}
branch := branches[0]
// utilising the fact these all have padding to only grab the name
// from the display string with the existing coloring applied
fmt.Fprint(v, " "+branch.DisplayString[4:])
colorLog(color.FgCyan, time.Now().Sub(StartTime))
return nil
}

View file

@ -9,6 +9,7 @@ package main
import ( import (
"fmt" "fmt"
"strings" "strings"
"time"
"github.com/jroimartin/gocui" "github.com/jroimartin/gocui"
) )
@ -97,6 +98,7 @@ func correctCursor(v *gocui.View) error {
func renderString(g *gocui.Gui, viewName, s string) error { func renderString(g *gocui.Gui, viewName, s string) error {
g.Update(func(*gocui.Gui) error { g.Update(func(*gocui.Gui) error {
timeStart := time.Now()
v, err := g.View(viewName) v, err := g.View(viewName)
if err != nil { if err != nil {
panic(err) panic(err)
@ -104,6 +106,7 @@ func renderString(g *gocui.Gui, viewName, s string) error {
v.Clear() v.Clear()
fmt.Fprint(v, s) fmt.Fprint(v, s)
v.Wrap = true v.Wrap = true
devLog("render time: ", time.Now().Sub(timeStart))
return nil return nil
}) })
return nil return nil