go fmt ./...

This commit is contained in:
cuu
2021-10-11 13:55:54 +08:00
parent 0fe6399441
commit 7382cc40b1
88 changed files with 10028 additions and 10478 deletions

View File

@@ -1,19 +1,19 @@
package UI
import (
"os"
"log"
"path/filepath"
"strings"
"fmt"
"bufio"
"bytes"
"io"
"bytes"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"syscall"
"os/exec"
"runtime"
"strings"
"syscall"
"github.com/cuu/gogame/display"
"github.com/clockworkpi/LauncherGoDev/sysgo"
@@ -27,14 +27,14 @@ func ShowErr(e error) {
func Assert(e error) {
if e != nil {
log.Fatal("Assert: " , e)
log.Fatal("Assert: ", e)
}
}
func CheckAndPanic(e error) {
if e != nil {
panic(e)
}
if e != nil {
panic(e)
}
}
func Abs(n int) int {
@@ -42,42 +42,41 @@ func Abs(n int) int {
return (n ^ y) - y
}
func SkinMap(orig_file_or_dir string) string {
DefaultSkin := "skin/default/"
ret := ""
if strings.HasPrefix(orig_file_or_dir, "/home/cpi/apps/Menu") {
ret = strings.Replace(orig_file_or_dir,"/home/cpi/apps/Menu/", sysgo.SKIN+"/Menu/GameShell/",-1)
if FileExists(ret) == false {
ret = DefaultSkin+orig_file_or_dir
}
}else { // there is no need to add insert "sysgo" in the middle
ret = sysgo.SKIN+orig_file_or_dir
ret = strings.Replace(orig_file_or_dir, "/home/cpi/apps/Menu/", sysgo.SKIN+"/Menu/GameShell/", -1)
if FileExists(ret) == false {
ret = DefaultSkin+orig_file_or_dir
ret = DefaultSkin + orig_file_or_dir
}
} else { // there is no need to add insert "sysgo" in the middle
ret = sysgo.SKIN + orig_file_or_dir
if FileExists(ret) == false {
ret = DefaultSkin + orig_file_or_dir
}
}
if FileExists(ret) {
return ret
}else { // if not existed both in default or custom skin ,return where it is
} else { // if not existed both in default or custom skin ,return where it is
return orig_file_or_dir
}
}
func CmdClean(cmdpath string) string {
spchars := "\\`$();|{}&'\"*?<>[]!^~-#\n\r "
for _,v:= range spchars {
cmdpath = strings.Replace(cmdpath,string(v),"\\"+string(v),-1)
for _, v := range spchars {
cmdpath = strings.Replace(cmdpath, string(v), "\\"+string(v), -1)
}
return cmdpath
}
func FileExists(name string) bool {
if _, err := os.Stat(name ); err == nil {
if _, err := os.Stat(name); err == nil {
return true
}else {
} else {
return false
}
}
@@ -86,22 +85,20 @@ func IsDirectory(path string) bool {
fileInfo, err := os.Stat(path)
if err != nil {
return false
}else {
return fileInfo.IsDir()
} else {
return fileInfo.IsDir()
}
}
func IsAFile(path string) bool {
fileInfo, err := os.Stat(path)
if err != nil {
return false
}else {
return fileInfo.Mode().IsRegular()
} else {
return fileInfo.Mode().IsRegular()
}
}
func MakeExecutable(path string) {
fileInfo, err := os.Stat(path)
if err != nil {
@@ -109,8 +106,8 @@ func MakeExecutable(path string) {
return
}
mode := fileInfo.Mode()
mode |= (mode & 0444) >> 2
os.Chmod(path,mode)
mode |= (mode & 0444) >> 2
os.Chmod(path, mode)
}
func GetExePath() string {
@@ -118,73 +115,72 @@ func GetExePath() string {
if err != nil {
log.Fatal(err)
}
return dir
return dir
}
func ReplaceSuffix(orig_file_str string, new_ext string) string {
orig_ext := filepath.Ext(orig_file_str)
if orig_ext!= "" {
las_pos := strings.LastIndex(orig_file_str,".")
return orig_file_str[0:las_pos]+"."+new_ext
if orig_ext != "" {
las_pos := strings.LastIndex(orig_file_str, ".")
return orig_file_str[0:las_pos] + "." + new_ext
}
return orig_file_str // failed just return back where it came
return orig_file_str // failed just return back where it came
}
func SwapAndShow() {
display.Flip()
}
func ReadLines(path string)(lines [] string,err error){
func ReadLines(path string) (lines []string, err error) {
var (
file *os.File
part [] byte
file *os.File
part []byte
prefix bool
)
if file, err = os.Open(path); err != nil {
return
}
reader := bufio.NewReader(file)
buffer := bytes.NewBuffer(make([]byte,0))
buffer := bytes.NewBuffer(make([]byte, 0))
for {
if part, prefix, err = reader.ReadLine();err != nil {
if part, prefix, err = reader.ReadLine(); err != nil {
break
}
buffer.Write(part)
if !prefix {
lines = append(lines,buffer.String())
lines = append(lines, buffer.String())
buffer.Reset()
}
}
if err == io.EOF {
err = nil
}
return
}
func WriteLines(lines [] string,path string)(err error){
var file *os.File
if file,err = os.Create(path); err != nil{
return
}
defer file.Close()
for _,elem := range lines {
_,err := file.WriteString(strings.TrimSpace(elem)+"\n")
if err != nil {
fmt.Println(err)
break
}
}
return
}
func WriteLines(lines []string, path string) (err error) {
var file *os.File
if file, err = os.Create(path); err != nil {
return
}
defer file.Close()
for _, elem := range lines {
_, err := file.WriteString(strings.TrimSpace(elem) + "\n")
if err != nil {
fmt.Println(err)
break
}
}
return
}
func GetGid(path string) int {
s, err := os.Stat(path)
@@ -200,7 +196,6 @@ func GetGid(path string) int {
return int(sys_interface.(*syscall.Stat_t).Gid)
}
func GetUid(path string) int {
s, err := os.Stat(path)
if err != nil {
@@ -215,76 +210,73 @@ func GetUid(path string) int {
return int(sys_interface.(*syscall.Stat_t).Uid)
}
func CheckBattery() int {
if FileExists(sysgo.Battery) == false {
return -1
}
batinfos,err := ReadLines(sysgo.Battery)
if err == nil {
for _,v := range batinfos {
if strings.HasPrefix(v,"POWER_SUPPLY_CAPACITY") {
parts := strings.Split(v,"=")
if len(parts) > 1 {
cur_cap,err := strconv.Atoi(parts[1])
if err == nil {
return cur_cap
}else {
return 0
}
}
}
}
}else{
fmt.Println(err)
}
return 0
func CheckBattery() int {
if FileExists(sysgo.Battery) == false {
return -1
}
batinfos, err := ReadLines(sysgo.Battery)
if err == nil {
for _, v := range batinfos {
if strings.HasPrefix(v, "POWER_SUPPLY_CAPACITY") {
parts := strings.Split(v, "=")
if len(parts) > 1 {
cur_cap, err := strconv.Atoi(parts[1])
if err == nil {
return cur_cap
} else {
return 0
}
}
}
}
} else {
fmt.Println(err)
}
return 0
}
func System(cmd string) string {
ret := ""
out,err := exec.Command("bash","-c",cmd).Output()
if err != nil {
if _, ok := err.(*exec.ExitError); ok {
//exit code !=0 ,but it can be ignored
}else{
fmt.Println(err)
}
}else {
ret = string(out)
}
ret := ""
out, err := exec.Command("bash", "-c", cmd).Output()
if err != nil {
if _, ok := err.(*exec.ExitError); ok {
//exit code !=0 ,but it can be ignored
} else {
fmt.Println(err)
}
} else {
ret = string(out)
}
return ret
return ret
}
func ArmSystem(cmd string) string {
if strings.Contains(runtime.GOARCH,"arm") == true {
return System(cmd)
}else {
return ""
}
if strings.Contains(runtime.GOARCH, "arm") == true {
return System(cmd)
} else {
return ""
}
}
func SystemTrim(cmd string) string {
ret := ""
out,err := exec.Command("bash","-c",cmd).Output()
if err != nil {
if _, ok := err.(*exec.ExitError); ok {
//exit code !=0 ,but it can be ignored
}else{
fmt.Println(err)
}
}else {
ret = string(out)
}
ret := ""
out, err := exec.Command("bash", "-c", cmd).Output()
if err != nil {
if _, ok := err.(*exec.ExitError); ok {
//exit code !=0 ,but it can be ignored
} else {
fmt.Println(err)
}
} else {
ret = string(out)
}
return strings.Trim(ret,"\r\n")
return strings.Trim(ret, "\r\n")
}
func cmdEnv() []string {