Skip to content

Commit

Permalink
fix lint errors
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
  • Loading branch information
aluzzardi committed Nov 1, 2018
1 parent 219e9ce commit c05e17c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 45 deletions.
53 changes: 53 additions & 0 deletions pkg/ui/tree.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package ui

import (
"io/ioutil"
"os"
"path"
"strings"

"github.com/xlab/treeprint"
)

// Tree prints a source tree.
func Tree(p string, ignore []string) error {
root := treeprint.New()
root.SetValue(p)
if err := walk(p, root, ignore); err != nil {
return err
}
Verbose(strings.TrimSpace(root.String()))
return nil
}

func walk(p string, node treeprint.Tree, ignore []string) error {
shouldIgnore := func(f os.FileInfo) bool {
for _, i := range ignore {
if f.Name() == i {
return true
}
}
return false
}

files, err := ioutil.ReadDir(p)
if err != nil {
return err
}
for _, file := range files {
if shouldIgnore(file) {
continue
}
if file.IsDir() {
sub := node.AddBranch(file.Name())
if err := walk(path.Join(p, file.Name()), sub, ignore); err != nil {
return err
}
continue
}

node.AddNode(file.Name())
}

return nil
}
55 changes: 10 additions & 45 deletions pkg/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ package ui

import (
"fmt"
"io/ioutil"
"os"
"path"
"strings"
"unicode/utf8"

"github.com/mitchellh/colorstring"
spin "github.com/tj/go-spin"
"github.com/xlab/treeprint"
"golang.org/x/crypto/ssh/terminal"
)

Expand All @@ -26,39 +23,48 @@ func init() {
spinner.Set(spin.Spin1)
}

// EnableColors enables or disable output coloring.
func EnableColors(enabled bool) {
colorize.Disable = !enabled
}

// Info prints an info message.
func Info(msg string, args ...interface{}) {
fmt.Printf(colorize.Color("[bold][blue]==> [reset][bold]%s\n"), fmt.Sprintf(msg, args...))
}

// Verbose prints a verbose message.
func Verbose(msg string, args ...interface{}) {
fmt.Printf(colorize.Color("[dim]%s\n"), fmt.Sprintf(msg, args...))
}

// Success prints a success message.
func Success(msg string, args ...interface{}) {
fmt.Printf(colorize.Color("[bold][green]✔[reset][bold] %s\n"), fmt.Sprintf(msg, args...))
}

// Error prints an error message.
func Error(msg string, args ...interface{}) {
fmt.Printf(colorize.Color("[bold][red]✗[reset][bold] %s\n"), fmt.Sprintf(msg, args...))
}

// Fatal prints an error message and exits.
func Fatal(msg string, args ...interface{}) {
Error(msg, args...)
os.Exit(1)
}

// Small returns a `small` colored string.
func Small(msg string) string {
return colorize.Color("[dim]" + msg)
}

// Emphasize returns a `emphasized` colored string.
func Emphasize(msg string) string {
return colorize.Color("[bold][yellow]" + msg)
}

// ConsoleWidth returns the console's width.
func ConsoleWidth() int {
width, _, err := terminal.GetSize(0)
if err != nil || width <= 0 {
Expand All @@ -67,6 +73,7 @@ func ConsoleWidth() int {
return width
}

// Live is used to print a live message. Subsequent calls will replace the line.
func Live(msg string) {
// Format the message.
msg = fmt.Sprintf("%s %s", spinner.Next(), strings.TrimSpace(msg))
Expand All @@ -87,45 +94,3 @@ func Live(msg string) {

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

func Tree(p string, ignore []string) error {
root := treeprint.New()
root.SetValue(p)
if err := walk(p, root, ignore); err != nil {
return err
}
Verbose(strings.TrimSpace(root.String()))
return nil
}

func walk(p string, node treeprint.Tree, ignore []string) error {
shouldIgnore := func(f os.FileInfo) bool {
for _, i := range ignore {
if f.Name() == i {
return true
}
}
return false
}

files, err := ioutil.ReadDir(p)
if err != nil {
return err
}
for _, file := range files {
if shouldIgnore(file) {
continue
}
if file.IsDir() {
sub := node.AddBranch(file.Name())
if err := walk(path.Join(p, file.Name()), sub, ignore); err != nil {
return err
}
continue
}

node.AddNode(file.Name())
}

return nil
}

0 comments on commit c05e17c

Please sign in to comment.