feat: crono

This commit is contained in:
Urko 2023-04-05 17:25:46 +02:00
commit 5a024fbb8d
4 changed files with 118 additions and 0 deletions

0
.gitignore vendored Normal file
View File

11
go.mod Normal file
View File

@ -0,0 +1,11 @@
module gitea.urkob.com/crono
go 1.19
require github.com/jedib0t/go-pretty/v6 v6.4.6
require (
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
golang.org/x/sys v0.1.0 // indirect
)

23
go.sum Normal file
View File

@ -0,0 +1,23 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/jedib0t/go-pretty/v6 v6.4.6 h1:v6aG9h6Uby3IusSSEjHaZNXpHFhzqMmjXcPq1Rjl9Jw=
github.com/jedib0t/go-pretty/v6 v6.4.6/go.mod h1:Ndk3ase2CkQbXLLNf5QDHoYb6J9WtVfmHZu9n8rk2xs=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/pkg/profile v1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.4 h1:wZRexSlwd7ZXfKINDLsO4r7WBt3gTKONc6K/VesHvHM=
github.com/stretchr/testify v1.7.4/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

84
main.go Normal file
View File

@ -0,0 +1,84 @@
package crono
import (
"fmt"
"os"
"time"
"github.com/jedib0t/go-pretty/v6/table"
)
type Timing struct {
Msg string
Elapsed time.Duration
}
type Crono struct {
Begin time.Time
Cursor time.Time
Timings []Timing
}
// new cronograph with autostart.
func New() *Crono {
c := new(Crono)
c.Timings = make([]Timing, 0)
c.Begin = time.Now()
c.Cursor = c.Begin
return c
}
// restart the crono
// keeps the begin time.
func (c *Crono) Restart() {
c.Cursor = time.Now()
}
// push a mark.
func (c *Crono) Mark(msg string) {
c.Timings = append(c.Timings, Timing{msg, c.GetElapsed()})
}
// push a mark and restart the cursor time.
func (c *Crono) MarkAndRestart(msg string) {
c.Mark(msg)
c.Restart()
}
// time in seconds since last start
// it doesn't restart the crono.
func (c *Crono) GetElapsed() time.Duration {
return time.Since(c.Cursor)
}
// total time sice start.
func (c *Crono) Total() time.Duration {
return time.Since(c.Begin)
}
func (c *Crono) Table() {
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"#", "Description", "Elapsed"})
for i, e := range c.Timings {
t.AppendRow([]interface{}{i, e.Msg, durationToString(e.Elapsed)})
}
t.AppendSeparator()
t.AppendFooter(table.Row{"", "Total", durationToString(c.Total())})
t.Render()
}
func durationToString(t time.Duration) string {
timeString := fmt.Sprintf("%dms", t.Milliseconds())
if t.Seconds() > 3 {
timeString += " !!!"
} else if t.Seconds() > 2 {
timeString += " .!!"
} else if t.Seconds() > 1 {
timeString += " ..!"
} else {
timeString += " ..."
}
return timeString
}