Merge pull request #816 from harshavardhana/pr_out_turn_off_boxed_ui_for_update_notification_on_smaller_terminals

Turn off boxed UI for update notification on smaller terminals
This commit is contained in:
Harshavardhana
2015-06-17 20:38:39 -07:00
15 changed files with 375 additions and 29 deletions
+4
View File
@@ -38,6 +38,10 @@
"ImportPath": "github.com/minio/minio/pkg/iodine",
"Rev": "0f58045480309d4b13fe45878f05736c3ff20e7e"
},
{
"ImportPath": "github.com/olekukonko/ts",
"Rev": "ecf753e7c962639ab5a1fb46f7da627d4c0a04b8"
},
{
"ImportPath": "github.com/shiena/ansicolor",
"Rev": "8368d3b31cf6f2c2464c7a91675342c9a0ac6658"
+6
View File
@@ -0,0 +1,6 @@
language: go
go:
- 1.1
- 1.2
- tip
+19
View File
@@ -0,0 +1,19 @@
Copyright (C) 2014 by Oleku Konko
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
+28
View File
@@ -0,0 +1,28 @@
ts (Terminal Size)
==
[![Build Status](https://travis-ci.org/olekukonko/ts.png?branch=master)](https://travis-ci.org/olekukonko/ts) [![Total views](https://sourcegraph.com/api/repos/github.com/olekukonko/ts/counters/views.png)](https://sourcegraph.com/github.com/olekukonko/ts)
Simple go Application to get Terminal Size. So Many Implementations do not support windows but `ts` has full windows support.
Run `go get github.com/olekukonko/ts` to download and install
#### Example
```go
package main
import (
"fmt"
"github.com/olekukonko/ts"
)
func main() {
size, _ := ts.GetSize()
fmt.Println(size.Col()) // Get Width
fmt.Println(size.Row()) // Get Height
fmt.Println(size.PosX()) // Get X position
fmt.Println(size.PosY()) // Get Y position
}
```
[See Documentation](http://godoc.org/github.com/olekukonko/ts)
+36
View File
@@ -0,0 +1,36 @@
// Copyright 2014 Oleku Konko All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
// This module is a Terminal API for the Go Programming Language.
// The protocols were written in pure Go and works on windows and unix systems
/**
Simple go Application to get Terminal Size. So Many Implementations do not support windows but `ts` has full windows support.
Run `go get github.com/olekukonko/ts` to download and install
Installation
Minimum requirements are Go 1.1+ with fill Windows support
Example
package main
import (
"fmt"
"github.com/olekukonko/ts"
)
func main() {
size, _ := ts.GetSize()
fmt.Println(size.Col()) // Get Width
fmt.Println(size.Row()) // Get Height
fmt.Println(size.PosX()) // Get X position
fmt.Println(size.PosY()) // Get Y position
}
**/
package ts
+36
View File
@@ -0,0 +1,36 @@
// Copyright 2014 Oleku Konko All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
// This module is a Terminal API for the Go Programming Language.
// The protocols were written in pure Go and works on windows and unix systems
package ts
// Return System Size
type Size struct {
row uint16
col uint16
posX uint16
posY uint16
}
// Get Terminal Width
func (w Size) Col() int {
return int(w.col)
}
// Get Terminal Height
func (w Size) Row() int {
return int(w.row)
}
// Get Position X
func (w Size) PosX() int {
return int(w.posX)
}
// Get Position Y
func (w Size) PosY() int {
return int(w.posY)
}
+14
View File
@@ -0,0 +1,14 @@
// +build darwin
// Copyright 2014 Oleku Konko All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
// This module is a Terminal API for the Go Programming Language.
// The protocols were written in pure Go and works on windows and unix systems
package ts
const (
TIOCGWINSZ = 0x40087468
)
+13
View File
@@ -0,0 +1,13 @@
// +build linux
// Copyright 2014 Oleku Konko All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
// This module is a Terminal API for the Go Programming Language.
// The protocols were written in pure Go and works on windows and unix systems
package ts
const (
TIOCGWINSZ = 0x5413
)
+14
View File
@@ -0,0 +1,14 @@
// +build !windows,!darwin,!freebsd,!netbsd,!openbsd,!linux
// Copyright 2014 Oleku Konko All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
// This module is a Terminal API for the Go Programming Language.
// The protocols were written in pure Go and works on windows and unix systems
package ts
const (
TIOCGWINSZ = 0
)
+32
View File
@@ -0,0 +1,32 @@
// Copyright 2014 Oleku Konko All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
// This module is a Terminal API for the Go Programming Language.
// The protocols were written in pure Go and works on windows and unix systems
package ts
import (
"fmt"
"testing"
)
func ExampleGetSize() {
size, _ := GetSize()
fmt.Println(size.Col()) // Get Width
fmt.Println(size.Row()) // Get Height
fmt.Println(size.PosX()) // Get X position
fmt.Println(size.PosY()) // Get Y position
}
func TestSize(t *testing.T) {
size, err := GetSize()
if err != nil {
t.Fatal(err)
}
if size.Col() == 0 || size.Row() == 0 {
t.Fatalf("Screen Size Failed")
}
}
+14
View File
@@ -0,0 +1,14 @@
// +build freebsd netbsd openbsd
// Copyright 2014 Oleku Konko All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
// This module is a Terminal API for the Go Programming Language.
// The protocols were written in pure Go and works on windows and unix systems
package ts
const (
TIOCGWINSZ = 0x40087468
)
+64
View File
@@ -0,0 +1,64 @@
// +build windows
// Copyright 2014 Oleku Konko All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
// This module is a Terminal API for the Go Programming Language.
// The protocols were written in pure Go and works on windows and unix systems
package ts
import (
"syscall"
"unsafe"
)
var (
kernel32 = syscall.NewLazyDLL("kernel32.dll")
// Retrieves information about the specified console screen buffer.
// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms683171(v=vs.85).aspx
screenBufferInfo = kernel32.NewProc("GetConsoleScreenBufferInfo")
)
// Contains information about a console screen buffer.
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms682093(v=vs.85).aspx
type CONSOLE_SCREEN_BUFFER_INFO struct {
DwSize COORD
DwCursorPosition COORD
WAttributes uint16
SrWindow SMALL_RECT
DwMaximumWindowSize COORD
}
// Defines the coordinates of a character cell in a console screen buffer.
// The origin of the coordinate system (0,0) is at the top, left cell of the buffer.
// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms682119(v=vs.85).aspx
type COORD struct {
X, Y uint16
}
// Defines the coordinates of the upper left and lower right corners of a rectangle.
// See http://msdn.microsoft.com/en-us/library/windows/desktop/ms686311(v=vs.85).aspx
type SMALL_RECT struct {
Left, Top, Right, Bottom uint16
}
func GetSize() (ws Size, err error) {
var info CONSOLE_SCREEN_BUFFER_INFO
rc, _, err := screenBufferInfo.Call(
uintptr(syscall.Stdout),
uintptr(unsafe.Pointer(&info)))
if rc == 0 {
return ws, err
}
ws = Size{info.SrWindow.Bottom,
info.SrWindow.Right,
info.DwCursorPosition.X,
info.DwCursorPosition.Y}
return ws, nil
}
+46
View File
@@ -0,0 +1,46 @@
// +build !windows
// Copyright 2014 Oleku Konko All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
// This module is a Terminal API for the Go Programming Language.
// The protocols were written in pure Go and works on windows and unix systems
package ts
import (
"syscall"
"unsafe"
)
// Get Windows Size
func GetSize() (ws Size, err error) {
_, _, ec := syscall.Syscall(syscall.SYS_IOCTL,
uintptr(syscall.Stdout),
uintptr(TIOCGWINSZ),
uintptr(unsafe.Pointer(&ws)))
err = getError(ec)
if TIOCGWINSZ == 0 && err != nil {
ws = Size{80, 25, 0, 0}
}
return ws, err
}
func getError(ec interface{}) (err error) {
switch v := ec.(type) {
case syscall.Errno: // Some implementation return syscall.Errno number
if v != 0 {
err = syscall.Errno(v)
}
case error: // Some implementation return error
err = ec.(error)
default:
err = nil
}
return
}
+12 -4
View File
@@ -18,6 +18,7 @@ package main
import (
"encoding/json"
"fmt"
"net/http"
"runtime"
"time"
@@ -84,11 +85,15 @@ https://dl.minio.io:9000 for continuous updates`
if latest.After(current) {
var updateString string
if runtime.GOOS == "windows" {
updateString = "mc.exe cp " + mcUpdateURLParse.Scheme + "://" + mcUpdateURLParse.Host + string(mcUpdateURLParse.Separator) + updates.Platforms[runtime.GOOS] + " ."
updateString = "mc.exe cp " + mcUpdateURLParse.Scheme + "://" + mcUpdateURLParse.Host + string(mcUpdateURLParse.Separator) + updates.Platforms[runtime.GOOS] + " .\\mc.exe"
} else {
updateString = "mc cp " + mcUpdateURLParse.Scheme + "://" + mcUpdateURLParse.Host + string(mcUpdateURLParse.Separator) + updates.Platforms[runtime.GOOS] + " ${HOME}/bin/mc"
updateString = "mc cp " + mcUpdateURLParse.Scheme + "://" + mcUpdateURLParse.Host + string(mcUpdateURLParse.Separator) + updates.Platforms[runtime.GOOS] + " ./mc"
}
printUpdateNotify(updateString, "new", "old")
msg, err := printUpdateNotify(updateString, "new", "old")
if err != nil {
return "", err
}
fmt.Println(msg)
return "", nil
}
return "You are already running the most recent version of ‘mc’", nil
@@ -113,5 +118,8 @@ func runUpdateCmd(ctx *cli.Context) {
Error: iodine.New(err, nil),
})
}
console.Infoln(msg)
// no msg do not print one
if msg != "" {
console.Infoln(msg)
}
}
+37 -25
View File
@@ -23,10 +23,11 @@ import (
"strings"
"github.com/fatih/color"
"github.com/olekukonko/ts"
)
// printUpdateNotify - inspired from Yeoman project npm package https://github.com/yeoman/update-notifier
func printUpdateNotify(updateString, latestVersion, currentVersion string) {
func printUpdateNotify(updateString, latestVersion, currentVersion string) (string, error) {
// TODO - make this configurable
//
// initialize coloring
@@ -35,11 +36,13 @@ func printUpdateNotify(updateString, latestVersion, currentVersion string) {
// calculate length without color coding, due to ANSI color characters padded to actual
// string the final length is wrong than the original string length
line1Length := len(fmt.Sprintf(" Update available: "))
line2Length := len(fmt.Sprintf(" Run \"%s\" to update. ", updateString))
line1Str := fmt.Sprintf(" Update available: ")
line2Str := fmt.Sprintf(" Run \"%s\" to update. ", updateString)
line1Length := len(line1Str)
line2Length := len(line2Str)
// populate lines with color coding
line1InColor := fmt.Sprintf(" Update available: ")
line1InColor := line1Str
line2InColor := fmt.Sprintf(" Run \"%s\" to update. ", blue(updateString))
// calculate the rectangular box size
@@ -47,28 +50,37 @@ func printUpdateNotify(updateString, latestVersion, currentVersion string) {
line1Rest := maxContentWidth - line1Length
line2Rest := maxContentWidth - line2Length
// on windows terminal turn off unicode characters
var top, bottom, sideBar string
if runtime.GOOS == "windows" {
top = yellow("*" + strings.Repeat("*", maxContentWidth) + "*")
bottom = yellow("*" + strings.Repeat("*", maxContentWidth) + "*")
sideBar = yellow("|")
} else {
// color the rectangular box, use unicode characters here
top = yellow("┏" + strings.Repeat("━", maxContentWidth) + "┓")
bottom = yellow("┗" + strings.Repeat("━", maxContentWidth) + "┛")
sideBar = yellow("┃")
terminal, err := ts.GetSize()
if err != nil {
return "", err
}
// fill spaces to the rest of the area
spacePaddingLine1 := strings.Repeat(" ", line1Rest)
spacePaddingLine2 := strings.Repeat(" ", line2Rest)
// construct the final message
message := "\n" + top + "\n" +
sideBar + line1InColor + spacePaddingLine1 + sideBar + "\n" +
sideBar + line2InColor + spacePaddingLine2 + sideBar + "\n" +
bottom + "\n"
var message string
switch {
case len(line2Str) > terminal.Col():
message = "\n" + line1InColor + "\n" + line2InColor + "\n"
default:
// on windows terminal turn off unicode characters
var top, bottom, sideBar string
if runtime.GOOS == "windows" {
top = yellow("*" + strings.Repeat("*", maxContentWidth) + "*")
bottom = yellow("*" + strings.Repeat("*", maxContentWidth) + "*")
sideBar = yellow("|")
} else {
// color the rectangular box, use unicode characters here
top = yellow("┏" + strings.Repeat("━", maxContentWidth) + "┓")
bottom = yellow("┗" + strings.Repeat("━", maxContentWidth) + "┛")
sideBar = yellow("┃")
}
// fill spaces to the rest of the area
spacePaddingLine1 := strings.Repeat(" ", line1Rest)
spacePaddingLine2 := strings.Repeat(" ", line2Rest)
// construct the final message
message = "\n" + top + "\n" +
sideBar + line1InColor + spacePaddingLine1 + sideBar + "\n" +
sideBar + line2InColor + spacePaddingLine2 + sideBar + "\n" +
bottom + "\n"
}
// finally print the message
fmt.Println(message)
return message, nil
}