You are not logged in.

#1 2013-08-17 18:28:33

jaredanderson
Member
Registered: 2013-04-06
Posts: 20

conky to show current i3 workspaces. i3-msg script [SOLVED]

Currently I have conky piped to i3 bar. i3 bar shows workspaces on it's own, I like this functionality but it overlaps with the conky output. I set workspace buttons off but of course i have no way of seeing workspaces. So in short I want conky  to display all the open workspaces and highlight the current one. the solution i have come up with is to use the command

 i3-msg -t get_workspaces 

which displays an output like

[{"num":1,"name":"1","visible":false,"focused":false,"rect":{"x":0,"y":19,"width":1366,"height":749},"output":"LVDS1","urgent":false},{"num":2,"name":"2","visible":false,"focused":false,"rect":{"x":0,"y":19,"width":1366,"height":749},"output":"LVDS1","urgent":false},{"num":3,"name":"3","visible":false,"focused":false,"rect":{"x":0,"y":19,"width":1366,"height":749},"output":"LVDS1","urgent":false},{"num":4,"name":"4","visible":false,"focused":false,"rect":{"x":0,"y":19,"width":1366,"height":749},"output":"LVDS1","urgent":false},{"num":5,"name":"5","visible":true,"focused":true,"rect":{"x":0,"y":19,"width":1366,"height":749},"output":"LVDS1","urgent":false}] 

so if i could pipe it to grep or form a sort of script to filter out the numbers and which desktop is visible i could easily put it into conky but i am not sure how to go about create such a script. Once i have the script i could easily create my conkyrc to give the desired output but i am not sure where to start with such a script.

Last edited by jaredanderson (2013-08-18 08:15:33)

Offline

#2 2013-08-17 19:48:35

progandy
Member
Registered: 2012-05-17
Posts: 5,322

Re: conky to show current i3 workspaces. i3-msg script [SOLVED]

You can use some bash and the jshon utility

#!/bin/bash
workspace=0
# jshon output: one field per line; first name then focues state, then the next name, ...
while read line; do
        if [ "$line" = true ]; then
                break # break if we found the active workspace 
        else
                workspace="$line" # evaluate the next workspace
        fi
done < <(i3-msg -t get_workspaces | jshon -a -e name -u -p -e focused -u)
echo $workspace # name of active workspace

PS: This breaks if you have a unnumbered workspace called "true".

Last edited by progandy (2013-08-17 19:50:15)


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' | alias ENGLISH='LANG=C.UTF-8 ' |

Offline

#3 2013-08-18 03:03:45

jv2112
Member
Registered: 2011-07-23
Posts: 160

Re: conky to show current i3 workspaces. i3-msg script [SOLVED]

I run two bars. Top for desktop and bottom for my Conky data.

Configs.
http://pastebin.com/u/jv2112


Pic:
http://s24.postimg.org/o0hzodlp1/2013_08_17.png


Life is pleasant. Death is peaceful. It's the transition that's troublesome. Isaac Asimov - / -

Offline

#4 2013-08-18 08:13:29

jaredanderson
Member
Registered: 2013-04-06
Posts: 20

Re: conky to show current i3 workspaces. i3-msg script [SOLVED]

To be honest never liked running two bars, takes up too much space. the jshon thing wasn't a hundred percent what i was looking for but it gave me a good base to build off of. my script looks like this.

#! /bin/bash
	if [ $# -lt 1 ]
		then
		    echo "no command"
		exit
	fi
case $1 in
	"color")
		color=`i3-msg -t get_workspaces | jshon -a -e name -u -p -e focused -u | paste -d ' ' - - | grep $2 | cut -c3-`
# set true and false to 0 and 1 for conky
#true and false determines highlight for workspace
	if [ "$color" = "" ]; then
		echo "null"
	elif [ "$color" = "false" ]; then
		echo "0"
	elif [ "$color" = "true" ]; then
		echo "1"	
	fi
   ;;
   "status")
		stat=`i3-msg -t get_workspaces | jshon -a -e name -u | grep $2`
#shows if workspace entered exists
#only negative is tenth workspace is plotted to 0 
	if [ "$stat" = "" ]; then
		echo "0"
	else
		echo $stat
	fi
	esac

