mostly improvements, wip
[distro-setup] / i3-split-maybe
1 #!/bin/bash
2 # I, Ian Kelling, follow the GNU license recommendations at
3 # https://www.gnu.org/licenses/license-recommendations.en.html. They
4 # recommend that small programs, < 300 lines, be licensed under the
5 # Apache License 2.0. This file contains or is part of one or more small
6 # programs. If a small program grows beyond 300 lines, I plan to switch
7 # its license to GPL.
8
9 # Copyright 2024 Ian Kelling
10
11 # Licensed under the Apache License, Version 2.0 (the "License");
12 # you may not use this file except in compliance with the License.
13 # You may obtain a copy of the License at
14
15 # http://www.apache.org/licenses/LICENSE-2.0
16
17 # Unless required by applicable law or agreed to in writing, software
18 # distributed under the License is distributed on an "AS IS" BASIS,
19 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 # See the License for the specific language governing permissions and
21 # limitations under the License.
22
23
24 set -e; . /usr/local/lib/bash-bear; set +e
25
26 # We use this along with
27 # /a/opt/i3-alternating-layout/alternating_layouts.py to anticipate when
28 # we want to split/tab windows. There are 2 options of when to do it:
29 # just after a window is created, or just before a window is
30 # created.
31 #
32 # * Doing it after a window is created allows you to move a window into
33 # the split that only has 1 window, whereas the other way doesn't. For
34 # my use cases, I think I don't really want to move it into the split if
35 # it is a tabbed split. upon further reflection, I've determined that
36 # single window containers are inherently confusing because they tend to
37 # exist and get nested at unexpected times and then it is unclear how to
38 # get rid of them and what is going on and the benefit is generally not
39 # worth it. This command helps identify single window containers during
40 # testing: /a/opt/i3ipc-python/examples/i3-debug-console.py
41 #
42 # * Doing it just before a windows is created, you need to call this
43 # script, which means wrapping launch of a program, which I have no way
44 # to do for all cases, I just do it for the common programs I have bound
45 # to keys in i3.
46 #
47 # * Doing it after a window is created also leaves that split behind if
48 # the window is closed. I partially deal with that below.
49 #
50 # I have a keybind which disables both, it runs /b/ds/i3-auto-layout-toggle
51
52
53 dry_run=false
54 m() { "$@"; }
55 d() {
56 if $dry_run; then
57 printf "%s\n" "$*"
58 fi
59 }
60 case $1 in
61 -n)
62 dry_run=true
63 m() { printf "%s\n" "$*"; }
64 ;;
65 esac
66
67 if [[ -e /tmp/iank-i3-no-auto ]]; then
68 exit 0
69 fi
70
71
72 tmp=$(mktemp)
73
74 i3-msg -t get_workspaces | jq ".[]| select(.focused==true) | .rect | .width, .height" >$tmp
75
76 { read -r screen_width; read -r screen_height; } <$tmp
77
78 i3-msg -t get_tree | jq -r ".. | select(.focused? == true).rect | .width, .height" >$tmp
79
80 half_w=$(( screen_width / 2 ))
81 half_h=$(( screen_height / 2 ))
82
83
84 { read -r w; read -r h; } <$tmp
85
86 d w=$w , h=$h , half_w=$half_w , half_h=$half_h
87
88 if (( screen_width < 1920 )); then
89 # haven't considered this case yet
90 exit 0
91 fi
92
93
94 if (( w <= half_w && h <= half_h )); then
95 m i3-msg "split vertical, layout tabbed"
96 elif (( w == screen_width )); then
97 # if we had 2 windows on screen, made them vertical splits, then
98 # closed one, it stays vertical split, but we want it horizontal at
99 # that point. So, make it horizontal here.
100 m i3-msg "split horizontal"
101 fi
102
103 rm -f $tmp