Add script to generate config for custom resolutions

The script looks at the directories 720p/ and 1080p/ and tries to generate a
new directory "{resolution}p" where the numbers are adjusted. It does this by
going through all files in the initial directories and scaling changed numbers
linearly.
For example: If a number is 2 for 720p, 3 for 1080p, and you generate for 1400p
You will end up with 4 for 1440p.
Likewise 5 (2+3) (720p) and 6 (3+3) (1080p) would yield 7 (4+3).
This commit is contained in:
Syphdias 2021-01-09 17:29:14 +01:00
parent 9c4093c665
commit bd88a90a19
No known key found for this signature in database
GPG Key ID: 6F62E6FB3E67F92D
2 changed files with 132 additions and 2 deletions

View File

@ -0,0 +1,110 @@
#!/usr/bin/env python3
"""This script generates new rofi config files for the given resolution
The script looks at the directories 720p/ and 1080p/ and tries to generate a
new directory "{resolution}p" where the numbers are adjusted. It does this by
going through all files in the initial directories and scaling changed numbers
linearly.
For example: If a number is 2 for 720p, 3 for 1080p, and you generate for 1400p
You will end up with 4 for 1440p.
Likewise 5 (2+3) (720p) and 6 (3+3) (1080p) would yield 7 (4+3).
"""
from sys import stderr
import re
from argparse import ArgumentParser
from os import walk
from shutil import copytree
import numpy as np
number = re.compile(r"\d+(\.\d+)?")
def scale_x_to_1440p(a, b):
m = np.array([[720, 1], [1080, 1]])
s = np.array([a, b])
x, y = np.linalg.solve(m, s)
c = x*1440 + y
return round(c, 2)
def convert_lines(line_from, line_to):
start_ws = line_from.removesuffix(line_from.lstrip())
line_from_fields = line_from.split()
line_to_fields = line_to.split()
line_out_fields = []
for idx, field in enumerate(line_from_fields):
searched_num_in_from = re.search(number, field)
try:
searched_num_in_to = re.search(number, line_to_fields[idx])
except IndexError:
searched_num_in_to = None
if searched_num_in_from and searched_num_in_to:
num_in_from = searched_num_in_from.group()
num_in_to = searched_num_in_to.group()
# scale and replace if number differs
if num_in_from != num_in_to:
scaled_field = scale_x_to_1440p(float(num_in_from),
float(num_in_to))
field = field.replace(str(num_in_from), f"{scaled_field:g}")
line_out_fields.append(field)
line_out = " ".join(line_out_fields)
return f"{start_ws}{line_out}\n"
def main(resolution):
scale_from = 720
scale_to = 1080
try:
copytree(f"{scale_from}p", f"{resolution}p")
except FileExistsError:
print(f"There is already a file or directory called \"{resolution}p\"."
"Please remove it before running the script!", file=stderr)
exit(1)
# loop over all files
for root, dirnames, filenames in walk(f"{scale_from}p"):
for filename in filenames:
# cut away basedir
filename = f"{root}/{filename}".removeprefix(f"{scale_from}p/")
# compare files
file_from = open(f"{scale_from}p/{filename}", "r")
file_to = open(f"{scale_to}p/{filename}", "r")
line_from = file_from.readline()
line_to = file_to.readline()
lines_new_resolution = ""
while line_from or line_to:
if line_from != line_to:
lines_new_resolution += convert_lines(line_from, line_to)
else:
lines_new_resolution += line_from
# go to next line
line_from = file_from.readline()
line_to = file_to.readline()
file_from.close()
file_to.close()
with open(f"{resolution}p/{filename}", "w") as file_out:
file_out.write(lines_new_resolution)
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument(
"resolution",
type=int,
help="Height of of your screens resolution, e.g. 1440 for 2560x1440",
)
args = parser.parse_args()
main(args.resolution)

View File

@ -36,16 +36,31 @@ install_themes() {
fi fi
} }
# Clean up RES variable
clean_up_RES() {
number_pattern='^[0-9]+p?$'
if [[ "${RES}" =~ ${number_pattern} ]]; then
RES="$(tr -d p <<<"$RES")p"
else
echo -e "Sorry, something went wrong when checking your" \
"custom resolution.\n"\
"Please enter the number of your resolution height" \
"in pixels, e.g. 1440."
exit 1
fi
}
# Main # Main
main() { main() {
clear clear
cat <<- EOF cat <<- EOF
[*] Installing Rofi Themes... [*] Installing Rofi Themes...
[*] Choose your screen resolution - [*] Choose your screen resolution -
[1] 1920x1080 [1] 1920x1080
[2] 1366x768 [2] 1366x768
[3] Custom (Run ./generate-custom-resolution-config.py first)
EOF EOF
read -p "[?] Select Option : " read -p "[?] Select Option : "
@ -58,6 +73,11 @@ main() {
RES='720p' RES='720p'
install_fonts install_fonts
install_themes install_themes
elif [[ $REPLY == "3" ]]; then
read -p "How many pixes high is your resolution? " RES
clean_up_RES
install_fonts
install_themes
else else
echo -e "\n[!] Invalid Option, Exiting...\n" echo -e "\n[!] Invalid Option, Exiting...\n"
exit 1 exit 1