diff --git a/README.md b/README.md index 4c7852a..f7218f7 100644 --- a/README.md +++ b/README.md @@ -33,18 +33,18 @@ Unofficial packages are available on: [Debian](https://packages.debian.org/buste ### Commands - update Update the index - search [] Search available apps - show Show detailed info about apps - install [] Install or upgrade apps - uninstall Uninstall an app - download Download an app - devices List connected devices - list (categories) List all known values of a kind - repo Manage repositories - clean Clean index and/or cache - defaults Reset to the default settings - version Print version information + update Update the index + search [] Search available apps + show Show detailed info about apps + install [] Install or upgrade apps + uninstall Uninstall an app + download Download an app + devices List connected devices + list (categories/users) List all known values of a kind + repo Manage repositories + clean Clean index and/or cache + defaults Reset to the default settings + version Print version information An appid is just an app's unique package name. A specific version of an app can diff --git a/list.go b/list.go index 8ee84a8..0acbff3 100644 --- a/list.go +++ b/list.go @@ -4,13 +4,16 @@ package main import ( + "bufio" "fmt" "os" + "regexp" "sort" + "strings" ) var cmdList = &Command{ - UsageLine: "list (categories)", + UsageLine: "list (categories/users)", Short: "List all known values of a kind", } @@ -34,6 +37,10 @@ func runList(args []string) error { values[c] = struct{}{} } } + case "users": + if err := listUsers(); err != nil { + return err + } default: return fmt.Errorf("invalid argument") } @@ -47,3 +54,64 @@ func runList(args []string) error { } return nil } + +var userIdNameRegex = regexp.MustCompile(`UserInfo{(\d+):([^:}]*):[^}]*}`) + +func listUsers() error { + device, err := oneDevice() + if err != nil { + return err + } + cmd := device.AdbShell("pm", "list", "users") + stdout, err := cmd.StdoutPipe() + if err != nil { + return err + } + if err := cmd.Start(); err != nil { + return err + } + uidHeader := "UID" + nameHeader := "Name" + runningHeader := "Running" + scanner := bufio.NewScanner(stdout) + uids := make([]string, 0) + names := make([]string, 0) + running := make([]bool, 0) + maxUidLen := len(uidHeader) + maxNameLen := len(nameHeader) + for scanner.Scan() { + text := scanner.Text() + m := userIdNameRegex.FindStringSubmatch(text) + if m == nil { + continue + } + uid := m[1] + uids = append(uids, uid) + if uidLen := len(uid); uidLen > maxUidLen { + maxUidLen = uidLen + } + name := m[2] + names = append(names, name) + if nameLen := len(name); nameLen > maxNameLen { + maxNameLen = nameLen + } + currentRunning := false + if strings.HasSuffix(strings.TrimSpace(text), "running") { + currentRunning = true + } + running = append(running, currentRunning) + } + if len(uids) == 0 { + return nil + } + fmt.Printf("%-*s %-*s %s\n", maxUidLen, uidHeader, maxNameLen, nameHeader, runningHeader) + for i, uid := range uids { + name := names[i] + runningStr := "" + if running[i] { + runningStr = "Yes" + } + fmt.Printf("%*s %-*s %s\n", maxUidLen, uid, maxNameLen, name, runningStr) + } + return nil +}