master ohmu/wmutils / scripts / snap.sh
ohmu  ·  2026-07-25
 1#!/bin/sh
 2#
 3# wmutils/contrib>: snap.sh, 9/12/15 kekler 
 4# snaps focued window to the left, right, top, or bottom edge
 5
 6usage() {
 7    echo "usage: $(basename $0) <direction>" >&2
 8    exit 1
 9}
10
11# default values for gaps and master area
12TOP_PANEL=${PANEL:-20}
13GAP=${GAP:-5}
14
15# get current window id and its borderwidth
16PFW=$(pfw)
17BW=$(wattr b $PFW)
18
19# get root window's size
20ROOT=$(lsw -r)
21SW=$(wattr w $ROOT)
22SH=$(wattr h $ROOT)
23
24# calculate usable screen size (without borders and gaps)
25SH=$((SH - TOP_PANEL))
26
27snap_up() 
28{
29    wtp $GAP $((GAP + TOP_PANEL)) $((SW - 2*GAP - 2*BW)) $((SH/2 - 2*BW - GAP - GAP/2)) $PFW
30}
31
32snap_right() 
33{
34    wtp $((SW - SW/2 + GAP/2)) $((GAP + TOP_PANEL)) $((SW/2 - 2*BW - GAP - GAP/2)) $((SH - 2*BW - 2*GAP)) $PFW
35}
36
37snap_down() 
38{
39    wtp $GAP $((SH - SH/2 + GAP/2 + TOP_PANEL)) $((SW - 2*GAP - 2*BW)) $((SH/2 - 2*BW - GAP - GAP/2)) $PFW
40}
41
42snap_left()
43{
44    wtp $GAP $((GAP + TOP_PANEL)) $((SW/2 - 2*BW - GAP - GAP/2)) $((SH - 2*BW - 2*GAP)) $PFW
45}
46
47HSW=$((SW/2 - 2*BW - GAP - GAP/2))
48HSH=$((SH/2 - 2*BW - GAP - GAP/2))
49
50snap_tr()
51{
52    wtp $((SW - SW/2 + GAP/2)) $((GAP + TOP_PANEL)) $HSW $HSH $PFW
53}
54
55snap_br()
56{
57    wtp $((SW - SW/2 + GAP/2)) $((SH - SH/2 + GAP/2 + TOP_PANEL)) $HSW $HSH $PFW
58}
59
60snap_tl()
61{
62    wtp $GAP $((GAP + TOP_PANEL)) $HSW $HSH $PFW
63}
64
65snap_bl()
66{
67    wtp $GAP $((SH - SH/2 + GAP/2 + TOP_PANEL)) $HSW $HSH $PFW
68}
69
70case $1 in
71    h|a|west|left)  snap_left ;;
72    j|s|south|down) snap_down ;;
73    k|w|north|up)   snap_up ;;
74    l|d|east|right) snap_right ;;
75    tr|northeast)   snap_tr ;;
76    br|southeast)   snap_br ;;
77    tl|northwest)   snap_tl ;;
78    bl|southwest)   snap_bl ;;
79esac
80