mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
The /wtf directory is now in source control
This commit is contained in:
parent
0b8a063487
commit
176a821079
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,2 @@
|
||||
DS_Store
|
||||
gcal/client_secret.json
|
||||
wtf
|
||||
|
16
wtf/base_widget.go
Normal file
16
wtf/base_widget.go
Normal file
@ -0,0 +1,16 @@
|
||||
package wtf
|
||||
|
||||
import (
|
||||
//"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type BaseWidget struct {
|
||||
Name string
|
||||
RefreshedAt time.Time
|
||||
RefreshInt int
|
||||
}
|
||||
|
||||
func (widget *BaseWidget) RefreshInterval() int {
|
||||
return widget.RefreshInt
|
||||
}
|
25
wtf/refresher.go
Normal file
25
wtf/refresher.go
Normal file
@ -0,0 +1,25 @@
|
||||
package wtf
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Refresher interface {
|
||||
Refresh()
|
||||
RefreshInterval() int
|
||||
}
|
||||
|
||||
func Refresh(widget Refresher) {
|
||||
tick := time.NewTicker(time.Duration(widget.RefreshInterval()) * time.Second)
|
||||
quit := make(chan struct{})
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-tick.C:
|
||||
widget.Refresh()
|
||||
case <-quit:
|
||||
tick.Stop()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
29
wtf/utils.go
Normal file
29
wtf/utils.go
Normal file
@ -0,0 +1,29 @@
|
||||
package wtf
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
func CenterText(str string, width int) string {
|
||||
return fmt.Sprintf("%[1]*s", -width, fmt.Sprintf("%[1]*s", (width+len(str))/2, str))
|
||||
}
|
||||
|
||||
func ExecuteCommand(cmd *exec.Cmd) string {
|
||||
stdout, err := cmd.StdoutPipe()
|
||||
if err != nil {
|
||||
return fmt.Sprintf("A: %v\n", err)
|
||||
}
|
||||
|
||||
if err := cmd.Start(); err != nil {
|
||||
return fmt.Sprintf("B: %v\n", err)
|
||||
}
|
||||
|
||||
var str string
|
||||
if b, err := ioutil.ReadAll(stdout); err == nil {
|
||||
str += string(b)
|
||||
}
|
||||
|
||||
return str
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user