there is probably an even better way to do this but this works for me.
I then added these lines to my conkyrc

{
	"full_text":
	    ${if_match ${exec ~/.scripts/i3wc status 1}>0}
	              "1",
		${else} "",
    	    ${endif} 
	"color":
	    ${if_match ${exec ~/.scripts/i3wc color 1}<1}"\#2aa198"${else}
	        ${if_match ${exec ~/.scripts/i3wc color 1}>0}"\#cb4b16"${else}
		    "\#ffffff"
            ${endif} 
        ${endif} 
    },

a slight draw back is this makes my conkyrc gargantuan because I have to insert this set individually for all nine workspaces.
but it shows all workspaces and highlights the current one which is exactly what 'i wanted so I can't complain. thanks for the help progandy never would have done it without you.
probably could remove the final else in the conkyrc under color. color wouldn't be use because no text is output

Last edited by jaredanderson (2013-08-18 08:27:24)

Offline

#5 2013-08-18 09:20:40

progandy
Member
Registered: 2012-05-17
Posts: 5,322

Re: conky to show current i3 workspaces. i3-msg script [SOLVED]

You should do everything in bash. Then you need only one call which is faster and shorter.

Last edited by progandy (2013-08-18 09:24:12)


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' | alias ENGLISH='LANG=C.UTF-8 ' |

Offline

#6 2013-08-18 15:18:20

jaredanderson
Member
Registered: 2013-04-06
Posts: 20

Re: conky to show current i3 workspaces. i3-msg script [SOLVED]

That is originally what I was going to do but I can't think of a way  to make the highlight work. That is there needs to be a separate command run for each desktop for the highlight to work correctly. because if it was one call in conky the text would display all the workspaces in which case you would only be able to highlight all of them.  or it would display all the workspaces and the current one separately in which case the current one would be highlighted but would be out of order. If you know of a way to make it work I am open to suggestions though.

Offline

#7 2013-08-18 15:30:34

progandy
Member
Registered: 2012-05-17
Posts: 5,322

Re: conky to show current i3 workspaces. i3-msg script [SOLVED]

Something like that?

#!/bin/bash
while read -r ws; do
	read -r active
        if [ "$active" = true ]; then
                echo "{ text = $ws, color = BAR },"

        else
                echo "{ text = $ws, color = FOO },"
        fi
done < <(i3-msg -t get_workspaces | jshon -a -e name -u -p -e focused -u)

| alias CUTF='LANG=en_XX.UTF-8@POSIX ' | alias ENGLISH='LANG=C.UTF-8 ' |

Offline

#8 2013-08-19 05:30:35

jaredanderson
Member
Registered: 2013-04-06
Posts: 20

Re: conky to show current i3 workspaces. i3-msg script [SOLVED]

simply beautiful. i wish I would of come up with that myself.  My script looks likes this

 
#!/bin/bash
while read -r ws; do
	read -r active
        if [ "$active" = true ]; then
                echo { \"full_text\":  \"$ws\", '"color"': '"#cb4b16"'},
        else
                echo { \"full_text\": \"$ws\", '"color"': '"#2aa198"'},
        fi
done < <(i3-msg -t get_workspaces | jshon -a -e name -u -p -e focused -u)

and instead of adding around hundred  lines to my conky i only had to add this one line

${exec ~/.scripts/i3wc2}

this is much better and far smoother. thank you.

Offline

#9 2013-08-19 07:37:05

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 227

Re: conky to show current i3 workspaces. i3-msg script [SOLVED]

Conky has the 'execp' command, which behaves like the 'exec' command, but then parses the output of your script.

First set up the colors you want in your .conkyrc. (Omit the '#' character before the hex value.)

color0 2aa198
color1 cb4b16

TEXT
${execp i3wc}

Then try this as your i3wc script:

#!/usr/bin/bash

get_focus() { [[ $1 == "true" ]] && echo 1 || echo 0; }

# read workspace data into an associative array
i=0
focus=0
declare -A ws_data
while read line; do
	[[ -z $line ]] && continue
	if (( i%2 == 0 )); then
		focus=$(get_focus "$line")
	else
		ws_data[$line]=$focus
	fi
	((i++))
done < <(i3-msg -t get_workspaces | jshon -a -e focused -u -p -e name -u)

# now output the data for all workspaces

for name in "${!ws_data[@]}"; do
	printf '${color%d}%s ' "${wsdata[$name]}" "$name"
done

Offline

#10 2013-08-20 00:05:25

jaredanderson
Member
Registered: 2013-04-06
Posts: 20

Re: conky to show current i3 workspaces. i3-msg script [SOLVED]

I was looking at the script it outputs this. three is the focused workspace for this example

${color0}1 ${color0}2 ${color0}3

it should output something like this

${color0}1 ${color0}2 ${color1}3 ${color}

I do not totally understand this particular script so I wasn't sure how to make that output happen. I could add the ${color} after execp and that might work but the highlight won't come out. As a side note if I wanted to change the color values output, like color3 instead of color1, how would i do that?

Offline

#11 2013-08-20 02:40:34

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 227

Re: conky to show current i3 workspaces. i3-msg script [SOLVED]

Well, I'm not sure why it doesn't work.  Put some echo's in the while loop like

while read line; do
	echo "i:($i) $line"
	[[ -z $line ]] && continue
	if (( i%2 == 0 )); then
		focus=$(get_focus "$line")
	else
		ws_data[$line]=$focus
	fi
	((i++))
	echo "ws_data: ${ws_data[@]}"
done < <(i3-msg -t get_workspaces | jshon -a -e focused -u -p -e name -u)

to see what's going on.

Offline

#12 2013-08-20 03:03:39

jaredanderson
Member
Registered: 2013-04-06
Posts: 20

Re: conky to show current i3 workspaces. i3-msg script [SOLVED]

this is the output

i:() false
./i3test: line 5: get_focus: command not found
ws_data: 
i:(1) 1
ws_data: 
i:(2) false
./i3test: line 5: get_focus: command not found
ws_data: 
i:(3) 2
ws_data:  
i:(4) false
./i3test: line 5: get_focus: command not found
ws_data:  
i:(5) 3
ws_data:   
i:(6) true
./i3test: line 5: get_focus: command not found
ws_data:   
i:(7) 4
ws_data:

Offline

#13 2013-08-20 03:30:33

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 227

Re: conky to show current i3 workspaces. i3-msg script [SOLVED]

Did you include the following line at the top of the script?

get_focus() { [[ $1 == "true" ]] && echo 1 || echo 0; }

Offline

#14 2013-08-20 04:14:58

jaredanderson
Member
Registered: 2013-04-06
Posts: 20

Re: conky to show current i3 workspaces. i3-msg script [SOLVED]

oh oops. I didn't read i just copy pasted the new script. here is the new output

i:(0) false
ws_data: 
i:(1) 1
ws_data: 0
i:(2) false
ws_data: 0
i:(3) 2
ws_data: 0 0
i:(4) true
ws_data: 0 0
i:(5) 3
ws_data: 0 0 1
${color0}1 ${color0}2 ${color0}3

Offline

#15 2013-08-20 05:33:11

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 227

Re: conky to show current i3 workspaces. i3-msg script [SOLVED]

You need to use the original script with just the echo's added.  Specifically you need the

declare -A ws_data

line.

Offline

#16 2013-08-20 05:37:52

jaredanderson
Member
Registered: 2013-04-06
Posts: 20

Re: conky to show current i3 workspaces. i3-msg script [SOLVED]

yes that is what I did. the output is the previous post

Offline

#17 2013-08-20 06:16:37

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 227

Re: conky to show current i3 workspaces. i3-msg script [SOLVED]

Oops, I see that the script I posted has a typo, sorry.

printf '${color%d}%s ' "${wsdata[$name]}" "$name"

should be

printf '${color%d}%s ' "${ws_data[$name]}" "$name"

note the missing underscore.

Offline

#18 2013-08-20 06:28:18

jaredanderson
Member
Registered: 2013-04-06
Posts: 20

Re: conky to show current i3 workspaces. i3-msg script [SOLVED]

yep that fixed the whole thing
works like a charm

Offline

Board footer

Powered by FluxBB