You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.6 KiB
83 lines
2.6 KiB
9 years ago
|
/*
|
||
|
* Minio Cloud Storage, (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
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"runtime"
|
||
|
"strings"
|
||
|
|
||
9 years ago
|
"github.com/cheggaaa/pb"
|
||
9 years ago
|
"github.com/fatih/color"
|
||
|
)
|
||
|
|
||
|
// colorizeUpdateMessage - inspired from Yeoman project npm package https://github.com/yeoman/update-notifier
|
||
9 years ago
|
func colorizeUpdateMessage(updateString string) string {
|
||
9 years ago
|
// Initialize coloring.
|
||
9 years ago
|
cyan := color.New(color.FgCyan, color.Bold).SprintFunc()
|
||
|
yellow := color.New(color.FgYellow, color.Bold).SprintfFunc()
|
||
|
|
||
9 years ago
|
// Calculate length without color coding, due to ANSI color
|
||
|
// characters padded to actual string the final length is wrong
|
||
|
// than the original string length.
|
||
|
line1Str := fmt.Sprintf(" New update: %s ", updateString)
|
||
9 years ago
|
line1Length := len(line1Str)
|
||
|
|
||
9 years ago
|
// Populate lines with color coding.
|
||
|
line1InColor := fmt.Sprintf(" New update: %s ", cyan(updateString))
|
||
9 years ago
|
|
||
9 years ago
|
// Calculate the rectangular box size.
|
||
|
maxContentWidth := line1Length
|
||
9 years ago
|
line1Rest := maxContentWidth - line1Length
|
||
|
|
||
9 years ago
|
// termWidth is set to a default one to use when we are
|
||
|
// not able to calculate terminal width via OS syscalls
|
||
|
termWidth := 25
|
||
|
|
||
|
if width, err := pb.GetTerminalWidth(); err == nil {
|
||
|
termWidth = width
|
||
9 years ago
|
}
|
||
|
|
||
|
var message string
|
||
|
switch {
|
||
9 years ago
|
case len(line1Str) > termWidth:
|
||
9 years ago
|
message = "\n" + line1InColor + "\n"
|
||
9 years ago
|
default:
|
||
9 years ago
|
// On windows terminal turn off unicode characters.
|
||
9 years ago
|
var top, bottom, sideBar string
|
||
|
if runtime.GOOS == "windows" {
|
||
|
top = yellow("*" + strings.Repeat("*", maxContentWidth) + "*")
|
||
|
bottom = yellow("*" + strings.Repeat("*", maxContentWidth) + "*")
|
||
|
sideBar = yellow("|")
|
||
|
} else {
|
||
9 years ago
|
// Color the rectangular box, use unicode characters here.
|
||
9 years ago
|
top = yellow("┏" + strings.Repeat("━", maxContentWidth) + "┓")
|
||
|
bottom = yellow("┗" + strings.Repeat("━", maxContentWidth) + "┛")
|
||
|
sideBar = yellow("┃")
|
||
|
}
|
||
9 years ago
|
// Fill spaces to the rest of the area.
|
||
9 years ago
|
spacePaddingLine1 := strings.Repeat(" ", line1Rest)
|
||
|
|
||
9 years ago
|
// Construct the final message.
|
||
9 years ago
|
message = "\n" + top + "\n" +
|
||
|
sideBar + line1InColor + spacePaddingLine1 + sideBar + "\n" +
|
||
|
bottom + "\n"
|
||
|
}
|
||
9 years ago
|
// Return the final message.
|
||
9 years ago
|
return message
|
||
9 years ago
|
}
|