Simplify and enhance the scan bar rendering (#1759)

This commit is contained in:
Anis Elleuch
2016-07-10 16:36:00 -07:00
committed by Harshavardhana
parent 92d1e226d3
commit e53fa1a5a2
3 changed files with 6 additions and 58 deletions
-6
View File
@@ -135,12 +135,6 @@ func (s *TestSuite) TestHumanizedTime(c *C) {
c.Assert(hTime.Days, Not(Equals), int64(0))
}
func (s *TestSuite) TestCommonPrefix(c *C) {
c.Assert(commonPrefix("/usr", "/usr/local"), Equals, "/usr")
c.Assert(commonPrefix("/uabbf", "/ursfad/ccc"), Equals, "/u")
c.Assert(commonPrefix("/usr/local/lib", "/usr/local/test"), Equals, "/usr/local/")
}
func (s *TestSuite) TestVersions(c *C) {
v1, e := version.NewVersion("1.6")
c.Assert(e, IsNil)
+6 -18
View File
@@ -1,5 +1,5 @@
/*
* Minio Client (C) 2014, 2015 Minio, Inc.
* Minio Client (C) 2014-2016 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -26,8 +26,7 @@ import (
"github.com/minio/pb"
)
/******************************** Scan Bar ************************************/
// fixateScanBar truncates long text to fit within the terminal size.
// fixateScanBar truncates or stretches text to fit within the terminal size.
func fixateScanBar(text string, width int) string {
if len([]rune(text)) > width {
// Trim text to fit within the screen
@@ -35,6 +34,8 @@ func fixateScanBar(text string, width int) string {
if trimSize < len([]rune(text)) {
text = "..." + text[trimSize:]
}
} else {
text += strings.Repeat(" ", width-len([]rune(text)))
}
return text
}
@@ -44,8 +45,6 @@ type scanBarFunc func(string)
// scanBarFactory returns a progress bar function to report URL scanning.
func scanBarFactory() scanBarFunc {
prevLineSize := 0
prevSource := ""
fileCount := 0
termWidth, e := pb.GetTerminalWidth()
fatalIf(probe.NewError(e), "Unable to get terminal size. Please use --quiet option.")
@@ -54,20 +53,9 @@ func scanBarFactory() scanBarFunc {
cursorCh := cursorAnimate()
return func(source string) {
scanPrefix := fmt.Sprintf("[%s] %s ", 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 + cmnPrefix + strings.Repeat(" ", eraseLen))
}
source = fixateScanBar(source, termWidth-len([]rune(scanPrefix))-1)
source = fixateScanBar(source, termWidth-len([]rune(scanPrefix)))
barText := scanPrefix + source
console.PrintC("\r" + barText)
prevSource = source
prevLineSize = len([]rune(barText))
console.PrintC("\r" + barText + "\r")
fileCount++
}
}
-34
View File
@@ -1,34 +0,0 @@
/*
* 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
}