Browse code

Fix the scoping of "diff" so its value doesn't leak between loop iterations

In the existing code, "diff" has function scope and the value from the
previous iteration may be used if it is not reset. This appears to be an
oversight. This commit changes its scope to the for loop body.

One confusing point is that the cursor movement escape sequences appear
to be necessary even if the requested movement is 0. I haven't been able
to figure out why this makes a difference.

Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>

Aaron Lehmann authored on 2015/12/08 10:01:47
Showing 1 changed files
... ...
@@ -150,11 +150,11 @@ func (jm *JSONMessage) Display(out io.Writer, isTerminal bool) error {
150 150
 // each line and move the cursor while displaying.
151 151
 func DisplayJSONMessagesStream(in io.Reader, out io.Writer, terminalFd uintptr, isTerminal bool) error {
152 152
 	var (
153
-		dec  = json.NewDecoder(in)
154
-		ids  = make(map[string]int)
155
-		diff = 0
153
+		dec = json.NewDecoder(in)
154
+		ids = make(map[string]int)
156 155
 	)
157 156
 	for {
157
+		diff := 0
158 158
 		var jm JSONMessage
159 159
 		if err := dec.Decode(&jm); err != nil {
160 160
 			if err == io.EOF {
... ...
@@ -180,11 +180,12 @@ func DisplayJSONMessagesStream(in io.Reader, out io.Writer, terminalFd uintptr,
180 180
 				if isTerminal {
181 181
 					fmt.Fprintf(out, "\n")
182 182
 				}
183
-				diff = 0
184 183
 			} else {
185 184
 				diff = len(ids) - line
186 185
 			}
187 186
 			if jm.ID != "" && isTerminal {
187
+				// NOTE: this appears to be necessary even if
188
+				// diff == 0.
188 189
 				// <ESC>[{diff}A = move cursor up diff rows
189 190
 				fmt.Fprintf(out, "%c[%dA", 27, diff)
190 191
 			}
... ...
@@ -198,6 +199,8 @@ func DisplayJSONMessagesStream(in io.Reader, out io.Writer, terminalFd uintptr,
198 198
 		}
199 199
 		err := jm.Display(out, isTerminal)
200 200
 		if jm.ID != "" && isTerminal {
201
+			// NOTE: this appears to be necessary even if
202
+			// diff == 0.
201 203
 			// <ESC>[{diff}B = move cursor down diff rows
202 204
 			fmt.Fprintf(out, "%c[%dB", 27, diff)
203 205
 		}