From e852e8f96a39368aea4b3915e325ecaaab14f855 Mon Sep 17 00:00:00 2001 From: Djordje Lukic Date: Thu, 2 Jul 2020 10:38:03 +0200 Subject: [PATCH 1/2] Count the lines from the terminal, not from ACI Count the number of lines output on the terminal wrt the terminal width so that we can go up the right amount. --- azure/aci.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/azure/aci.go b/azure/aci.go index e08457490..91c96b19c 100644 --- a/azure/aci.go +++ b/azure/aci.go @@ -27,6 +27,7 @@ import ( "github.com/Azure/azure-sdk-for-go/services/containerinstance/mgmt/2018-10-01/containerinstance" "github.com/Azure/go-autorest/autorest" "github.com/Azure/go-autorest/autorest/to" + "github.com/buger/goterm" tm "github.com/buger/goterm" "github.com/gobwas/ws" "github.com/gobwas/ws/wsutil" @@ -242,7 +243,8 @@ func getACIContainerLogs(ctx context.Context, aciContext store.AciContext, conta } func streamLogs(ctx context.Context, aciContext store.AciContext, containerGroupName, containerName string, out io.Writer) error { - lastOutput := 0 + terminalWidth := goterm.Width() + numLines := 0 for { select { case <-ctx.Done(): @@ -260,14 +262,20 @@ func streamLogs(ctx context.Context, aciContext store.AciContext, containerGroup // the kind of logs ACI is giving us. Hopefully Azue will give us // a real logs streaming api soon. b := aec.EmptyBuilder - b = b.Up(uint(lastOutput)) + b = b.Up(uint(numLines)) fmt.Fprint(out, b.Column(0).ANSI) for i := 0; i < currentOutput-1; i++ { fmt.Fprintln(out, logLines[i]) } - lastOutput = currentOutput - 1 + numLines = 0 + for i := 0; i < currentOutput-1; i++ { + numLines++ + if len(logLines[i]) > terminalWidth { + numLines += len(logLines[i]) / terminalWidth + } + } time.Sleep(2 * time.Second) } From d3f1209babf3d54976c5fb81f1fe36ad10347949 Mon Sep 17 00:00:00 2001 From: Djordje Lukic Date: Thu, 2 Jul 2020 11:11:09 +0200 Subject: [PATCH 2/2] Add test for the backtracking of lines --- azure/aci.go | 22 ++++++++++++++-------- azure/aci_test.go | 12 ++++++++++++ 2 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 azure/aci_test.go diff --git a/azure/aci.go b/azure/aci.go index 91c96b19c..71f2d75bb 100644 --- a/azure/aci.go +++ b/azure/aci.go @@ -265,23 +265,29 @@ func streamLogs(ctx context.Context, aciContext store.AciContext, containerGroup b = b.Up(uint(numLines)) fmt.Fprint(out, b.Column(0).ANSI) + numLines = getBacktrackLines(logLines, terminalWidth) + for i := 0; i < currentOutput-1; i++ { fmt.Fprintln(out, logLines[i]) } - numLines = 0 - for i := 0; i < currentOutput-1; i++ { - numLines++ - if len(logLines[i]) > terminalWidth { - numLines += len(logLines[i]) / terminalWidth - } - } - time.Sleep(2 * time.Second) } } } +func getBacktrackLines(lines []string, terminalWidth int) int { + numLines := 0 + for i := 0; i < len(lines)-1; i++ { + numLines++ + if len(lines[i]) > terminalWidth { + numLines += len(lines[i]) / terminalWidth + } + } + + return numLines +} + func getContainerGroupsClient(subscriptionID string) (containerinstance.ContainerGroupsClient, error) { containerGroupsClient := containerinstance.NewContainerGroupsClient(subscriptionID) err := setupClient(&containerGroupsClient.Client) diff --git a/azure/aci_test.go b/azure/aci_test.go new file mode 100644 index 000000000..4af8cf5a2 --- /dev/null +++ b/azure/aci_test.go @@ -0,0 +1,12 @@ +package azure + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGetLinesWritten(t *testing.T) { + assert.Equal(t, 0, getBacktrackLines([]string{"Hello"}, 10)) + assert.Equal(t, 3, getBacktrackLines([]string{"Hello", "world"}, 2)) +}