From e53fa1a5a2df2228a979c3d201e87c12a7bc1e40 Mon Sep 17 00:00:00 2001 From: Anis Elleuch Date: Mon, 11 Jul 2016 00:36:00 +0100 Subject: [PATCH] Simplify and enhance the scan bar rendering (#1759) --- mc_test.go | 6 ------ scan-bar.go | 24 ++++++------------------ strings.go | 34 ---------------------------------- 3 files changed, 6 insertions(+), 58 deletions(-) delete mode 100644 strings.go diff --git a/mc_test.go b/mc_test.go index 93229264..c365316b 100644 --- a/mc_test.go +++ b/mc_test.go @@ -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) diff --git a/scan-bar.go b/scan-bar.go index f0fffe59..bcc13969 100644 --- a/scan-bar.go +++ b/scan-bar.go @@ -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++ } } diff --git a/strings.go b/strings.go deleted file mode 100644 index ab3e0da1..00000000 --- a/strings.go +++ /dev/null @@ -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 -}