Compare commits

...

7 Commits

6 changed files with 95 additions and 54 deletions

1
.gitignore vendored
View File

@ -15,3 +15,4 @@
# Dependency directories (remove the comment below to include it) # Dependency directories (remove the comment below to include it)
# vendor/ # vendor/
.lumerc

View File

@ -4,10 +4,6 @@ import (
"flag" "flag"
) )
var (
idWidth, locationWidth, groupWidth, labelWidth, lastSeenWidth, powerWidth int
)
func init() { func init() {
var cmdName string = "ls" var cmdName string = "ls"
fs := flag.NewFlagSet(cmdName, flag.ExitOnError) fs := flag.NewFlagSet(cmdName, flag.ExitOnError)

Binary file not shown.

19
cmd/lume/init_windows.go Normal file
View File

@ -0,0 +1,19 @@
// +build windows
// https://stackoverflow.com/a/52579002
package main
import (
"os"
"golang.org/x/sys/windows"
)
func init() {
stdout := windows.Handle(os.Stdout.Fd())
var originalMode uint32
windows.GetConsoleMode(stdout, &originalMode)
windows.SetConsoleMode(stdout, originalMode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING)
}

View File

@ -9,25 +9,23 @@ import (
lifx "git.kill0.net/chill9/lume" lifx "git.kill0.net/chill9/lume"
lumecmd "git.kill0.net/chill9/lume/cmd" lumecmd "git.kill0.net/chill9/lume/cmd"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"golang.org/x/sys/windows"
) )
const lumercFile = ".lumerc" const lumercFile string = ".lumerc"
func main() { func main() {
var originalMode uint32
stdout := windows.Handle(os.Stdout.Fd())
windows.GetConsoleMode(stdout, &originalMode)
windows.SetConsoleMode(stdout, originalMode|windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING)
defer windows.SetConsoleMode(stdout, originalMode)
var config lumecmd.Config var config lumecmd.Config
config = loadConfig()
if config.AccessToken == "" { configPath := getConfigPath()
config.AccessToken = os.Getenv("LIFX_ACCESS_TOKEN") if _, err := toml.DecodeFile(configPath, &config); err != nil {
fmt.Printf("fatal: failed to parse %s\n", configPath)
fmt.Println(err)
os.Exit(1)
}
envAccessToken := os.Getenv("LIFX_ACCESS_TOKEN")
if envAccessToken != "" {
config.AccessToken = envAccessToken
} }
if config.AccessToken == "" { if config.AccessToken == "" {
@ -54,7 +52,7 @@ func main() {
fs := cmd.Flags fs := cmd.Flags
fs.Parse(os.Args[2:]) fs.Parse(os.Args[2:])
cmdArgs.Flags = lumecmd.Flags{fs} cmdArgs.Flags = lumecmd.Flags{FlagSet: fs}
exitCode, err := cmd.Func(cmdArgs) exitCode, err := cmd.Func(cmdArgs)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "fatal: %s\n", err) fmt.Fprintf(os.Stderr, "fatal: %s\n", err)
@ -62,10 +60,10 @@ func main() {
os.Exit(exitCode) os.Exit(exitCode)
} }
func loadConfig() lumecmd.Config { func getConfigPath() string {
var config lumecmd.Config
var tryPath, configPath string var tryPath, configPath string
// ~/.lumerc
homeDir, err := os.UserHomeDir() homeDir, err := os.UserHomeDir()
if err == nil { if err == nil {
tryPath = path.Join(homeDir, lumercFile) tryPath = path.Join(homeDir, lumercFile)
@ -74,6 +72,7 @@ func loadConfig() lumecmd.Config {
} }
} }
// ./.lumerc
cwd, err := os.Getwd() cwd, err := os.Getwd()
if err == nil { if err == nil {
tryPath = path.Join(cwd, lumercFile) tryPath = path.Join(cwd, lumercFile)
@ -82,9 +81,5 @@ func loadConfig() lumecmd.Config {
} }
} }
if configPath != "" { return configPath
toml.DecodeFile(configPath, &config)
}
return config
} }

View File

@ -2,11 +2,12 @@ package lumecmd
import ( import (
"fmt" "fmt"
"sort"
"strconv" "strconv"
"strings" "strings"
"time" "time"
"git.kill0.net/chill9/lume" lifx "git.kill0.net/chill9/lume"
) )
func powerColor(s string) string { func powerColor(s string) string {
@ -28,78 +29,88 @@ func statusColor(s lifx.Status) string {
} }
func PrintResults(res []lifx.Result) { func PrintResults(res []lifx.Result) {
var length, idWidth, labelWidth, statusWidth int var length int
var widths map[string]int
widths = make(map[string]int)
for _, r := range res { for _, r := range res {
length = len(r.Id) length = len(r.Id)
if idWidth < length { if widths["id"] < length {
idWidth = length widths["id"] = length
} }
length = len(r.Label) length = len(r.Label)
if labelWidth < length { if widths["label"] < length {
labelWidth = length widths["label"] = length
} }
length = len(r.Status) length = len(r.Status)
if statusWidth < length { if widths["status"] < length {
statusWidth = length widths["status"] = length
} }
} }
sortResults(res)
for _, r := range res { for _, r := range res {
fmt.Printf("%*s %*s %*s\n", fmt.Printf("%*s %*s %*s\n",
idWidth, r.Id, widths["id"], r.Id,
labelWidth, r.Label, widths["label"], r.Label,
statusWidth, statusColor(r.Status)) widths["status"], statusColor(r.Status))
} }
} }
func PrintLights(lights []lifx.Light) { func PrintLights(lights []lifx.Light) {
var length int var length int
var widths map[string]int
widths = make(map[string]int)
for _, l := range lights { for _, l := range lights {
length = len(l.Id) length = len(l.Id)
if idWidth < length { if widths["id"] < length {
idWidth = length widths["id"] = length
} }
length = len(l.Location.Name) length = len(l.Location.Name)
if locationWidth < length { if widths["location"] < length {
locationWidth = length widths["location"] = length
} }
length = len(l.Group.Name) length = len(l.Group.Name)
if groupWidth < length { if widths["group"] < length {
groupWidth = length widths["group"] = length
} }
length = len(l.Label) length = len(l.Label)
if labelWidth < length { if widths["label"] < length {
labelWidth = length widths["label"] = length
} }
length = len(l.LastSeen.Local().Format(time.RFC3339)) length = len(l.LastSeen.Local().Format(time.RFC3339))
if lastSeenWidth < length { if widths["last_seen"] < length {
lastSeenWidth = length widths["last_seen"] = length
} }
length = len(l.Power) length = len(l.Power)
if powerWidth < length { if widths["power"] < length {
powerWidth = length widths["power"] = length
} }
} }
sortLights(lights)
fmt.Printf("total %d\n", len(lights)) fmt.Printf("total %d\n", len(lights))
for _, l := range lights { for _, l := range lights {
fmt.Printf( fmt.Printf(
"%*s %*s %*s %*s %*s %-*s\n", "%*s %*s %*s %*s %*s %-*s\n",
idWidth, l.Id, widths["id"], l.Id,
locationWidth, l.Location.Name, widths["loction"], l.Location.Name,
groupWidth, l.Group.Name, widths["group"], l.Group.Name,
labelWidth, l.Label, widths["label"], l.Label,
lastSeenWidth, l.LastSeen.Local().Format(time.RFC3339), widths["last_seen"], l.LastSeen.Local().Format(time.RFC3339),
powerWidth, powerColor(l.Power), widths["power"], powerColor(l.Power),
) )
} }
} }
@ -121,3 +132,22 @@ func parseRGB(s string) (lifx.RGBColor, error) {
} }
return lifx.NewRGBColor(uint8(r), uint8(g), uint8(b)) return lifx.NewRGBColor(uint8(r), uint8(g), uint8(b))
} }
func sortLights(lights []lifx.Light) {
sort.Slice(lights, func(i, j int) bool {
if lights[i].Group.Name < lights[j].Group.Name {
return true
}
if lights[i].Group.Name > lights[j].Group.Name {
return false
}
return lights[i].Label < lights[j].Label
})
}
func sortResults(res []lifx.Result) {
sort.Slice(res, func(i, j int) bool {
return res[i].Label < res[j].Label
})
}