diff --git a/guid.go b/guid.go new file mode 100644 index 0000000..33ab3ef --- /dev/null +++ b/guid.go @@ -0,0 +1,17 @@ +package webutility + +import ( + "crypto/rand" + "fmt" +) + +// GUID ... +func GUID() (string, error) { + b := make([]byte, 16) + _, err := rand.Read(b) + if err != nil { + return "", err + } + id := fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:]) + return id, nil +} diff --git a/string_util.go b/string_util.go index d495248..32d5117 100644 --- a/string_util.go +++ b/string_util.go @@ -148,6 +148,14 @@ func SplitText(s string, maxLen int) (lines []string) { return lines } +func CutTextWithThreeDots(txt string, maxLen int) string { + if len(txt) < maxLen || len(txt) <= 3 { + return txt + } + + return txt[:maxLen-3] + "..." +} + const threeDots = "\u2056\u2056\u2056" func LimitTextWithThreeDots(txt string, maxLen int) string { @@ -158,6 +166,14 @@ func LimitTextWithThreeDots(txt string, maxLen int) string { return txt[:maxLen] + threeDots } +func LimitMSWordTextWithThreeDots(txt string, maxLen int) string { + if len(txt) <= maxLen { + return txt + } + + return txt[:maxLen] + "..." +} + // SplitStringAtWholeWords ... func SplitStringAtWholeWords(s string, maxLen int) (res []string) { parts := strings.Split(s, " ") diff --git a/timer.go b/timer.go new file mode 100644 index 0000000..6c2a0c8 --- /dev/null +++ b/timer.go @@ -0,0 +1,78 @@ +package webutility + +import ( + "fmt" + "time" +) + +// Timer ... +type Timer struct { + name string + running bool + started time.Time + stopped time.Time + lastDuration time.Duration +} + +// NewTimer ... +func NewTimer(name string) *Timer { + t := &Timer{name: name} + t.Reset() + return t +} + +// Start ... +func (t *Timer) Start() time.Time { + t.running = true + t.started = time.Now() + return t.started +} + +// Stop ... +func (t *Timer) Stop() time.Duration { + t.running = false + t.stopped = time.Now() + t.lastDuration = t.stopped.Sub(t.started) + return t.lastDuration +} + +// LastRunDuration ... +func (t *Timer) LastRunDuration() time.Duration { + return t.lastDuration +} + +// Clear ... +func (t *Timer) Clear() { + t.started = time.Now() + t.stopped = time.Now() + t.lastDuration = 0 +} + +// Reset ... +func (t *Timer) Reset() { + t.Stop() + t.Start() +} + +// Elapsed ... +func (t *Timer) Elapsed() time.Duration { + if t.running { + return time.Now().Sub(t.started) + } + return 0 +} + +// Print ... +func (t *Timer) Print(s string) { + status := "RUNNING" + if !t.running { + status = "STOPPED" + } + fmt.Printf("timer[%s][%s]: %v %s\n", t.name, status, t.Elapsed(), s) +} + +// Lap ... +func (t *Timer) Lap(s string) { + t.Print(s) + t.Reset() +}