Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ak-Army committed Feb 21, 2017
1 parent 256a0ea commit 7e5a692
Show file tree
Hide file tree
Showing 14 changed files with 1,013 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ _testmain.go
*.exe
*.test
*.prof
*.log
.idea
75 changes: 75 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"defaults": {
"color": "#ffffff"
},
"blocks": [{
"module": "ExternalCmd",
"label":"",
"config": {
"command": "apt-get --just-print upgrade |grep Inst | wc -l"
},
"interval": 60,
"info": {
"border_bottom": 2,
"border": "#ff00ff"
}
},{
"module": "VolumeInfo",
"label":"V:",
"interval": 1,
"info": {
"border_bottom": 2,
"border": "#ff00ff"
}
},{
"module": "CpuInfo",
"label": "",
"interval": 2,
"info": {
"border_bottom": 2,
"border": "#ffffff"
}
},{
"module": "MemInfo",
"label": "",
"interval": 5,
"info": {
"border_bottom": 2,
"border": "#00ff00"
}
},{
"module": "DiskUsage",
"label": "",
"interval": 10,
"info": {
"border_bottom": 2,
"border": "#ffff00"
}
},{
"module": "ExternalCmd",
"label":"",
"config": {
"command": "uptime |sed 's/,//g' |awk '{ print $3 }'"
},
"interval": 60,
"info": {
"border_bottom": 2,
"border": "#909090"
}
},{
"module": "DateTime",
"label": "",
"interval": 5,
"info": {
"border_bottom": 2,
"border": "#ffffff"
}
},{
"module": "ExternalCmd",
"label": "",
"config": {
"on_click": "shutdown -h -t now"
},
"interval": 0
}]
}
149 changes: 149 additions & 0 deletions gobar/bar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package gobar

import (
"encoding/json"
"fmt"
"strings"
"log"
"time"
"bufio"
"os"
)

// Header i3 header
type Header struct {
Version int `json:"version"`
ClickEvents bool `json:"click_events"`
// StopSignal int `json:"stop_signal"`
// ContinueSignal int `json:"cont_signal"`
}

type Bar struct {
modules []Block
logger *log.Logger
updateChannel chan UpdateChannelMsg
stop chan bool
}

type ClickMessage struct {
Name string `json:"name,omitempty"`
Instance string `json:"instance,omitempty"`
Button int `json:"button"`
X int `json:"x"`
Y int `json:"y"`
}

func (cm *ClickMessage) isMatch(block Block) bool {
return block.Info.Name == cm.Name && block.Info.Instance == cm.Instance
}

func (bar *Bar) Start() {
header := Header{
Version: 1,
ClickEvents: true,
// StopSignal: 20, // SIGHUP
// ContinueSignal: 19, // SIGCONT
}
headerJSON, _ := json.Marshal(header)
fmt.Println(string(headerJSON))
fmt.Println("[[]")
bar.stop = make(chan bool, 3)
bar.ReStart()
}

func (bar *Bar) ReStart() {
bar.stop = make(chan bool, 3)
go bar.update()
go bar.printItems()
go bar.handleClick()
}

func (bar *Bar) Stop() {
bar.stop <- true
bar.stop <- true
bar.stop <- true
}

func (bar *Bar) update() {
for {
select {
case <-bar.stop:
bar.logger.Println("update")
return
case m := <-bar.updateChannel:
bar.modules[m.ID].Info = m.Info
}
}
}

func (bar *Bar) handleClick() {
for {
select {
case <-bar.stop:
bar.logger.Println("handleClick")
return
default:
bio := bufio.NewReader(os.Stdin)
line, _, err := bio.ReadLine()
if err != nil {
continue
}
if len(line) == 0 {
continue
}

var clickMessage ClickMessage

if line[0] == ',' {
line = line[1:]
}

err = json.Unmarshal(line, &clickMessage)
if err == nil {
bar.logger.Printf("Click: line: %s, cm:%+v", string(line), clickMessage)
for _, module := range bar.modules {
if clickMessage.isMatch(module) {
bar.logger.Println("Click: handled")
err := module.HandleClick(clickMessage)
if err != nil {
bar.logger.Println("Click handle error: %s", err.Error())
}
}
}
}
time.Sleep(1)
}
}
}

func (bar *Bar) printItems() {
for {
select {
case <-bar.stop:
bar.logger.Println("printItems")
return
default:
var infoArray []string
var minInterval int64
for _, item := range bar.modules {
item.Info.FullText = item.Label + " " + item.Info.FullText
item.Info.ShortText = item.Label + " " + item.Info.ShortText

info, err := json.Marshal(item.Info)
if err != nil {
bar.logger.Printf("ERROR: %q", err)
} else {
infoArray = append(infoArray, string(info))
}
if minInterval == 0 || (item.Interval > 0 && item.Interval < minInterval) {
minInterval = item.Interval
}
}
fmt.Println(",[", strings.Join(infoArray, ",\n"), "]")
if minInterval == 0 {
break;
}
time.Sleep(time.Duration(minInterval) * time.Second)
}
}
}
Loading

0 comments on commit 7e5a692

Please sign in to comment.