Skip to content

Commit 90fbbef

Browse files
committed
fix(livelog): render started/done pending entries on first livelog.Render call
The livelog is rendered only once if command execution takes less than render tick interval. Previously, pending log messages were moved to in progress state on first render and rendered on second render. This caused log messages to not be rendered.
1 parent 1d1ce6f commit 90fbbef

3 files changed

Lines changed: 45 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
- Print version info, instead of missing credentials error, when runnning `upctl version` without credentials
1818
- Disable colors when outputting in JSON or YAML format
1919
- Display both public and private addresses in `server create` output
20+
- Render livelog messages of commands which execution takes less than render tick interval
2021

2122
## [1.1.3] - 2022-02-24
2223
### Changed

internal/ui/log.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,20 @@ func (s *LiveLog) Render() {
129129
s.eraseLine() // erase the end of line
130130
s.write(text.CursorUp.Sprint()) // and move up
131131
}
132+
133+
// Move pending entries that have started to in progress
134+
newPending := s.entriesPending[:0]
135+
for _, entry := range s.entriesPending {
136+
isStarted := !entry.started.IsZero()
137+
if !isStarted {
138+
newPending = append(newPending, entry)
139+
continue
140+
}
141+
s.entriesInProgress = append(s.entriesInProgress, entry)
142+
}
143+
s.entriesPending = newPending
144+
145+
// Move in progress entries that have completed to done
132146
newInProgress := s.entriesInProgress[:0]
133147
// Render any completed
134148
for _, entry := range s.entriesInProgress {
@@ -147,18 +161,6 @@ func (s *LiveLog) Render() {
147161
}
148162
s.entriesInProgress = newInProgress
149163

150-
// Add any pending entries that have started
151-
newPending := s.entriesPending[:0]
152-
for _, entry := range s.entriesPending {
153-
isStarted := !entry.started.IsZero()
154-
if !isStarted {
155-
newPending = append(newPending, entry)
156-
continue
157-
}
158-
s.entriesInProgress = append(s.entriesInProgress, entry)
159-
}
160-
s.entriesPending = newPending
161-
162164
if s.config.DisableLiveRendering {
163165
return
164166
}

internal/ui/log_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package ui
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
// TestLogRender ensures that log entries are outputted with single Render call
12+
func TestLogRender(t *testing.T) {
13+
var b bytes.Buffer
14+
config := LiveLogDefaultConfig
15+
config.EntryMaxWidth = 50
16+
livelog := NewLiveLog(&b, config)
17+
18+
// Simulate typical livelog usage
19+
msg := "Testing log.Render"
20+
entry := NewLogEntry(msg)
21+
livelog.AddEntries(entry)
22+
entry.StartedNow()
23+
entry.SetMessage(fmt.Sprintf("%s: Done", msg))
24+
entry.MarkDone()
25+
livelog.Render()
26+
livelog.Close()
27+
28+
assert.Contains(t, b.String(), msg)
29+
assert.Contains(t, b.String(), ": Done")
30+
}

0 commit comments

Comments
 (0)