elkowar / eww

ElKowars wacky widgets

Home Page:https://elkowar.github.io/eww

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG] deflisten script not working

rafaeljacov opened this issue · comments

Checklist before submitting an issue

  • I have searched through the existing closed and open issues for eww and made sure this is not a duplicate
  • I have specifically verified that this bug is not a common user error
  • I am providing as much relevant information as I am able to in this bug report (Minimal config to reproduce the issue for example, if applicable)

Description of the bug

I tested my python script to detect workspace change and it works but doesn't work on eww

eww state:

get-time: 18:05
active-ws: hyprctl activeworkspace -j | jq '.id'
music: Baby Don’t Hurt Me - Sped Up

eww.yuck

(deflisten active-ws 
    :initial `hyprctl activeworkspace -j | jq '.id'`
    `scripts/hypr.py -a`)

even after trying another way as seen below it still doesn't work

(deflisten active-ws 
    :initial '1'
    `scripts/hypr.py -a`)

hypr.py

#!/usr/bin/env python3
# Script for getting active workspace/window

import os
import sys
import socket
import subprocess

WORKSPACES = 6
HYPR_INSTANCE = os.getenv('HYPRLAND_INSTANCE_SIGNATURE')
SOCKET_PATH = f'/tmp/hypr/{HYPR_INSTANCE}/.socket2.sock'

def get_window_title():
    pass

def get_active_workspace(line):
    if line.find('workspace>>') == 0:
        print(line[-1])

with socket.socket(socket.AF_UNIX) as client_socket:
        # Connect to the Unix domain socket
        client_socket.connect(SOCKET_PATH)

        # Receive data from the socket
        while True:
            data = client_socket.recv(1024)  # Adjust the buffer size as needed
            decoded_data = data.decode().strip().split('\n')  # Convert bytes to string and remove leading/trailing whitespace
            for line in decoded_data:
                if sys.argv[1] == '-a':
                    get_active_workspace(line)

Reproducing the issue

just change workspaces

Expected behaviour

workspace widget should reflect active workspace on switch

Additional context

Full eww.yuck config:

;; Variables
(deflisten active-ws 
    :initial `hyprctl activeworkspace -j | jq '.id'`
    `scripts/hypr.py -a`)

(defpoll get-time :interval "2s"
    "date +%H:%M")

(defwindow bar
    :monitor 0
    :exclusive true
    :geometry (geometry
        :width "100%"
        :height "32px"
        :anchor "center top")
    (box :class "bar" 
        :valign "fill"
        :halign "fill"
        (workspace)
        (clock)
        ))

(defwidget workspace []
    (box :class "workspace" 
        :halign "start"
        :spacing 8
        :space-evenly true
        (ws-item :num '1')
        (ws-item :num '2')
        (ws-item :num '3')
        (ws-item :num '4')
        (ws-item :num '5')
        (ws-item :num '6')))

(defwidget ws-item [num]
    (eventbox :cursor "pointer"
        ;; :onclick "~/.config/hypr/scripts/workspace.py -s ${num}"
        (box :class "ws-item ${num == active-ws ? "ws-active": ""}"
            :valign "center")))

(defwidget clock []
    (box :class "clock"
        :halign "end"
        get-time))

Full scss:

* {
    all: unset
}

.bar {
    padding: 6px 12px;
    background-color: #0A1321;
}

.workspace {
    border-radius: 16px;
}

.ws-item {
    opacity: 0.4;
    border: 2px solid white;
    border-radius: 50%;
    transition: 0.15s ease;

    min-height: 10px;
    min-width: 10px;
}

.ws-active {
    opacity: 1;
    background-color: white;
}

.clock {
    font-size: 1.2rem;
    font-weight: bold;
}

I'm not sure it will fix your issue, but you should try printing with print(line[-1], flush=True).
Doing this fixed some of my script, so it might works for you.

I'm not sure it will fix your issue, but you should try printing with print(line[-1], flush=True). Doing this fixed some of my script, so it might works for you.

thanks it worked! but one more thing:

in the line below, is it invalid to set the initial value of deflisten as the output of an command?
I ran eww state and it shows the literal command that I ran instead of the output of the value.

(deflisten active-ws 
    :initial `hyprctl activeworkspace -j | jq '.id'`
    `scripts/hypr.py -a`)

;; eww state
;; active-ws: hyprctl activeworkspace -j | jq '.id'

That is indeed invalid! The idea of the initial value is to have some value that shows before any scripts push their first output, thus setting the initial value to some command would mean that eww would still have to wait for the command to finish before being able to proceed with rendering anything. To ensure that your variable is set to a correct value as fast as possible, I recommend just starting out your hypr.py script by running hyprctl activeworkspaces -j | jq .'id', such that eww gets a meaningful value as fast as possible.

And ye, that flush=True fix ensures that your script actually outputs the data, rather than just buffering it and then printing it in some batch at some point -- eww can't really do anything to avoid this being required :/ Thus, I'll close this issue. Feel free to re-open or create a new one if there are other issues.