vivien / i3blocks-contrib

Official repository for community contributed blocklets

Home Page:https://github.com/vivien/i3blocks

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Enhancement to sway-focusedwindow

stratosgear opened this issue · comments

Sorry for not following the issue template, this is more for a discussion about a potential enhancement to a block rather than an issue.

I would like to ask the advice of @cnt0, the author of sway-focusedwindow on the following:

I would like to have an indicator of the layout of the currently focused window (if it is horizontal or vertical).

It seems that the code for the sway-focusedwindow block is just pulling the window name.

I found the following jq queries that allows me to extract the required layout.

.. | select(.focused?) | .id

which gives me the ID of the focused window, and:

.. | select(.focus? | index(XXX)) | .layout

which gives me the required layout I am looking for (XXX is the window id from the first query).

But I do not know how to incorporate that into the actual code of the block code.

Mainly because I will need to do two jq queries since I cannot find the required info with one. I had issues using jq to find the parent of the focused node, so I could extract the layout info.

Could you help a bit?

Thanks!

It is not important that you change the original block (although a discreet (V) or (H) at the end of the name might not look that bad).

It could also be configurable so the (V) or (H) indicator appears or not...

I just could not make the double jq query work, otherwise I would have done it myself.

Thanks again.

@stratosgear in theory, you can combine 2 queries like this:

'.. | select(.focus?) as $root | .. | select(.focused?) | "\(.name) \($root.layout)"'

BTW there's an interactive jq query tester, https://github.com/Wazzaps/jqed

swaymsg -t get_tree | jqed

But, to be honest, jq is not a right tool for complex queries. I'd rather implement a simple tree traversal in python here

Sure, there is also jqplay as an interactive jq playground. It would be nice if your above snippet actually worked but it fails:

https://jqplay.org/s/tnH4k6BS6i

Nonetheless, the issue I am having problems with is the bash process method that you are using as a callback in the sway subscription.

For now I have patched it as:

process() {
  while read -r LINE; do
    SWAYTREE=`swaymsg -t get_tree`
    WN=` echo $SWAYTREE | jq --unbuffered -r '.. | select(.focused?) | .name'`
    ID=`echo $SWAYTREE | jq --unbuffered -r '.. | select(.focused?) | .id'`
    SWAYLAYOUT=`echo $SWAYTREE | jq --unbuffered -r  ".. | select(.focus? | index($ID)) | .layout"`
    format "$WN ($SWAYLAYOUT)"
  done
}

Which is ugly, but it works!