Created
December 20, 2016 14:41
-
-
Save emctoo/e2524107317c3168a582ec9a27a9cce8 to your computer and use it in GitHub Desktop.
feh momentum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"log" | |
"math/rand" | |
"os" | |
"os/exec" | |
"syscall" | |
"time" | |
) | |
func momentumVersion(prefix string) (error, string) { | |
files, err := ioutil.ReadDir(prefix) | |
if err != nil { | |
log.Println("不能读取目录") | |
return nil, "" | |
} | |
if len(files) != 1 { | |
log.Fatalln("版本路径获取错误") | |
return nil, "" | |
} | |
return nil, files[0].Name() | |
} | |
func momentumPictures(prefix string) ([]string, error) { | |
files, err := ioutil.ReadDir(prefix) | |
if err != nil { | |
log.Println("不能读取目录") | |
return nil, err | |
} | |
// outputFiles := make([]string, len(files)) | |
var outputFiles []string | |
for _, file := range files { | |
outputFiles = append(outputFiles, file.Name()) | |
} | |
return outputFiles, nil | |
} | |
func main() { | |
fmt.Println("fuck") | |
// 不确定这个app id是不是保持不变的。可能是作为参数。 | |
momentumAppId := "laookkfknpbbblfpciffpaejjkokdgca" | |
chromiumExntensionPath := "/.config/chromium/Default/Extensions" | |
home := os.Getenv("HOME") | |
if home == "" { | |
log.Fatal("HOME 变量获取错误") | |
} | |
path := fmt.Sprintf("%s%s/%s", home, chromiumExntensionPath, momentumAppId) | |
log.Println(path) | |
err, version := momentumVersion(path) | |
if err != nil { | |
log.Fatal("获取版本错误") | |
} | |
log.Printf("version: %s\n", version) | |
path = fmt.Sprintf("%s/%s/backgrounds", path, version) | |
log.Println(path) | |
files, err := momentumPictures(path) | |
if err != nil { | |
log.Fatal("no pictures") | |
} | |
for _, file := range files { | |
log.Printf("file: [%s]\n", file) | |
} | |
source := rand.NewSource(time.Now().UnixNano()) | |
random := rand.New(source) | |
nth := random.Intn(len(files)) | |
picture := fmt.Sprintf("%s/%s", path, files[nth]) | |
log.Printf("picture: %s\n", picture) | |
feh, err := exec.LookPath("feh") | |
if err != nil { | |
log.Fatal("找不到可执行文件") | |
} | |
argv := []string{"feh", "--bg-scale", picture} | |
env := os.Environ() | |
if execError := syscall.Exec(feh, argv, env); execError != nil { | |
log.Fatal("执行文件错误") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment