util_funcs.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. package UI
  2. import (
  3. "os"
  4. "log"
  5. "path/filepath"
  6. "strings"
  7. "fmt"
  8. "bufio"
  9. "bytes"
  10. "io"
  11. "strconv"
  12. "syscall"
  13. "os/exec"
  14. "runtime"
  15. "github.com/cuu/gogame/display"
  16. "github.com/clockworkpi/LauncherGoDev/sysgo"
  17. )
  18. func ShowErr(e error) {
  19. if e != nil {
  20. fmt.Println(e)
  21. }
  22. }
  23. func Assert(e error) {
  24. if e != nil {
  25. log.Fatal("Assert: " , e)
  26. }
  27. }
  28. func CheckAndPanic(e error) {
  29. if e != nil {
  30. panic(e)
  31. }
  32. }
  33. func Abs(n int) int {
  34. y := n >> 63
  35. return (n ^ y) - y
  36. }
  37. func SkinMap(orig_file_or_dir string) string {
  38. DefaultSkin := "default"
  39. ret := ""
  40. skin_dir_prefix:= "skin/"
  41. if strings.HasPrefix(orig_file_or_dir, "..") {
  42. ret = strings.Replace(orig_file_or_dir,"..",skin_dir_prefix + sysgo.SKIN,-1)
  43. if FileExists(ret) == false {
  44. ret = strings.Replace(orig_file_or_dir,"..", skin_dir_prefix + DefaultSkin,-1)
  45. }
  46. }else {
  47. ret = skin_dir_prefix+sysgo.SKIN+"/"+orig_file_or_dir
  48. if FileExists(ret) == false {
  49. ret = skin_dir_prefix+DefaultSkin+"/"+orig_file_or_dir
  50. }
  51. }
  52. if FileExists(ret) {
  53. return ret
  54. }else { // if not existed both in default or custom skin ,return where it is
  55. return orig_file_or_dir
  56. }
  57. }
  58. func CmdClean(cmdpath string) string {
  59. spchars := "\\`$();|{}&'\"*?<>[]!^~-#\n\r "
  60. for _,v:= range spchars {
  61. cmdpath = strings.Replace(cmdpath,string(v),"\\"+string(v),-1)
  62. }
  63. return cmdpath
  64. }
  65. func FileExists(name string) bool {
  66. if _, err := os.Stat(name ); err == nil {
  67. return true
  68. }else {
  69. return false
  70. }
  71. }
  72. func IsDirectory(path string) bool {
  73. fileInfo, err := os.Stat(path)
  74. if err != nil {
  75. return false
  76. }else {
  77. return fileInfo.IsDir()
  78. }
  79. }
  80. func IsAFile(path string) bool {
  81. fileInfo, err := os.Stat(path)
  82. if err != nil {
  83. return false
  84. }else {
  85. return fileInfo.Mode().IsRegular()
  86. }
  87. }
  88. func MakeExecutable(path string) {
  89. fileInfo, err := os.Stat(path)
  90. if err != nil {
  91. log.Fatalf("os.Stat %s failed", path)
  92. return
  93. }
  94. mode := fileInfo.Mode()
  95. mode |= (mode & 0444) >> 2
  96. os.Chmod(path,mode)
  97. }
  98. func GetExePath() string {
  99. dir, err := os.Getwd()
  100. if err != nil {
  101. log.Fatal(err)
  102. }
  103. return dir
  104. }
  105. func ReplaceSuffix(orig_file_str string, new_ext string) string {
  106. orig_ext := filepath.Ext(orig_file_str)
  107. if orig_ext!= "" {
  108. las_pos := strings.LastIndex(orig_file_str,".")
  109. return orig_file_str[0:las_pos]+"."+new_ext
  110. }
  111. return orig_file_str // failed just return back where it came
  112. }
  113. func SwapAndShow() {
  114. display.Flip()
  115. }
  116. func ReadLines(path string)(lines [] string,err error){
  117. var (
  118. file *os.File
  119. part [] byte
  120. prefix bool
  121. )
  122. if file, err = os.Open(path); err != nil {
  123. return
  124. }
  125. reader := bufio.NewReader(file)
  126. buffer := bytes.NewBuffer(make([]byte,0))
  127. for {
  128. if part, prefix, err = reader.ReadLine();err != nil {
  129. break
  130. }
  131. buffer.Write(part)
  132. if !prefix {
  133. lines = append(lines,buffer.String())
  134. buffer.Reset()
  135. }
  136. }
  137. if err == io.EOF {
  138. err = nil
  139. }
  140. return
  141. }
  142. func WriteLines(lines [] string,path string)(err error){
  143. var file *os.File
  144. if file,err = os.Create(path); err != nil{
  145. return
  146. }
  147. defer file.Close()
  148. for _,elem := range lines {
  149. _,err := file.WriteString(strings.TrimSpace(elem)+"\n")
  150. if err != nil {
  151. fmt.Println(err)
  152. break
  153. }
  154. }
  155. return
  156. }
  157. func GetGid(path string) int {
  158. s, err := os.Stat(path)
  159. if err != nil {
  160. return -1
  161. }
  162. sys_interface := s.(os.FileInfo).Sys()
  163. if sys_interface == nil {
  164. return -1
  165. }
  166. return int(sys_interface.(*syscall.Stat_t).Gid)
  167. }
  168. func GetUid(path string) int {
  169. s, err := os.Stat(path)
  170. if err != nil {
  171. return -1
  172. }
  173. sys_interface := s.(os.FileInfo).Sys()
  174. if sys_interface == nil {
  175. return -1
  176. }
  177. return int(sys_interface.(*syscall.Stat_t).Uid)
  178. }
  179. func CheckBattery() int {
  180. if FileExists(sysgo.Battery) == false {
  181. return -1
  182. }
  183. batinfos,err := ReadLines(sysgo.Battery)
  184. if err == nil {
  185. for _,v := range batinfos {
  186. if strings.HasPrefix(v,"POWER_SUPPLY_CAPACITY") {
  187. parts := strings.Split(v,"=")
  188. if len(parts) > 1 {
  189. cur_cap,err := strconv.Atoi(parts[1])
  190. if err == nil {
  191. return cur_cap
  192. }else {
  193. return 0
  194. }
  195. }
  196. }
  197. }
  198. }else{
  199. fmt.Println(err)
  200. }
  201. return 0
  202. }
  203. func System(cmd string) string {
  204. ret := ""
  205. out,err := exec.Command("bash","-c",cmd).Output()
  206. if err != nil {
  207. if _, ok := err.(*exec.ExitError); ok {
  208. //exit code !=0 ,but it can be ignored
  209. }else{
  210. fmt.Println(err)
  211. }
  212. }else {
  213. ret = string(out)
  214. }
  215. return ret
  216. }
  217. func ArmSystem(cmd string) string {
  218. if strings.Contains(runtime.GOARCH,"arm") == true {
  219. return System(cmd)
  220. }else {
  221. return ""
  222. }
  223. }
  224. func SystemTrim(cmd string) string {
  225. ret := ""
  226. out,err := exec.Command("bash","-c",cmd).Output()
  227. if err != nil {
  228. if _, ok := err.(*exec.ExitError); ok {
  229. //exit code !=0 ,but it can be ignored
  230. }else{
  231. fmt.Println(err)
  232. }
  233. }else {
  234. ret = string(out)
  235. }
  236. return strings.Trim(ret,"\r\n")
  237. }