Skip to content

Commit

Permalink
Update print code
Browse files Browse the repository at this point in the history
  • Loading branch information
xwjdsh committed Nov 13, 2017
1 parent e328181 commit f8450a7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
10 changes: 5 additions & 5 deletions actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var (
func list(c *cli.Context) error {
hosts := sshconfig.List(path, c.Args()...)
printSuccessFlag()
whiteBoldColor.Printf("Listing %d records.\n\n", len(hosts))
printMessage("Listing %d records.\n\n", len(hosts))
printHosts(hosts)
return nil
}
Expand All @@ -43,7 +43,7 @@ func add(c *cli.Context) error {
return cli.NewExitError(err, 1)
}
printSuccessFlag()
whiteBoldColor.Printf("alias[%s] added successfully.\n\n", host.Aliases)
printMessage("alias[%s] added successfully.\n\n", host.Aliases)
printHost(host)
return nil
}
Expand All @@ -66,7 +66,7 @@ func update(c *cli.Context) error {
}

printSuccessFlag()
whiteBoldColor.Printf("alias[%s] updated successfully.\n\n", host.Aliases)
printMessage("alias[%s] updated successfully.\n\n", host.Aliases)
printHost(host)
return nil
}
Expand All @@ -80,7 +80,7 @@ func delete(c *cli.Context) error {
return cli.NewExitError(err, 1)
}
printSuccessFlag()
whiteBoldColor.Printf("deleted %d records.\n", len(c.Args()))
printMessage("deleted %d records.\n", len(c.Args()))
return nil
}

Expand All @@ -101,6 +101,6 @@ func backup(c *cli.Context) error {
return cli.NewExitError(err, 1)
}
printSuccessFlag()
whiteBoldColor.Printf("backup ssh config to [%s] successfully.", backupPath)
printMessage("backup ssh config to [%s] successfully.", backupPath)
return nil
}
33 changes: 22 additions & 11 deletions print.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@ import (
)

var (
whiteBoldColor = color.New(color.FgWhite, color.Bold)
yellowBoldColor = color.New(color.FgYellow, color.Bold)
magentaBoldColor = color.New(color.FgMagenta, color.Bold)
messageStyle = color.New(color.FgWhite, color.Bold)
aliasStyle = color.New(color.FgYellow, color.Bold)
globalStyle = color.New(color.FgMagenta, color.Bold)
globalKeyStyle = color.New(color.FgCyan, color.Bold)

successColor = color.New(color.BgGreen, color.FgWhite)
errorColor = color.New(color.BgRed, color.FgWhite)
successStyle = color.New(color.BgGreen, color.FgWhite)
errorStyle = color.New(color.BgRed, color.FgWhite)
)

func printSuccessFlag() {
successColor.Printf("%-9s", " success")
successStyle.Printf("%-9s", " success")
}

func printErrorFlag() {
errorColor.Printf("%-7s", " error")
errorStyle.Printf("%-7s", " error")
}

func printErrorWithHelp(c *cli.Context, err error) error {
Expand All @@ -45,15 +46,25 @@ func printHosts(hosts []*utils.HostConfig) {
}
}

func printMessage(format string, a ...interface{}) {
messageStyle.Printf(format, a)
}

func printHost(host *utils.HostConfig) {
if host.Aliases == "*" {
magentaBoldColor.Printf("\t (*) Global Configs\n")
isGlobal := host.Aliases == "*"
if isGlobal {
globalStyle.Printf("\t(*) Global Configs\n")
} else {
yellowBoldColor.Printf("\t%s", host.Aliases)
aliasStyle.Printf("\t%s", host.Aliases)
fmt.Printf(" -> %s\n", host.Connect)
}
for k, v := range host.Config {
fmt.Printf("\t\t%s = %s\n", k, v)
if isGlobal {
globalKeyStyle.Printf("\t\t%s ", k)
fmt.Printf("= %s\n", v)
} else {
fmt.Printf("\t\t%s = %s\n", k, v)
}
}
fmt.Println()
}

0 comments on commit f8450a7

Please # to comment.