Skip to content

Commit

Permalink
build UI: Better support for terminal sizing
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Luzzardi <[email protected]>
  • Loading branch information
aluzzardi committed Nov 1, 2018
1 parent 611f46c commit cf96704
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
7 changes: 6 additions & 1 deletion pkg/builder/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ func (p *Parser) processProgress(text string) bool {
total int
)

// Don't show progress bars on small terminals.
if ui.ConsoleWidth() < 80 {
return false
}

sr := strings.NewReader(text)
// Check if this is a progressbar-style output (e.g. "X out of Y").
if n, _ := fmt.Fscanf(sr, "(%d/%d) Wrote", &step, &total); n != 2 {
Expand All @@ -89,7 +94,7 @@ func (p *Parser) processProgress(text string) bool {
BarStart: "[",
BarEnd: "]",
}),
progressbar.OptionSetWidth(ui.ConsoleWidth()/2),
progressbar.OptionSetWidth(ui.ConsoleWidth()-20),
)
}

Expand Down
27 changes: 18 additions & 9 deletions pkg/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path"
"strings"
"unicode/utf8"

spin "github.com/tj/go-spin"
"github.com/ttacon/chalk"
Expand All @@ -17,6 +18,10 @@ var (
spinner = spin.New()
)

func init() {
spinner.Set(spin.Spin1)
}

func Info(msg string, args ...interface{}) {
fmt.Printf("%s %s\n", chalk.Bold.TextStyle(chalk.Blue.Color("==>")), chalk.Bold.TextStyle(fmt.Sprintf(msg, args...)))
}
Expand Down Expand Up @@ -55,20 +60,24 @@ func ConsoleWidth() int {
}

func Live(msg string) {
lineLength := ConsoleWidth() - 5
msg = strings.TrimSpace(msg)
// Format the message.
msg = fmt.Sprintf("%s %s", spinner.Next(), strings.TrimSpace(msg))

// Get the actual console width.
lineLength := ConsoleWidth()

// Truncate length
if len(msg) > lineLength {
msg = msg[0:lineLength-2] + "…"
// Shorten the message until it fits.
for utf8.RuneCountInString(msg) > lineLength {
msg = msg[0:len(msg)-4] + "…"
}

// Pad with spaces to clear previous line.
for len(msg) < lineLength {
msg += " "
// Pad the message with spaces until it takes the entire line.
// This is in order to clear the previous line.
for utf8.RuneCountInString(msg) < lineLength {
msg = msg + " "
}

fmt.Printf("%s %s\r", spinner.Next(), Small(msg))
fmt.Printf("%s\r", Small(msg))
}

func Tree(p string, ignore []string) error {
Expand Down

0 comments on commit cf96704

Please sign in to comment.