Commit 1c83dc241fe9999ce1d6e48bdf454e30891d896c

Authored by Marko Tikvić
1 parent 3173b06a41
Exists in master

GUID and timer

Showing 3 changed files with 111 additions and 0 deletions   Show diff stats
... ... @@ -0,0 +1,17 @@
  1 +package webutility
  2 +
  3 +import (
  4 + "crypto/rand"
  5 + "fmt"
  6 +)
  7 +
  8 +// GUID ...
  9 +func GUID() (string, error) {
  10 + b := make([]byte, 16)
  11 + _, err := rand.Read(b)
  12 + if err != nil {
  13 + return "", err
  14 + }
  15 + id := fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
  16 + return id, nil
  17 +}
... ...
... ... @@ -148,6 +148,14 @@ func SplitText(s string, maxLen int) (lines []string) {
148 148 return lines
149 149 }
150 150  
  151 +func CutTextWithThreeDots(txt string, maxLen int) string {
  152 + if len(txt) < maxLen || len(txt) <= 3 {
  153 + return txt
  154 + }
  155 +
  156 + return txt[:maxLen-3] + "..."
  157 +}
  158 +
151 159 const threeDots = "\u2056\u2056\u2056"
152 160  
153 161 func LimitTextWithThreeDots(txt string, maxLen int) string {
... ... @@ -158,6 +166,14 @@ func LimitTextWithThreeDots(txt string, maxLen int) string {
158 166 return txt[:maxLen] + threeDots
159 167 }
160 168  
  169 +func LimitMSWordTextWithThreeDots(txt string, maxLen int) string {
  170 + if len(txt) <= maxLen {
  171 + return txt
  172 + }
  173 +
  174 + return txt[:maxLen] + "..."
  175 +}
  176 +
161 177 // SplitStringAtWholeWords ...
162 178 func SplitStringAtWholeWords(s string, maxLen int) (res []string) {
163 179 parts := strings.Split(s, " ")
... ...
... ... @@ -0,0 +1,78 @@
  1 +package webutility
  2 +
  3 +import (
  4 + "fmt"
  5 + "time"
  6 +)
  7 +
  8 +// Timer ...
  9 +type Timer struct {
  10 + name string
  11 + running bool
  12 + started time.Time
  13 + stopped time.Time
  14 + lastDuration time.Duration
  15 +}
  16 +
  17 +// NewTimer ...
  18 +func NewTimer(name string) *Timer {
  19 + t := &Timer{name: name}
  20 + t.Reset()
  21 + return t
  22 +}
  23 +
  24 +// Start ...
  25 +func (t *Timer) Start() time.Time {
  26 + t.running = true
  27 + t.started = time.Now()
  28 + return t.started
  29 +}
  30 +
  31 +// Stop ...
  32 +func (t *Timer) Stop() time.Duration {
  33 + t.running = false
  34 + t.stopped = time.Now()
  35 + t.lastDuration = t.stopped.Sub(t.started)
  36 + return t.lastDuration
  37 +}
  38 +
  39 +// LastRunDuration ...
  40 +func (t *Timer) LastRunDuration() time.Duration {
  41 + return t.lastDuration
  42 +}
  43 +
  44 +// Clear ...
  45 +func (t *Timer) Clear() {
  46 + t.started = time.Now()
  47 + t.stopped = time.Now()
  48 + t.lastDuration = 0
  49 +}
  50 +
  51 +// Reset ...
  52 +func (t *Timer) Reset() {
  53 + t.Stop()
  54 + t.Start()
  55 +}
  56 +
  57 +// Elapsed ...
  58 +func (t *Timer) Elapsed() time.Duration {
  59 + if t.running {
  60 + return time.Now().Sub(t.started)
  61 + }
  62 + return 0
  63 +}
  64 +
  65 +// Print ...
  66 +func (t *Timer) Print(s string) {
  67 + status := "RUNNING"
  68 + if !t.running {
  69 + status = "STOPPED"
  70 + }
  71 + fmt.Printf("timer[%s][%s]: %v %s\n", t.name, status, t.Elapsed(), s)
  72 +}
  73 +
  74 +// Lap ...
  75 +func (t *Timer) Lap(s string) {
  76 + t.Print(s)
  77 + t.Reset()
  78 +}
... ...