From bd547d9084086ad6fde317c915c4fdec5936bd56 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Mon, 24 Aug 2015 11:51:52 -0700 Subject: [PATCH] Make sure to colorize diff types properly, also handle proper newlines --- cp-main.go | 16 +++++++++++++++- diff-main.go | 5 ++++- diff.go | 11 ++++++----- mirror-main.go | 15 +++++++++++++++ 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/cp-main.go b/cp-main.go index a34129db..dfa0b030 100644 --- a/cp-main.go +++ b/cp-main.go @@ -194,6 +194,11 @@ func doPrepareCopyURLs(session *sessionV2, trapCh <-chan bool) { totalBytes += cpURLs.SourceContent.Size totalObjects++ case <-trapCh: + // Print in new line and adjust to top so that we don't print over the ongoing scan bar + if !globalQuietFlag && !globalJSONFlag { + console.Printf("%c[2K\n", 27) + console.Printf("%c[A", 27) + } session.Close() // If we are interrupted during the URL scanning, we drop the session. session.Delete() os.Exit(0) @@ -247,10 +252,19 @@ func doCopyCmdSession(session *sessionV2) { if cpURLs.Error == nil { session.Header.LastCopied = cpURLs.SourceContent.Name } else { - console.Println() + // Print in new line and adjust to top so that we don't print over the ongoing progress bar + if !globalQuietFlag && !globalJSONFlag { + console.Printf("%c[2K\n", 27) + console.Printf("%c[A", 27) + } errorIf(cpURLs.Error.Trace(), fmt.Sprintf("Failed to copy ‘%s’.", cpURLs.SourceContent.Name)) } case <-trapCh: // Receive interrupt notification. + // Print in new line and adjust to top so that we don't print over the ongoing progress bar + if !globalQuietFlag && !globalJSONFlag { + console.Printf("%c[2K\n", 27) + console.Printf("%c[A", 27) + } session.Close() session.Info() os.Exit(0) diff --git a/diff-main.go b/diff-main.go index 9c3bd44e..528e3098 100644 --- a/diff-main.go +++ b/diff-main.go @@ -59,7 +59,10 @@ func mainDiff(ctx *cli.Context) { } console.SetCustomTheme(map[string]*color.Color{ - "Diff": color.New(color.FgGreen, color.Bold), + "DiffMessage": color.New(color.FgGreen, color.Bold), + "DiffOnlyInFirst": color.New(color.FgRed, color.Bold), + "DiffType": color.New(color.FgYellow, color.Bold), + "DiffSize": color.New(color.FgMagenta, color.Bold), }) config := mustGetMcConfig() diff --git a/diff.go b/diff.go index 8425f7c1..d7a489a3 100644 --- a/diff.go +++ b/diff.go @@ -58,15 +58,15 @@ func (d DiffMessage) String() string { msg := "" switch d.Diff { case "only-in-first": - msg = "‘" + d.FirstURL + "’" + " and " + "‘" + d.SecondURL + "’" + " - only in first." + msg = console.Colorize("DiffMessage", "‘"+d.FirstURL+"’"+" and "+"‘"+d.SecondURL+"’") + console.Colorize("DiffOnlyInFirst", " - only in first.") case "type": - msg = "‘" + d.FirstURL + "’" + " and " + "‘" + d.SecondURL + "’" + " - differ in type." + msg = console.Colorize("DiffMessage", "‘"+d.FirstURL+"’"+" and "+"‘"+d.SecondURL+"’") + console.Colorize("DiffType", " - differ in type.") case "size": - msg = "‘" + d.FirstURL + "’" + " and " + "‘" + d.SecondURL + "’" + " - differ in size." + msg = console.Colorize("DiffMessage", "‘"+d.FirstURL+"’"+" and "+"‘"+d.SecondURL+"’") + console.Colorize("DiffSize", " - differ in size.") default: fatalIf(errDummy.Trace(), "Unhandled difference between ‘"+d.FirstURL+"’ and ‘"+d.SecondURL+"’.") } - return console.Colorize("Diff", msg) + return msg } diffJSONBytes, err := json.Marshal(d) fatalIf(probe.NewError(err), "Unable to marshal diff message ‘"+d.FirstURL+"’, ‘"+d.SecondURL+"’ and ‘"+d.Diff+"’.") @@ -299,7 +299,8 @@ func dodiffRecursive(firstClnt, secondClnt client.Client, ch chan DiffMessage) { wg.Wait() doneCh <- struct{}{} if !globalQuietFlag && !globalJSONFlag { - console.PrintC("\r" + "Finished." + "\n") + console.Printf("%c[2K\n", 27) + console.Printf("%c[A", 27) } matchNameCh := make(chan string, 10000) diff --git a/mirror-main.go b/mirror-main.go index ea6c408d..dff0b0a3 100644 --- a/mirror-main.go +++ b/mirror-main.go @@ -175,6 +175,11 @@ func doPrepareMirrorURLs(session *sessionV2, trapCh <-chan bool) { totalBytes += sURLs.SourceContent.Size totalObjects++ case <-trapCh: + // Print in new line and adjust to top so that we don't print over the ongoing scan bar + if !globalQuietFlag && !globalJSONFlag { + console.Printf("%c[2K\n", 27) + console.Printf("%c[A", 27) + } session.Close() // If we are interrupted during the URL scanning, we drop the session. session.Delete() os.Exit(0) @@ -228,9 +233,19 @@ func doMirrorCmdSession(session *sessionV2) { if sURLs.Error == nil { session.Header.LastCopied = sURLs.SourceContent.Name } else { + // Print in new line and adjust to top so that we don't print over the ongoing progress bar + if !globalQuietFlag && !globalJSONFlag { + console.Printf("%c[2K\n", 27) + console.Printf("%c[A", 27) + } errorIf(sURLs.Error.Trace(), "Failed to mirror.") } case <-trapCh: // Receive interrupt notification. + // Print in new line and adjust to top so that we don't print over the ongoing progress bar + if !globalQuietFlag && !globalJSONFlag { + console.Printf("%c[2K\n", 27) + console.Printf("%c[A", 27) + } session.Close() session.Info() os.Exit(0)