Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] deflisten script not working #1056

Closed
3 tasks done
rafaeljacov opened this issue Mar 28, 2024 · 3 comments
Closed
3 tasks done

[BUG] deflisten script not working #1056

rafaeljacov opened this issue Mar 28, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@rafaeljacov
Copy link

rafaeljacov commented Mar 28, 2024

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;
}
@rafaeljacov rafaeljacov added the bug Something isn't working label Mar 28, 2024
@Rayzeq
Copy link
Contributor

Rayzeq commented Mar 29, 2024

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.

@rafaeljacov
Copy link
Author

rafaeljacov commented Mar 29, 2024

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'

@elkowar
Copy link
Owner

elkowar commented Mar 30, 2024

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.

@elkowar elkowar closed this as not planned Won't fix, can't repro, duplicate, stale Mar 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants