0

Hi I am making a lemonbar, and I want to make it in python or go or c or something else than shell script, because I want the program to manage the loop, and the threads going on in there.

I found out that I can make a script where I call eg. a python script in every itteration in the loop, like

bar.sh

while true
do
    python script.py
    sleep 1
done

and then the script.py

print('%{c}hello')

and then I run it like this

sh bar.sh | lemonbar

That works and I get a hello in the middle of the bar. But I would like to do something like this

bar.py

while True:
    print('%{c}hello')

and pipe it to lemonbar

python bar.py | lemonbar

This doesnt work. I get a bar but there is nothing on it.

My guess is that is has something to do with the file descriptors used by pythons print function compared to the ones used by my shell zsh

Edit: I also tried with

import sys, time

fd = sys.stdout

while True:
    fd.write('hej\n')
    time.sleep(1)

That didnt change anything.

Thank you for reading my question. I hope you can help :)

1 Answer 1

0

Actually I just found out how to do it by reading how someone implemented a module for lemonbar

import time
from subprocess import Popen, PIPE

fd = Popen('lemonbar', stdin=PIPE, stdout=PIPE, encoding='UTF-8')

while True:
    time.sleep(1)
    fd.stdin.write('%{c}hello')
    fd.stdin.flush()
    print(fd.stdout.read())

The trick was to flush the filedescriptor after writing to it

So I could also do it like this

bar.py

import time 
import sys
fd = sys.stdout
while True:
    fd.write("%{c}hello")
    fd.flush()
    time.sleep(1)

and then run it like normally

python bar.py | lemonbar

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.