Skip to content

Commit ef597ab

Browse files
committed
Added example for color usage
1 parent 14fffe7 commit ef597ab

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,37 @@ Hello Someusername
257257
```
258258

259259

260+
### Output with Color and Bold letters
261+
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)).
262+
263+
```go
264+
import "github.com/fatih/color"
265+
266+
cyan := color.New(color.FgCyan).SprintFunc()
267+
yellow := color.New(color.FgYellow).SprintFunc()
268+
boldRed := color.New(color.FgRed, color.Bold).SprintFunc()
269+
shell.AddCmd(&ishell.Cmd{
270+
Name: "color",
271+
Help: "color print",
272+
Func: func(c *ishell.Context) {
273+
c.Print(cyan("cyan\n"))
274+
c.Println(yellow("yellow"))
275+
c.Printf("%s\n", boldRed("bold red"))
276+
},
277+
}
278+
```
279+
Output
280+
```bash
281+
$ go run main.go
282+
Sample Interactive Shell
283+
>>> color
284+
cyan
285+
yellow
286+
bold red
287+
>>>
288+
```
289+
290+
260291
### Example
261292
Available [here](https://github.com/abiosoft/ishell/blob/master/example/main.go).
262293
```sh

example/main.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
"github.com/abiosoft/ishell"
11+
"github.com/fatih/color"
1112
)
1213

1314
func main() {
@@ -190,6 +191,19 @@ This is another line of it.
190191
},
191192
})
192193

194+
cyan := color.New(color.FgCyan).SprintFunc()
195+
yellow := color.New(color.FgYellow).SprintFunc()
196+
boldRed := color.New(color.FgRed, color.Bold).SprintFunc()
197+
shell.AddCmd(&ishell.Cmd{
198+
Name: "color",
199+
Help: "color print",
200+
Func: func(c *ishell.Context) {
201+
c.Print(cyan("cyan\n"))
202+
c.Println(yellow("yellow"))
203+
c.Printf("%s\n", boldRed("bold red"))
204+
},
205+
})
206+
193207
// when started with "exit" as first argument, assume non-interactive execution
194208
if len(os.Args) > 1 && os.Args[1] == "exit" {
195209
shell.Process(os.Args[2:]...)

0 commit comments

Comments
 (0)