From c9665b33b3339b2662492bd6f49667acbcad87c5 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Tue, 30 Aug 2022 13:33:58 +0200 Subject: [PATCH] add block ps --- Makefile | 5 +++++ README.md | 8 +++++++- blocks/ps/main.go | 37 +++++++++++++++++++++++++++++++++++ blocks/workspace_apps/main.go | 8 +++++--- 4 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 blocks/ps/main.go diff --git a/Makefile b/Makefile index 4e2a319..395310b 100644 --- a/Makefile +++ b/Makefile @@ -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" diff --git a/README.md b/README.md index f474860..8b3861a 100644 --- a/README.md +++ b/README.md @@ -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 http:///api/telemetry ``` +Show a message when the given process is running (use `preg -f`). + +``` +ps +``` + Show indicator of RSS. ``` diff --git a/blocks/ps/main.go b/blocks/ps/main.go new file mode 100644 index 0000000..0797b82 --- /dev/null +++ b/blocks/ps/main.go @@ -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) +} diff --git a/blocks/workspace_apps/main.go b/blocks/workspace_apps/main.go index 3e2c8a1..91bcf79 100644 --- a/blocks/workspace_apps/main.go +++ b/blocks/workspace_apps/main.go @@ -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)