-
Notifications
You must be signed in to change notification settings - Fork 0
/
resize-tool
executable file
·151 lines (119 loc) · 3.36 KB
/
resize-tool
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python3
import i3ipc
import sys
conn = i3ipc.Connection()
tree = conn.get_tree()
def single_leaf():
"""
Returns (single, vertical).
single is true if the current workspace contains only a single container.
For this, a tabbed container counts as only one.
vertical is true if the workspace is split vertically and single is false.
"""
node = tree.find_focused()
single = True
vertical = False
# Check if there is only a single leaf.
# If not, check if the curent container is in a vertical split.
while True:
parent = node.parent
if node.type == "workspace" or not parent:
break
elif parent.type == "floating_con":
single = False
break
elif len(parent.nodes) > 1 and parent.layout == "splith":
single = False
break
elif len(parent.nodes) > 1 and parent.layout == "splitv":
single = False
vertical = True
break
node = parent
return single, vertical
def workspace_stats():
"""
Returns (rect, gaps).
rect is the rectangle of the current workspace.
gaps are how much gap sizes of the current workspace differ from the default.
"""
node = tree.find_focused()
while True:
parent = node.parent
if node.type == "workspace" or not parent:
return node.rect, node.gaps
break
node = parent
def get_xresources():
from Xlib import display, rdb, Xatom
# Get Xresources
dbstr = display.Display().screen(0).root.get_full_property(
Xatom.RESOURCE_MANAGER, Xatom.STRING
)
db = rdb.ResourceDB(string=dbstr.value.decode())
dpi = int(db.get("i3.dpi", "i3.dpi", "0"))
hgap = int(db.get("i3.hgap", "i3.hgap", "0"))
igap = int(db.get("i3.igap", "i3.igap", "0"))
return dpi, hgap, igap
def resize(direction, amount):
"""
Resize the current container along to the given direction.
If there is only a single container, resize by adjusting gaps.
If the direction is "natural", resize vertically in a splitv container, else
horizontally. If it is "orhtogonal", do the opposite.
"""
if direction not in [
"natural", "orthogonal", "horizontal", "vertical",
"top", "bottom", "left", "right",
]:
usage()
try:
amount = int(amount)
except ValueError:
print("Bad resize amount given.")
exit(1)
single, vertical = single_leaf()
if single:
if direction == "natural":
direction = "horizontal"
elif direction == "orthogonal":
direction = "vertical"
if amount < 0:
mode = "plus"
amount = -amount
else:
mode = "minus"
conn.command(f"gaps {direction} current {mode} {amount}")
else:
if direction == "horizontal":
direction = "width"
elif direction == "vertical":
direction = "height"
elif direction == "natural":
direction = "height" if vertical else "width"
elif direction == "orthogonal":
direction = "width" if vertical else "height"
elif direction == "top":
direction = "up"
elif direction == "bottom":
direction = "down"
if amount < 0:
mode = "shrink"
amount = -amount
else:
mode = "grow"
conn.command(f"resize {mode} {direction} {amount} px or {amount//16} ppt")
def usage():
print("Usage: resize_tool DIRECTION AMOUNT")
print("DIRECTON can be: top, bottom, left, right,")
print(" horizontal, vertical, natural, orthogonal")
exit(1)
def main(command, direction=None, amount=None):
if command == "resize":
resize(direction, amount)
else:
usage()
if __name__ == "__main__":
if len(sys.argv) != 4:
usage()
main(*sys.argv[1:])