From 7ed54ec1a525492e118a6d2813d2f24b934d6b77 Mon Sep 17 00:00:00 2001 From: CapCreeperGR Date: Wed, 15 May 2024 16:36:44 +0300 Subject: [PATCH] DISPLAY_PROTOCOL Variable and separate monitor resolutions (On X11) Added a DISPLAY_PROTOCOL variable Resolution information has been converted to a Golang function and now lists different monitors separately --- config/fetch_script.sh | 27 +++++++++----------- config/fetch_script_functions.sh | 6 ----- main.go | 8 ++++++ utils.go | 42 ++++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 22 deletions(-) diff --git a/config/fetch_script.sh b/config/fetch_script.sh index 12f8c9e..970ffb2 100644 --- a/config/fetch_script.sh +++ b/config/fetch_script.sh @@ -5,20 +5,15 @@ 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}${USER_SHELL}" -if [ ! -z "$CPU_MODEL" ]; then - echo -e "${C3}CPU: ${C4}${CPU_MODEL} (${CPU_THREADS} threads)" -fi -if [ ! -z "$GPU_MODEL" ]; then - echo -e "${C3}GPU: ${C4}${GPU_MODEL}" -fi -if [ ! -z "$MEM_TOTAL" ] && [ ! -z "$MEM_USED" ]; then - echo -e "${C3}Memory: ${C4}${MEM_USED} MiB / ${MEM_TOTAL} MiB" -fi -if xhost >& /dev/null ; then - 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 +if [ ! -z "$CPU_MODEL" ]; then echo -e "${C3}CPU: ${C4}${CPU_MODEL} (${CPU_THREADS} threads)"; fi +if [ ! -z "$GPU_MODEL" ]; then echo -e "${C3}GPU: ${C4}${GPU_MODEL}"; fi +if [ ! -z "$MEM_TOTAL" ] && [ ! -z "$MEM_USED" ]; then echo -e "${C3}Memory: ${C4}${MEM_USED} MiB / ${MEM_TOTAL} MiB"; fi +if [ ! -z "$DISPLAY_PROTOCOL" ]; then + echo -e "${C3}Display Protocol: ${C4}${DISPLAY_PROTOCOL}" + for i in $(seq ${CONNECTED_MONITORS}); do + monitor="MONITOR$i" + echo -e "${C3}Screen $i: ${C4}${!monitor}" + done fi +if [ ! -z "$DE_WM" ]; then echo -e "${C3}DE/WM: ${C4}${DE_WM}"; fi + diff --git a/config/fetch_script_functions.sh b/config/fetch_script_functions.sh index e6f22e1..c5d4e52 100644 --- a/config/fetch_script_functions.sh +++ b/config/fetch_script_functions.sh @@ -9,12 +9,6 @@ command_exists() { fi } -get_screen_resolution() { - if xhost >& /dev/null && command_exists xdpyinfo; then - xdpyinfo | grep dimensions | tr -s ' ' | cut -d " " -f3 - fi -} - get_packages() { ARRAY=() if command_exists dpkg; then diff --git a/main.go b/main.go index 3066bff..66c8e61 100644 --- a/main.go +++ b/main.go @@ -151,6 +151,14 @@ func readConfig() { } cmd.Env = append(cmd.Env, "DE_WM="+GetDEWM()) cmd.Env = append(cmd.Env, "USER_SHELL="+GetShell()) + cmd.Env = append(cmd.Env, "DISPLAY_PROTOCOL="+GetDisplayProtocol()) + monitors := getMonitorResolution() + if len(monitors) != 0 { + cmd.Env = append(cmd.Env, "CONNECTED_MONITORS="+strconv.Itoa(len(monitors))) + for i, monitor := range monitors { + cmd.Env = append(cmd.Env, "MONITOR"+strconv.Itoa(i+1)+"="+monitor) + } + } if getGPUName() != "" { cmd.Env = append(cmd.Env, "GPU_MODEL="+getGPUName()) } diff --git a/utils.go b/utils.go index 13c7dff..58c0217 100644 --- a/utils.go +++ b/utils.go @@ -2,6 +2,7 @@ package main import ( "bufio" + "fmt" "github.com/jackmordaunt/ghw" "github.com/mitchellh/go-ps" "log" @@ -283,6 +284,47 @@ func GetDEWM() string { return "" } +func GetDisplayProtocol() string { + protocol := os.Getenv("XDG_SESSION_TYPE") + if protocol == "x11" { + return "X11" + } else if protocol == "wayland" { + return "Wayland" + } + return "" +} + +func getMonitorResolution() []string { + var monitors []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)) + } + + if GetDisplayProtocol() == "X11" { + if _, err := os.Stat("/usr/bin/xrandr"); err != nil { + return monitors + } + connections := strings.Split(runCommand("xrandr --query | grep -w \"connected\" | awk '{print $1}'"), "\n") + for i, con := range connections { + Xaxis := runCommand(fmt.Sprintf("xrandr --current | grep -m%d '*' | tail -n1 | uniq | awk '{print $1}' | cut -d 'x' -f1", i+1)) + Yaxis := runCommand(fmt.Sprintf("xrandr --current | grep -m%d '*' | tail -n1 | uniq | awk '{print $1}' | cut -d 'x' -f2", i+1)) + monitors = append(monitors, con+" ("+Xaxis+"x"+Yaxis+")") + } + } + return monitors +} + 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)