0

因此,当我在 i3 上打开的工作区少于 3 个时,我拥有的脚本是在屏幕上输出更大的 dzen2,并在打开的工作区超过 3 个时将其缩小。这是脚本 dzresize.py:

import subprocess
def main():
    #gets the number of workspaces from i3
    status = subprocess.check_output(["i3-msg", "-t", "get_workspaces"])
    #puts status in a list
    status_list = list(status)
    #sets name to 0
    name = 0
    for i in status_list:
        if i == "name":
            name +=1
    #counts the amount of name in status_list
    if len(status_list) <=3:
    #if the workspaces are less than or equal to 3, expands dzen2 with conky output
            subprocess.check_output(["conky", "-d", "-c", "~/bin/conkyrc_cli|dzen2", "-fg", "#666666", "-bg", "#333333", "-ta", "left", "-w", "725", "-x", "54", "-y", "750"])
    else:
    #if the workspaces are greater than or equal to 3 run the minimal smaller size dzen2 with conky
            subprocess.check_output(["dzconky.sh"])
main()

这是脚本的输出:

Conky: forked to background, pid is 18519

i3-msg -t get_workspaces 的示例输出(如果我有 2 个工作区打开):

i3-msg -t get_workspaces
[{"num":1,"name":"1","visible":false,"focused":false,"rect":{"x":0,"y":0,"width":1366,"height":749},"#output":"LVDS1","urgent":false},{"num":2,"name":"2","visible":true,"focused":true,"rect":{"x":0,"y":#0,"width":1366,"height":749},"output":"LVDS1","urgent":false}]

依赖于 i3、dzen2 和文件 ~/bin/conkyrc_cli 和 ~/bin/dzconky.sh。~/bin/conkyrc_cli:

# Conky configuration for Dzen2, to be piped into i3bar
##############################################
#  Settings
##############################################
background no
out_to_console yes
update_interval 1.0
total_run_times 0
use_spacer none
TEXT
^fg(\#6699cc)Processor^fg()^fg(green)${cpu cpu0}.00%^fg()^fg(white)|^fg(\#6699cc)Memory^fg()^fg(green)${memperc}.00%^fg()^fg(white)|^fg(\#6699cc)Root^fg()^fg(green)${fs_used_perc /}.00%^fg()^fg(white)|^fg(\#6699cc)Home^fg()^fg(green)${fs_used_perc /home}.00%^fg()^fg(white)|^fg(\#6699cc)Temperature^fg()^fg(green)${hwmon temp 1}'C^fg()^fg(white)|^fg(\#6699cc)Dn^fg()^fg(green)${downspeedf wlan0}KiB^fg()^fg(white)|^fg(\#6699cc)U^fg()^fg(green)${upspeedf wlan0}KiB^fg()

~/bin/dzconky.sh:

#!/bin/sh
exec conky -d -c "$HOME/bin/conkyrc_cli" | dzen2 -fg "#666666" -bg "#333333" -ta left -w 607 -x 188 -y 750 &
exit 0

编辑:更新代码以反映新的变化和新的输出

4

2 回答 2

2

你全忘,subprocess.call([...])

["i3-msg", "-t", "get_workspaces"]

编辑:

if len(status_list) <=3:
    subprocess.check_output(["dzconky_for_3_workspaces.sh"])
else:
    subprocess.check_output(["dzconky.sh"])

dzconky_for_3_workspaces.sh

#!/bin/sh
exec conky -d -c "$HOME/bin/conkyrc_cli" | dzen2 -fg "#666666" -bg "#333333" -ta left -w 725 -x 54 -y 750 &
exit 0
于 2013-07-07T22:58:28.500 回答
0

您可以使用 python 的sh包。这是一个非常好的用于 shell 执行的 Pythonic 包装器。如果您查看文档,您会发现在sh大多数情况下您可能需要几行代码。

安装

点安装 sh

来自文档:

容易:

从 sh 导入 curl、git、ifconfig、ls
ls()
混帐('获取')
ifconfig(a=真)

重定向:

ls(_out="files.list")
ls("不存在", _err="error.txt")
# 标准输入也有 _in

管道:

# 按最大文件排序这个目录
打印(排序(du(glob(“*”),“-sb”),“-rn”))
于 2013-07-07T23:18:41.153 回答