summaryrefslogtreecommitdiff
path: root/src/update_i3.py
blob: b06b47e98672c47a7980debd11eecd90d89f591d (plain)
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
import subprocess
import cv2 as cv
import os

from rich import print

def updatei3Theme(
        config_path: str, 
        img_path: str, 
        colors: list, 
        compliments: list, 
        update_i3_lock: bool, 
        dmenu: bool,
        lock_img_path: str,
        menu_keybind: str,
        ) -> None:
    
    print('[bold red]Updating i3 color scheme')
    

    update_i3_colors(config_path, colors, compliments, dmenu, img_path, menu_keybind)
     
    if update_i3_lock:
        change_i3_lock_img(img_path, lock_img_path)
    
    
    print("[bold red]Restarting i3")
    os.system("i3 restart")


def change_i3_lock_img(img_path: str, lock_img_path):
    img = cv.imread(img_path)
    
    imgHeight, imgWidth, _ = img.shape
    screenWidth, screenHeight = getScreenResolution()
    h_lock_scale = screenWidth / imgWidth
    v_lock_scale = screenHeight / imgHeight
    
    print('[bold red]Creating lock screen')
      
    dim = (int(imgWidth * h_lock_scale), int(imgHeight * v_lock_scale))
    img = cv.resize(img, dim, interpolation= cv.INTER_AREA)
     
    cv.imwrite('lock.png', img)
    os.rename('lock.png', lock_img_path)


def update_i3_colors(
        config_path: str, 
        colors: list, 
        compliments: list, 
        dmenu: bool, 
        img_path: str,
        menu_keybind: str,
        ) -> None:
    
    data = ''
    
    with open(config_path, 'r') as file:
        data = file.readlines()
     
    for i, line in enumerate(data):
        # update colors
        if "set $bgcolor" in line:
            data[i] = 'set $bgcolor ' + colors[0] + '\n'
        if "set $in-bgcolor" in line:
            data[i] = 'set $in-bgcolor ' + colors[1] + '\n'
        if "set $text" in line:
            data[i] = 'set $text ' + compliments[0] + '\n'
        if "set $indicator" in line:
            data[i] = 'set $indicator ' + colors[2] + '\n'
        if "set $in-text" in line:
            data[i] = 'set $in-text ' + compliments[1] + '\n'
        # update background image
        if "set $bgimage" in line:
            data[i] = 'set $bgimage ' + img_path + '\n'
        # update i3 lock image        
        if f"bindsym {menu_keybind} exec --no-startup-id dmenu_run" in line:
            if dmenu:
                print('[bold red]Updating Dmenu color scheme')
                data[i] = f"bindsym {menu_keybind} exec --no-startup-id dmenu_run -nb '{colors[0]}' -sf '{compliments[0]}' -sb '{colors[1]}' -nf '{compliments[1]}'\n"


    with open(config_path, 'w') as file:
        file.writelines(data)
 

def getScreenResolution() -> tuple:
    output = subprocess.Popen("xdpyinfo | awk '/dimensions:/ { print $2 }'",shell=True, stdout=subprocess.PIPE).communicate()[0]
    resolution = output.split()[0].split(b'x')
    width = int(resolution[0].decode('UTF-8'))
    height = int(resolution[1].decode('UTF-8'))
    return width, height