diff --git a/cp-main.go b/cp-main.go index 4cfc09f9..9458b6ea 100644 --- a/cp-main.go +++ b/cp-main.go @@ -161,7 +161,7 @@ func doPrepareCopyURLs(session *sessionV2, trapCh <-chan bool) { var scanBar scanBarFunc if !globalQuietFlag && !globalJSONFlag { // set up progress bar - scanBar = scanBarFactory("") + scanBar = scanBarFactory() } URLsCh := prepareCopyURLs(sourceURLs, targetURL) @@ -180,14 +180,14 @@ func doPrepareCopyURLs(session *sessionV2, trapCh <-chan bool) { console.Printf("%c[2K\n", 27) console.Printf("%c[A", 27) } - errorIf(cpURLs.Error.Trace(), "Unable to prepare URLs for copying.") + errorIf(cpURLs.Error.Trace(), "Unable to prepare URL for copying.") break } jsonData, err := json.Marshal(cpURLs) if err != nil { session.Close() - fatalIf(probe.NewError(err), "Unable to prepare URLs for copying. Error in JSON marshaling.") + fatalIf(probe.NewError(err), "Unable to prepare URL for copying. Error in JSON marshaling.") } fmt.Fprintln(dataFP, string(jsonData)) if !globalQuietFlag && !globalJSONFlag { diff --git a/mirror-main.go b/mirror-main.go index 0c1db4b2..00d43047 100644 --- a/mirror-main.go +++ b/mirror-main.go @@ -158,7 +158,7 @@ func doPrepareMirrorURLs(session *sessionV2, trapCh <-chan bool) { var scanBar scanBarFunc if !globalQuietFlag && !globalJSONFlag { // set up progress bar - scanBar = scanBarFactory("") + scanBar = scanBarFactory() } URLsCh := prepareMirrorURLs(sourceURL, targetURLs) diff --git a/pb.go b/pb.go index a87ad6e0..34c026c0 100644 --- a/pb.go +++ b/pb.go @@ -221,27 +221,32 @@ func fixateScanBar(text string, width int) string { type scanBarFunc func(string) // scanBarFactory returns a progress bar function to report URL scanning. -func scanBarFactory(prefix string) scanBarFunc { +func scanBarFactory() scanBarFunc { prevLineSize := 0 + prevSource := "" fileCount := 0 termSize, err := ts.GetSize() if err != nil { - fatalIf(probe.NewError(err), "Unable to get terminal size. Please enable --quiet mode for simpler terminals") + fatalIf(probe.NewError(err), "Unable to get terminal size. Please use --quiet option.") } termWidth := termSize.Col() cursorCh := cursorAnimate() return func(source string) { scanPrefix := fmt.Sprintf("[%s] %s ", humanize.Comma(int64(fileCount)), string(<-cursorCh)) - if prefix != "" { - scanPrefix = fmt.Sprintf("Scanning %s [%s] %s ", prefix, humanize.Comma(int64(fileCount)), string(<-cursorCh)) + cmnPrefix := commonPrefix(source, prevSource) + eraseLen := prevLineSize - len([]rune(scanPrefix+cmnPrefix)) + if eraseLen < 1 { + eraseLen = 0 } if prevLineSize != 0 { // erase previous line - console.PrintC("\r" + scanPrefix + strings.Repeat(" ", prevLineSize-len([]rune(scanPrefix)))) + console.PrintC("\r" + scanPrefix + cmnPrefix + strings.Repeat(" ", eraseLen)) } + source = fixateScanBar(source, termWidth-len([]rune(scanPrefix))-1) - barText := "\r" + scanPrefix + source - console.PrintC(barText) + barText := scanPrefix + source + console.PrintC("\r" + barText) + prevSource = source prevLineSize = len([]rune(barText)) fileCount++ } diff --git a/strings.go b/strings.go new file mode 100644 index 00000000..ab3e0da1 --- /dev/null +++ b/strings.go @@ -0,0 +1,34 @@ +/* + * Minio Client (C) 2015 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package main + +// commonPrefix returns the longest common prefix between two strings. +func commonPrefix(first, second string) string { + if first == "" || second == "" { + return "" + } + if first > second { + // Pick the shorter one for looping. + first, second = second, first + } + for i := range first { + if first[i] != second[i] { + return first[:i] + } + } + return first +}