diff --git a/Utils.go b/Utils.go index e2a1ca5..947b7a2 100644 --- a/Utils.go +++ b/Utils.go @@ -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) diff --git a/config/fetch_script.sh b/config/fetch_script.sh index f88f231..5d8807d 100644 --- a/config/fetch_script.sh +++ b/config/fetch_script.sh @@ -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 - diff --git a/config/fetch_script_functions.sh b/config/fetch_script_functions.sh index 7fa13e8..76e9876 100644 --- a/config/fetch_script_functions.sh +++ b/config/fetch_script_functions.sh @@ -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 diff --git a/go.mod b/go.mod index 84f8e46..eb3286f 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 03d0abd..66018b0 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index 1a5a058..181cae5 100644 --- a/main.go +++ b/main.go @@ -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())