fix elapsed on complete
Vladimir Bauer
7 years ago
| 27 | 27 | WC |
| 28 | 28 | style int |
| 29 | 29 | startTime time.Time |
| 30 | msg string | |
| 30 | 31 | completeMsg *string |
| 31 | 32 | } |
| 32 | 33 | |
| 33 | 34 | func (d *elapsedDecorator) Decor(st *Statistics) string { |
| 34 | if st.Completed && d.completeMsg != nil { | |
| 35 | return d.FormatMsg(*d.completeMsg) | |
| 35 | if st.Completed { | |
| 36 | if d.completeMsg != nil { | |
| 37 | return d.FormatMsg(*d.completeMsg) | |
| 38 | } | |
| 39 | return d.FormatMsg(d.msg) | |
| 36 | 40 | } |
| 37 | 41 | |
| 38 | var str string | |
| 39 | 42 | timeElapsed := time.Since(d.startTime) |
| 40 | 43 | hours := int64((timeElapsed / time.Hour) % 60) |
| 41 | 44 | minutes := int64((timeElapsed / time.Minute) % 60) |
| 43 | 46 | |
| 44 | 47 | switch d.style { |
| 45 | 48 | case ET_STYLE_GO: |
| 46 | str = fmt.Sprint(time.Duration(timeElapsed.Seconds()) * time.Second) | |
| 49 | d.msg = fmt.Sprint(time.Duration(timeElapsed.Seconds()) * time.Second) | |
| 47 | 50 | case ET_STYLE_HHMMSS: |
| 48 | str = fmt.Sprintf("%02d:%02d:%02d", hours, minutes, seconds) | |
| 51 | d.msg = fmt.Sprintf("%02d:%02d:%02d", hours, minutes, seconds) | |
| 49 | 52 | case ET_STYLE_HHMM: |
| 50 | str = fmt.Sprintf("%02d:%02d", hours, minutes) | |
| 53 | d.msg = fmt.Sprintf("%02d:%02d", hours, minutes) | |
| 51 | 54 | case ET_STYLE_MMSS: |
| 52 | str = fmt.Sprintf("%02d:%02d", minutes, seconds) | |
| 55 | if hours > 0 { | |
| 56 | d.msg = fmt.Sprintf("%02d:%02d:%02d", hours, minutes, seconds) | |
| 57 | } else { | |
| 58 | d.msg = fmt.Sprintf("%02d:%02d", minutes, seconds) | |
| 59 | } | |
| 53 | 60 | } |
| 54 | 61 | |
| 55 | return d.FormatMsg(str) | |
| 62 | return d.FormatMsg(d.msg) | |
| 56 | 63 | } |
| 57 | 64 | |
| 58 | 65 | func (d *elapsedDecorator) OnCompleteMessage(msg string) { |