flicker free scanbar

This commit is contained in:
Anand Babu (AB) Periasamy
2015-09-22 04:10:48 -07:00
parent d86c7cc793
commit dc3a647dfe
4 changed files with 50 additions and 11 deletions
+3 -3
View File
@@ -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 {
+1 -1
View File
@@ -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)
+12 -7
View File
@@ -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++
}
+34
View File
@@ -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
}