فهرست منبع

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 9 سال پیش
والد
کامیت
59df2adc07
1فایلهای تغییر یافته به همراه7 افزوده شده و 4 حذف شده
  1. 7 4
      pkg/jsonmessage/jsonmessage.go

+ 7 - 4
pkg/jsonmessage/jsonmessage.go

@@ -150,11 +150,11 @@ func (jm *JSONMessage) Display(out io.Writer, isTerminal bool) error {
 // each line and move the cursor while displaying.
 // each line and move the cursor while displaying.
 func DisplayJSONMessagesStream(in io.Reader, out io.Writer, terminalFd uintptr, isTerminal bool) error {
 func DisplayJSONMessagesStream(in io.Reader, out io.Writer, terminalFd uintptr, isTerminal bool) error {
 	var (
 	var (
-		dec  = json.NewDecoder(in)
-		ids  = make(map[string]int)
-		diff = 0
+		dec = json.NewDecoder(in)
+		ids = make(map[string]int)
 	)
 	)
 	for {
 	for {
+		diff := 0
 		var jm JSONMessage
 		var jm JSONMessage
 		if err := dec.Decode(&jm); err != nil {
 		if err := dec.Decode(&jm); err != nil {
 			if err == io.EOF {
 			if err == io.EOF {
@@ -180,11 +180,12 @@ func DisplayJSONMessagesStream(in io.Reader, out io.Writer, terminalFd uintptr,
 				if isTerminal {
 				if isTerminal {
 					fmt.Fprintf(out, "\n")
 					fmt.Fprintf(out, "\n")
 				}
 				}
-				diff = 0
 			} else {
 			} else {
 				diff = len(ids) - line
 				diff = len(ids) - line
 			}
 			}
 			if jm.ID != "" && isTerminal {
 			if jm.ID != "" && isTerminal {
+				// NOTE: this appears to be necessary even if
+				// diff == 0.
 				// <ESC>[{diff}A = move cursor up diff rows
 				// <ESC>[{diff}A = move cursor up diff rows
 				fmt.Fprintf(out, "%c[%dA", 27, diff)
 				fmt.Fprintf(out, "%c[%dA", 27, diff)
 			}
 			}
@@ -198,6 +199,8 @@ func DisplayJSONMessagesStream(in io.Reader, out io.Writer, terminalFd uintptr,
 		}
 		}
 		err := jm.Display(out, isTerminal)
 		err := jm.Display(out, isTerminal)
 		if jm.ID != "" && isTerminal {
 		if jm.ID != "" && isTerminal {
+			// NOTE: this appears to be necessary even if
+			// diff == 0.
 			// <ESC>[{diff}B = move cursor down diff rows
 			// <ESC>[{diff}B = move cursor down diff rows
 			fmt.Fprintf(out, "%c[%dB", 27, diff)
 			fmt.Fprintf(out, "%c[%dB", 27, diff)
 		}
 		}