DE/WM information has been converted to a Golang function

This commit is contained in:
CapCreeperGR 2024-05-15 09:15:15 +03:00
parent a29a8f1dd9
commit 4c72295972
6 changed files with 60 additions and 29 deletions

View File

@ -3,9 +3,13 @@ package main
import (
"bufio"
"github.com/jackmordaunt/ghw"
"github.com/mitchellh/go-ps"
"log"
"os"
"os/exec"
"path"
"regexp"
"slices"
"strconv"
"strings"
)
@ -176,6 +180,56 @@ func GetMemoryInfo() Memory {
return res
}
func GetDEWM() string {
processes, err := ps.Processes()
if err != nil {
log.Fatal(err)
}
var executables []string
for _, process := range processes {
executables = append(executables, process.Executable())
}
processExists := func(process string) bool {
return slices.Contains(executables, process)
}
runCommand := func(command string) string {
cmd := exec.Command("/bin/bash", "-c", command)
workdir, err := os.Getwd()
if err != nil {
return ""
}
cmd.Dir = workdir
cmd.Env = os.Environ()
out, err := cmd.Output()
if err != nil {
return ""
}
return string(out)
}
if processExists("plasmashell") {
return strings.TrimSpace("KDE Plasma " + runCommand("plasmashell --version | awk '{print $2}'"))
} else if processExists("gnome-session") {
return strings.TrimSpace("Gnome " + runCommand("gnome-shell --version | awk '{print $3}'"))
} else if processExists("xfce4-session") {
return strings.TrimSpace("XFCE " + runCommand("xfce4-session --version | grep xfce4-session | awk '{print $2}'"))
} else if processExists("cinnamon") {
return strings.TrimSpace("Cinnamon " + runCommand("cinnamon --version | awk '{print $3}'"))
} else if processExists("mate-panel") {
return strings.TrimSpace("MATE " + runCommand("mate-about --version | awk '{print $4}'"))
} else if processExists("lxsession") {
return "LXDE"
} else if processExists("sway") {
return strings.TrimSpace("Sway " + runCommand("sway --version | awk '{print $3}'"))
} else if processExists("bspwm") {
return strings.TrimSpace("Bspwm " + runCommand("bspwm -v"))
} else if processExists("icewm-session") {
return strings.TrimSpace("IceWM " + runCommand("icewm --version | awk '{print $2}'"))
}
return ""
}
func stripAnsii(str string) string {
const ansi = "[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))"
var re = regexp.MustCompile(ansi)

View File

@ -11,11 +11,10 @@ if [ ! -z "$GPU_MODEL" ]; then
fi
echo -e "${C3}Memory: ${C4}${MEM_USED} MiB / ${MEM_TOTAL} MiB"
if xhost >& /dev/null ; then
if get_de_wm &> /dev/null; then
echo -e "${C3}DE/WM: ${C4}$(get_de_wm)"
if [ ! -z "$DE_WM" ]; then
echo -e "${C3}DE/WM: ${C4}${DE_WM}"
fi
if command_exists xdpyinfo ; then
echo -e "${C3}Screen Resolution: ${C4}$(get_screen_resolution)"
fi
fi

View File

@ -34,32 +34,6 @@ get_shell() {
esac
}
get_de_wm() {
if ps -e | grep "plasmashell" &> /dev/null ; then
echo "KDE Plasma $(plasmashell --version | awk '{print $2}')"
elif ps -e | grep "gnome-session" &> /dev/null ; then
echo "Gnome $(gnome-shell --version | awk '{print $3}')"
elif ps -e | grep "xfce4-session" &> /dev/null ; then
echo "XFCE $(xfce4-session --version | grep xfce4-session | awk '{print $2}')"
elif ps -e | grep "cinnamon" &> /dev/null ; then
echo "Cinnamon $(cinnamon --version | awk '{print $2}')"
elif ps -e | grep "mate-panel" &> /dev/null ; then
echo "Mate $(mate-about --version | awk '{print $4}')"
elif ps -e | grep "lxsession" &> /dev/null ; then
echo "LXDE"
elif ps -e | grep "sway" &> /dev/null ; then
echo "Sway $(sway --version | awk '{print $2}')"
elif ps -e | grep "bspwm" &> /dev/null ; then
echo "Bspwm $(bspwm -v)"
elif ps -e | grep "icewm-session" &> /dev/null ; then
echo "IceWM $(icewm --version | awk '{print $2}' | sed 's/,//g')"
elif [ ! -z $DESKTOP_SESSION ]; then
echo "$DESKTOP_SESSION"
else
return 1
fi
}
get_screen_resolution() {
if xhost >& /dev/null && command_exists xdpyinfo; then
xdpyinfo | grep dimensions | tr -s ' ' | cut -d " " -f3

1
go.mod
View File

@ -12,6 +12,7 @@ require (
github.com/jaypipes/ghw v0.12.0 // indirect
github.com/jaypipes/pcidb v1.0.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-ps v1.0.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
golang.org/x/sys v0.3.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect

2
go.sum
View File

@ -19,6 +19,8 @@ github.com/jaypipes/pcidb v1.0.0/go.mod h1:TnYUvqhPBzCKnH34KrIX22kAeEbDCSRJ9cqLR
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-ps v1.0.0 h1:i6ampVEEF4wQFF+bkYfwYgY+F/uYJDktmvLPf7qIgjc=
github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

View File

@ -147,6 +147,7 @@ func readConfig() {
cmd.Env = append(cmd.Env, "MEM_TOTAL="+strconv.Itoa(memory.MemTotal))
cmd.Env = append(cmd.Env, "MEM_USED="+strconv.Itoa(memory.MemTotal-memory.MemAvailable))
cmd.Env = append(cmd.Env, "MEM_FREE="+strconv.Itoa(memory.MemAvailable))
cmd.Env = append(cmd.Env, "DE_WM="+GetDEWM())
if getGPUName() != "" {
cmd.Env = append(cmd.Env, "GPU_MODEL="+getGPUName())