From 4a2833afff91d3091db820b080e7853b4de31a5b Mon Sep 17 00:00:00 2001 From: CapCreeperGR Date: Wed, 15 May 2024 12:02:43 +0300 Subject: [PATCH] Shell information has been converted to a Golang function --- config/fetch_script.sh | 4 +- config/fetch_script_functions.sh | 25 ------------ main.go | 2 +- utils.go | 65 +++++++++++++++++++++++++++----- 4 files changed, 59 insertions(+), 37 deletions(-) diff --git a/config/fetch_script.sh b/config/fetch_script.sh index 5d8807d..fa107d7 100644 --- a/config/fetch_script.sh +++ b/config/fetch_script.sh @@ -4,10 +4,10 @@ echo -e "${C3}Distribution: ${C4}${DISTRO_LONG_NAME} ($(uname -m))" echo -e "${C3}Hostname: ${C4}$(cat /etc/hostname)" echo -e "${C3}Kernel: ${C4}$(uname -s) $(uname -r)" echo -e "${C3}Packages: ${C4}$(get_packages)" -echo -e "${C3}Shell: ${C4}$(get_shell)" +echo -e "${C3}Shell: ${C4}${USER_SHELL}" echo -e "${C3}CPU: ${C4}${CPU_MODEL} (${CPU_THREADS} threads)" if [ ! -z "$GPU_MODEL" ]; then - echo -e "${C3}GPU: ${C4}${GPU_MODEL})" + echo -e "${C3}GPU: ${C4}${GPU_MODEL}" fi echo -e "${C3}Memory: ${C4}${MEM_USED} MiB / ${MEM_TOTAL} MiB" if xhost >& /dev/null ; then diff --git a/config/fetch_script_functions.sh b/config/fetch_script_functions.sh index 76e9876..e6f22e1 100644 --- a/config/fetch_script_functions.sh +++ b/config/fetch_script_functions.sh @@ -9,31 +9,6 @@ command_exists() { fi } -get_shell() { - case ${SHELL##*/} in - "") - echo "Unknown" - ;; - sh|ash|dash|es) - echo "${SHELL##*/} $(${SHELL##*/} --version)" - ;; - bash) - echo "${SHELL##*/} $(${SHELL##*/} -c "echo "'$BASH_VERSION')" - ;; - *) - SHELL_NAME=${SHELL##*/} - SHELL_VERSION="$($SHELL --version)" - SHELL_VERSION=${SHELL_VERSION//","} - SHELL_VERSION=${SHELL_VERSION//" "} - SHELL_VERSION=${SHELL_VERSION//"version"} - SHELL_VERSION=${SHELL_VERSION//"$SHELL_NAME"} - echo "$SHELL_NAME $SHELL_VERSION" - unset SHELL_NAME - unset SHELL_VERSION - ;; - esac -} - get_screen_resolution() { if xhost >& /dev/null && command_exists xdpyinfo; then xdpyinfo | grep dimensions | tr -s ' ' | cut -d " " -f3 diff --git a/main.go b/main.go index 7b1e7ab..3b1d03f 100644 --- a/main.go +++ b/main.go @@ -148,7 +148,7 @@ func readConfig() { 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()) - + cmd.Env = append(cmd.Env, "USER_SHELL="+GetShell()) if getGPUName() != "" { cmd.Env = append(cmd.Env, "GPU_MODEL="+getGPUName()) } diff --git a/utils.go b/utils.go index 947b7a2..b64b3ff 100644 --- a/utils.go +++ b/utils.go @@ -8,6 +8,7 @@ import ( "os" "os/exec" "path" + "path/filepath" "regexp" "slices" "strconv" @@ -180,6 +181,52 @@ func GetMemoryInfo() Memory { return res } +func GetShell() string { + 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 strings.TrimSpace(string(out)) + } + file, err := os.ReadFile("/etc/passwd") + if err != nil { + return "" + } + str := string(file) + shell := "" + + for _, line := range strings.Split(str, "\n") { + if strings.TrimSpace(line) == "" { + continue + } + userInfo := strings.Split(line, ":") + if userInfo[2] == strconv.Itoa(os.Getuid()) { + shell = userInfo[6] + } + } + shellName := filepath.Base(shell) + switch shellName { + case "dash": + return "Dash" + case "bash": + return "Bash " + runCommand("echo $BASH_VERSION") + case "zsh": + return "Zsh " + runCommand("$SHELL --version | awk '{print $2}'") + case "fish": + return "Fish " + runCommand("$SHELL --version | awk '{print $3}'") + default: + return "Unknown" + } +} + func GetDEWM() string { processes, err := ps.Processes() if err != nil { @@ -205,27 +252,27 @@ func GetDEWM() string { if err != nil { return "" } - return string(out) + return strings.TrimSpace(string(out)) } if processExists("plasmashell") { - return strings.TrimSpace("KDE Plasma " + runCommand("plasmashell --version | awk '{print $2}'")) + return "KDE Plasma " + runCommand("plasmashell --version | awk '{print $2}'") } else if processExists("gnome-session") { - return strings.TrimSpace("Gnome " + runCommand("gnome-shell --version | awk '{print $3}'")) + return "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}'")) + return "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}'")) + return "Cinnamon " + runCommand("cinnamon --version | awk '{print $3}'") } else if processExists("mate-panel") { - return strings.TrimSpace("MATE " + runCommand("mate-about --version | awk '{print $4}'")) + return "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}'")) + return "Sway " + runCommand("sway --version | awk '{print $3}'") } else if processExists("bspwm") { - return strings.TrimSpace("Bspwm " + runCommand("bspwm -v")) + return "Bspwm " + runCommand("bspwm -v") } else if processExists("icewm-session") { - return strings.TrimSpace("IceWM " + runCommand("icewm --version | awk '{print $2}'")) + return "IceWM " + runCommand("icewm --version | awk '{print $2}'") } return "" }