Skip to content

Commit

Permalink
Added example for color usage
Browse files Browse the repository at this point in the history
  • Loading branch information
poppycompass committed Dec 13, 2017
1 parent 14fffe7 commit ef597ab
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,37 @@ Hello Someusername
```


### Output with Color and Bold letters
If you want to change the output such as coloring and bold letters, you can do as follows(example is using [fatih/color](https://github.com/fatih/color)).

```go
import "github.com/fatih/color"

cyan := color.New(color.FgCyan).SprintFunc()
yellow := color.New(color.FgYellow).SprintFunc()
boldRed := color.New(color.FgRed, color.Bold).SprintFunc()
shell.AddCmd(&ishell.Cmd{
Name: "color",
Help: "color print",
Func: func(c *ishell.Context) {
c.Print(cyan("cyan\n"))
c.Println(yellow("yellow"))
c.Printf("%s\n", boldRed("bold red"))
},
}
```
Output
```bash
$ go run main.go
Sample Interactive Shell
>>> color
cyan
yellow
bold red
>>>
```


### Example
Available [here](https://github.com/abiosoft/ishell/blob/master/example/main.go).
```sh
Expand Down
14 changes: 14 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/abiosoft/ishell"
"github.com/fatih/color"
)

func main() {
Expand Down Expand Up @@ -190,6 +191,19 @@ This is another line of it.
},
})

cyan := color.New(color.FgCyan).SprintFunc()
yellow := color.New(color.FgYellow).SprintFunc()
boldRed := color.New(color.FgRed, color.Bold).SprintFunc()
shell.AddCmd(&ishell.Cmd{
Name: "color",
Help: "color print",
Func: func(c *ishell.Context) {
c.Print(cyan("cyan\n"))
c.Println(yellow("yellow"))
c.Printf("%s\n", boldRed("bold red"))
},
})

// when started with "exit" as first argument, assume non-interactive execution
if len(os.Args) > 1 && os.Args[1] == "exit" {
shell.Process(os.Args[2:]...)
Expand Down

0 comments on commit ef597ab

Please sign in to comment.