Skip to content
This repository has been archived by the owner on Jul 14, 2022. It is now read-only.

Commit

Permalink
📝 Better Color Outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
athul committed Jan 25, 2020
1 parent 304e2fa commit b2b98b9
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
9 changes: 6 additions & 3 deletions methods/fns.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import (

// Formatresp formats the Response with Indents and Colors
func formatresp(resp *http.Response) string {
c := color.New(color.FgCyan)
c := color.New(color.FgCyan, color.Bold)
magenta := color.New(color.FgHiMagenta)
yellow := color.New(color.FgHiYellow)
body, err := ioutil.ReadAll(resp.Body)
str := string(body)
var obj map[string]interface{}
Expand All @@ -23,9 +25,10 @@ func formatresp(resp *http.Response) string {
f.Indent = 6
s, _ := f.Marshal(obj)
for key, value := range resp.Header {
c.Print(key, " : ", value, "\n")
c.Print(key, " : ")
magenta.Print(value, "\n")
}
retbody := fmt.Sprintf("\nStatus:\t\t%s\n\nStatusCode:\t%d\n\n%s\n", resp.Status, resp.StatusCode, string(s))
retbody := yellow.Sprintf("\nStatus:\t\t%s\n\nStatusCode:\t%d\n", resp.Status, resp.StatusCode) + fmt.Sprintf("\n%s\n", string(s))
if err != nil {
log.Println("Error on response.\n[ERRO] -", err)
}
Expand Down
2 changes: 1 addition & 1 deletion methods/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ func Getbasic(c *cli.Context) error {
}
defer resp.Body.Close()
s := formatresp(resp)
fmt.Printf("\n%s", s)
fmt.Println(s)
return nil
}
2 changes: 1 addition & 1 deletion methods/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ func Postbasic(c *cli.Context) {
}
defer resp.Body.Close()
s := formatresp(resp)
fmt.Println("response Body:", s)
fmt.Println(s)
}
2 changes: 1 addition & 1 deletion methods/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ func Putbasic(c *cli.Context) {
defer resp.Body.Close()

s := formatresp(resp)
fmt.Println("\nresponse Body:\n", s)
fmt.Println(s)
}

0 comments on commit b2b98b9

Please sign in to comment.