Skip to content

Commit

Permalink
Merge pull request #11 from wangzi1325/main
Browse files Browse the repository at this point in the history
feat: support `screenshot`
  • Loading branch information
electricbubble authored Aug 16, 2021
2 parents 4645556 + 5746b75 commit 0ba17c0
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ $ gidevice syslog
$ gidevice crashreport /path/.../local/dir/ -e -k
```

#### Screenshot

```shell
$ gidevice screenshot
$ gidevice screenshot -u=39xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx7 -o=/path/..../screenshot.png
```

## Thanks

| |About|
Expand Down
63 changes: 63 additions & 0 deletions cmd/screenshot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package cmd

import (
"fmt"
"image"
"image/jpeg"
"image/png"
"log"
"os"
"time"

"github.com/electricbubble/gidevice-cli/internal"
"github.com/spf13/cobra"
)

// screenshotCmd represents the screenshot command
var screenshotCmd = &cobra.Command{
Use: "screenshot",
Short: "Device screenshot",
Run: func(cmd *cobra.Command, args []string) {
udid, _ := cmd.Flags().GetString("udid")
outputFile, _ := cmd.Flags().GetString("outputFile")

d, err := internal.GetDeviceFromCommand(udid)
internal.ErrorExit(err)

raw, err := d.Screenshot()
if err != nil {
log.Fatalln(err)
}

img, format, err := image.Decode(raw)
if err != nil {
log.Fatalln(err)
}
if len(outputFile) == 0 {
now := time.Now()
outputFile = fmt.Sprintf("%d%d%d%d%d%d", now.Year(), int(now.Month()),
now.Day(), now.Hour(), now.Minute(), now.Second()) + "." + format
}
file, err := os.Create(outputFile)
if err != nil {
log.Fatalln(err)
}
defer func() { _ = file.Close() }()
switch format {
case "png":
err = png.Encode(file, img)
case "jpeg":
err = jpeg.Encode(file, img, nil)
}
if err != nil {
log.Fatalln(err)
}
log.Println(file.Name())
},
}

func init() {
rootCmd.AddCommand(screenshotCmd)
screenshotCmd.Flags().StringP("udid", "u", "", "Device uuid")
screenshotCmd.Flags().StringP("outputFile", "o", "", "Output file")
}
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/electricbubble/gidevice v0.3.0 h1:7BviJ3frVmV1iIBRl1max2oDczTQ6H7XwsqSHgGiwjI=
github.com/electricbubble/gidevice v0.3.0/go.mod h1:hWRHIPf4uyiEB56hnVHVvu6MoVg7RlJY8ZV2FVgLKZA=
github.com/electricbubble/gidevice v0.3.2 h1:s//oKXaC8hihiu3EYoaPwnyKfrmaEPxMVXjoXxRXb78=
github.com/electricbubble/gidevice v0.3.2/go.mod h1:hWRHIPf4uyiEB56hnVHVvu6MoVg7RlJY8ZV2FVgLKZA=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
Expand Down

0 comments on commit 0ba17c0

Please sign in to comment.