Use terminal color library
This commit is contained in:
		
							
								
								
									
										24
									
								
								cmd/print.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								cmd/print.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,24 @@
 | 
				
			|||||||
 | 
					package lumecmd
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"git.kill0.net/chill9/lifx-go"
 | 
				
			||||||
 | 
						"github.com/fatih/color"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func ColorizePower(s string) string {
 | 
				
			||||||
 | 
						c := color.New(color.FgRed)
 | 
				
			||||||
 | 
						if s == "on" {
 | 
				
			||||||
 | 
							c = color.New(color.FgGreen)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return c.Sprint(s)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func ColorizeStatus(s lifx.Status) string {
 | 
				
			||||||
 | 
						c := color.New(color.FgRed)
 | 
				
			||||||
 | 
						if s == "ok" {
 | 
				
			||||||
 | 
							c = color.New(color.FgGreen)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return c.Sprint(s)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -42,7 +42,7 @@ func ShowCmd(args CmdArgs) (int, error) {
 | 
				
			|||||||
			"Light ID: %s, %s, Power: %s\n",
 | 
								"Light ID: %s, %s, Power: %s\n",
 | 
				
			||||||
			l.Id,
 | 
								l.Id,
 | 
				
			||||||
			connected(l.Connected),
 | 
								connected(l.Connected),
 | 
				
			||||||
			powerColor(l.Power),
 | 
								ColorizePower(l.Power),
 | 
				
			||||||
		)
 | 
							)
 | 
				
			||||||
		indent += Tabstop
 | 
							indent += Tabstop
 | 
				
			||||||
		PrintfWithIndent(indent, "Label: %s, ID: %s\n", l.Label, l.Id)
 | 
							PrintfWithIndent(indent, "Label: %s, ID: %s\n", l.Label, l.Id)
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										22
									
								
								cmd/util.go
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								cmd/util.go
									
									
									
									
									
								
							@@ -11,24 +11,6 @@ import (
 | 
				
			|||||||
	"git.kill0.net/chill9/lifx-go"
 | 
						"git.kill0.net/chill9/lifx-go"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func powerColor(s string) string {
 | 
					 | 
				
			||||||
	fs := "\033[1;31m%s\033[0m"
 | 
					 | 
				
			||||||
	if s == "on" {
 | 
					 | 
				
			||||||
		fs = "\033[1;32m%s\033[0m"
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return fmt.Sprintf(fs, s)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func statusColor(s lifx.Status) string {
 | 
					 | 
				
			||||||
	fs := "\033[1;31m%s\033[0m"
 | 
					 | 
				
			||||||
	if s == "ok" {
 | 
					 | 
				
			||||||
		fs = "\033[1;32m%s\033[0m"
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return fmt.Sprintf(fs, s)
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func PrintResults(res []lifx.Result) {
 | 
					func PrintResults(res []lifx.Result) {
 | 
				
			||||||
	var length int
 | 
						var length int
 | 
				
			||||||
	var widths map[string]int
 | 
						var widths map[string]int
 | 
				
			||||||
@@ -58,7 +40,7 @@ func PrintResults(res []lifx.Result) {
 | 
				
			|||||||
		fmt.Printf("%*s %*s %*s\n",
 | 
							fmt.Printf("%*s %*s %*s\n",
 | 
				
			||||||
			widths["id"], r.Id,
 | 
								widths["id"], r.Id,
 | 
				
			||||||
			widths["label"], r.Label,
 | 
								widths["label"], r.Label,
 | 
				
			||||||
			widths["status"], statusColor(r.Status))
 | 
								widths["status"], ColorizeStatus(r.Status))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -111,7 +93,7 @@ func PrintLights(lights []lifx.Light) {
 | 
				
			|||||||
			widths["group"], l.Group.Name,
 | 
								widths["group"], l.Group.Name,
 | 
				
			||||||
			widths["label"], l.Label,
 | 
								widths["label"], l.Label,
 | 
				
			||||||
			widths["last_seen"], l.LastSeen.Local().Format(time.RFC3339),
 | 
								widths["last_seen"], l.LastSeen.Local().Format(time.RFC3339),
 | 
				
			||||||
			widths["power"], powerColor(l.Power),
 | 
								widths["power"], ColorizePower(l.Power),
 | 
				
			||||||
		)
 | 
							)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										1
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								go.mod
									
									
									
									
									
								
							@@ -5,5 +5,6 @@ go 1.15
 | 
				
			|||||||
require (
 | 
					require (
 | 
				
			||||||
	git.kill0.net/chill9/lifx-go v0.0.0-20210215004437-f86c28b0a5ef
 | 
						git.kill0.net/chill9/lifx-go v0.0.0-20210215004437-f86c28b0a5ef
 | 
				
			||||||
	github.com/BurntSushi/toml v0.3.1
 | 
						github.com/BurntSushi/toml v0.3.1
 | 
				
			||||||
 | 
						github.com/fatih/color v1.10.0
 | 
				
			||||||
	golang.org/x/sys v0.0.0-20210110051926-789bb1bd4061
 | 
						golang.org/x/sys v0.0.0-20210110051926-789bb1bd4061
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										8
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								go.sum
									
									
									
									
									
								
							@@ -2,5 +2,13 @@ git.kill0.net/chill9/lifx-go v0.0.0-20210215004437-f86c28b0a5ef h1:8yyXAk+qiRvro
 | 
				
			|||||||
git.kill0.net/chill9/lifx-go v0.0.0-20210215004437-f86c28b0a5ef/go.mod h1:ZFKIcwdJ4Nqlrkn/eUHbeLt0NVhFsfxBREkVoA+jzUc=
 | 
					git.kill0.net/chill9/lifx-go v0.0.0-20210215004437-f86c28b0a5ef/go.mod h1:ZFKIcwdJ4Nqlrkn/eUHbeLt0NVhFsfxBREkVoA+jzUc=
 | 
				
			||||||
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
 | 
					github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
 | 
				
			||||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
 | 
					github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
 | 
				
			||||||
 | 
					github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
 | 
				
			||||||
 | 
					github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
 | 
				
			||||||
 | 
					github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
 | 
				
			||||||
 | 
					github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
 | 
				
			||||||
 | 
					github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
 | 
				
			||||||
 | 
					github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
 | 
				
			||||||
 | 
					golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 | 
				
			||||||
 | 
					golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 | 
				
			||||||
golang.org/x/sys v0.0.0-20210110051926-789bb1bd4061 h1:DQmQoKxQWtyybCtX/3dIuDBcAhFszqq8YiNeS6sNu1c=
 | 
					golang.org/x/sys v0.0.0-20210110051926-789bb1bd4061 h1:DQmQoKxQWtyybCtX/3dIuDBcAhFszqq8YiNeS6sNu1c=
 | 
				
			||||||
golang.org/x/sys v0.0.0-20210110051926-789bb1bd4061/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 | 
					golang.org/x/sys v0.0.0-20210110051926-789bb1bd4061/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user