add block ps
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Simon Vieille 2022-08-30 13:33:58 +02:00
parent 528620bb9a
commit c9665b33b3
Signed by: deblan
GPG key ID: 579388D585F70417
4 changed files with 54 additions and 4 deletions

View file

@ -6,3 +6,8 @@ all:
clean:
rm build/* 2>/dev/null || true
.ONESHELL:
run-code-quality-analysis:
export SONAR_TOKEN="$$SONAR_TOKEN_DEBLAN_I3_BLOCKS_GO"
sonar-scanner -Dsonar.projectKey=deblan-i3-blocks-go -Dsonar.sources=. -Dsonar.host.url="$$SONAR_SERVER"

View file

@ -4,7 +4,7 @@ Blocks compatible with [i3blocks](https://github.com/vivien/i3blocks).
## Requirements
* go for compilation
* `go` for compilation
* `sudo` for `wireguard` block
* `tmux` for `app` and `date` blocks
* `df` for `du` block
@ -63,6 +63,12 @@ Show the telemetry using Prusa Printer API.
prusa_telemetry <block_name> http://<prusa_printer_ip>/api/telemetry
```
Show a message when the given process is running (use `preg -f`).
```
ps <process> <message>
```
Show indicator of RSS.
```

37
blocks/ps/main.go Normal file
View file

@ -0,0 +1,37 @@
package main
import (
"fmt"
r "gitnet.fr/deblan/i3-blocks-go/rendering"
"os"
"os/exec"
"strings"
)
func ProcessExists(process string) bool {
output, _ := exec.Command("pgrep", "-f", process).Output()
lines := strings.TrimSpace(string(output))
count := strings.Count(lines, "\n")
return count > 1
}
func main() {
argProcess := os.Args[1]
argName := os.Args[2]
if !ProcessExists(argProcess) {
return
}
value := r.TextWithPadding(argName, r.FB{
Foreground: r.Color("green"),
Background: r.Color("black1"),
})
options := r.NewBlockOptions()
options.FullText = value
block := r.Block("ps", options)
fmt.Println(block)
}

View file

@ -15,6 +15,8 @@ type WindowProperty struct {
Instance int `json:"instance"`
}
const binI3Msg = "i3-msg"
type Tree struct {
Num int `json:"num"`
Output string `json:"output"`
@ -33,11 +35,11 @@ type Workspace struct {
}
func Focus(window int) {
exec.Command("i3-msg", fmt.Sprintf("[id=%d] focus", window)).Run()
exec.Command(binI3Msg, fmt.Sprintf("[id=%d] focus", window)).Run()
}
func GetTree() Tree {
output, _ := exec.Command("i3-msg", "-t", "get_tree").Output()
output, _ := exec.Command(binI3Msg, "-t", "get_tree").Output()
tree := Tree{}
json.Unmarshal(output, &tree)
@ -45,7 +47,7 @@ func GetTree() Tree {
}
func GetVisibleWorkspaces() []Workspace {
output, _ := exec.Command("i3-msg", "-t", "get_workspaces").Output()
output, _ := exec.Command(binI3Msg, "-t", "get_workspaces").Output()
workspaces := []Workspace{}
datas := []Workspace{}
json.Unmarshal(output, &datas)